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
| #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #define inf 0x3FFFFFFF using namespace std; struct Node { int to,next,v; }e[6005],mp[6005]; int s,t,n,m,h[155],f[155][155],d[155],gap[155],fa[155],num[22505],cnt=1; bool vis[155]; inline void Addedge(int x,int y,int v) { mp[++cnt]=(Node){y,h[x],v};h[x]=cnt;return; } inline int dfs(int x,int maxf) { int i,y,ret=0,delta; if(x==t)return maxf; for(i=h[x];i;i=e[i].next) { y=e[i].to; if(e[i].v&&d[x]==d[y]+1) { delta=dfs(y,min(maxf,e[i].v)); e[i].v-=delta; e[i^1].v+=delta; ret+=delta; maxf-=delta; if(!maxf||d[x]==n)return ret; } } if(!(--gap[d[x]]))d[s]=n; ++gap[++d[x]]; return ret; } inline void dfs(int x) { int i,y; vis[x]=true; for(i=h[x];i;i=e[i].next) { y=e[i].to; if(e[i].v&&!vis[y])dfs(y); } return; } inline void Gusfield() { int i,j,ans; memset(f,0x3F,sizeof(f)); for(i=2;i<=n;++i)fa[i]=1; for(i=2;i<=n;++i) { memcpy(e,mp,sizeof(mp)); memset(gap,0,sizeof(gap)); memset(d,0,sizeof(d)); memset(vis,0,sizeof(vis)); s=fa[i];t=i;ans=0; gap[0]=n; while(d[s]<n)ans+=dfs(s,inf); dfs(s); for(j=i+1;j<=n;++j) { if(!vis[j]&&fa[j]==fa[i])fa[j]=i; } for(j=1;j<i;++j)f[i][j]=f[j][i]=min(f[fa[i]][j],ans); } return; } int main(void) { int i,j,q,T,x,y,v; scanf("%d",&T); while(T--) { memset(h,0,sizeof(h));cnt=1; scanf("%d%d",&n,&m); for(i=1;i<=m;++i) { scanf("%d%d%d",&x,&y,&v); Addedge(x,y,v); Addedge(y,x,v); } Gusfield();num[0]=0; for(i=1;i<=n;++i) { for(j=i+1;j<=n;++j) { num[++num[0]]=f[i][j]; } } sort(num+1,num+num[0]+1); scanf("%d",&q); for(i=1;i<=q;++i) { scanf("%d",&x); printf("%d\n",upper_bound(num+1,num+num[0]+1,x)-num-1); } putchar('\n'); } return 0; }
|