1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
| #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #define ll long long using namespace std;
const int N=1005,M=1e5+10; const ll INF=1e15;
int h[N],e[M],ne[M],idx; ll w[M],ans[N][N]; int cur[N],d[N],q[N],S,T,n,m; int node[N],tmp1[N],tmp2[N];
void add(int a,int b,int c) { e[idx]=b,ne[idx]=h[a],w[idx]=c,h[a]=idx++; e[idx]=a,ne[idx]=h[b],w[idx]=0,h[b]=idx++; }
bool bfs() { memset(d,0,sizeof d); int hh=1,tt=1; q[1]=S;cur[S]=h[S];d[S]=1; while (hh<=tt) { int x=q[hh++]; for (int i=h[x];~i;i=ne[i]) if (!d[e[i]]&&w[i]) { d[e[i]]=d[x]+1; cur[e[i]]=h[e[i]]; if (e[i]==T) return true; q[++tt]=e[i]; } } return false; }
int findflow(int x,ll limit) { if (x==T) return limit; ll flow=0; for (int i=cur[x];~i&&flow<limit;i=ne[i]) { cur[x]=i; if (d[e[i]]==d[x]+1&&w[i]) { int t=findflow(e[i],min(w[i],limit-flow)); if (!t) d[e[i]]=-1; w[i]-=t,w[i^1]+=t,flow+=t; } } return flow; }
void init() { for (int i=0;i<idx;i+=2) w[i]=(w[i]+w[i^1]),w[i^1]=0; return; }
ll dinic() { init(); ll r=0,flow; while (bfs()) while (flow=findflow(S,INF)) r+=flow; return r; }
void work(int l,int r) { if (l==r) return ; S=node[l],T=node[l+1]; ll t=dinic(); int s=node[l],tt=node[l+1]; ans[T][S]=ans[S][T]=t;
int cnt1=0,cnt2=0; for (int i=l;i<=r;++i) if (d[node[i]]) tmp1[++cnt1]=node[i]; else tmp2[++cnt2]=node[i]; for (int i=1;i<=cnt1;++i) node[i+l-1]=tmp1[i]; for (int i=1;i<=cnt2;++i) node[cnt1+l+i-1]=tmp2[i]; work(l,l+cnt1-1); work(l+cnt1,r); for (int i=1;i<=cnt1;++i) for (int j=1;j<=cnt2;++j) { int ii=node[i+l-1],jj=node[j+cnt1+l-1]; ans[jj][ii]=ans[ii][jj]=min(min(ans[ii][s],ans[s][tt]),ans[tt][jj]); } return; }
int main() { int Case=0;cin>>Case; while (Case--) { memset(h,-1,sizeof h); idx=0; cin>>n>>m; for (int i=1;i<=n;++i) for (int j=1;j<=n;++j) ans[i][j]=INF; int x,y; ll z; while (m--) { scanf("%d %d %lld",&x,&y,&z); add(x,y,z);add(y,x,z); } for (int i=0;i<=n;++i) node[i]=i; work(1,n); int que;cin>>que; while (que--) { scanf("%lld",&z); int tot=0; for (int i=1;i<=n;++i) for (int j=1;j<=n;++j) if (ans[i][j]<=z) tot++; printf("%d\n",tot/2); } puts(""); } return 0; }
|