就是最大流问题。用的EK。
#include<iostream> #include<cstdio> #include<algorithm> #include<cstdlib> #include<cstring> #define maxx 1000000000 #include<queue> using namespace std; int ans,n,p[1000],a[1000],g[1000][1000]; int work(int s,int t) { ans=0; while (true) { queue<int>q; for (int i=1;i<=n;i++) a[i]=0,p[i]=0; a[s]=maxx; q.push(s); while (!q.empty()) { int now=q.front();q.pop(); for (int i=1;i<=n;i++) if ((a[i]==0)&&(g[now][i]>0)) { a[i]=min(a[now],g[now][i]); p[i]=now; q.push(i); } if (a[t]) break; } if (!a[t]) break; ans+=a[t]; for (int i=t;i!=s;i=p[i]) { g[p[i]][i]-=a[t]; g[i][p[i]]+=a[t]; } } return ans; } int main() { scanf("%d",&n); int tot=0; while (n!=0) { int st,en,m; scanf("%d%d%d",&st,&en,&m); for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) g[i][j]=0; int x,y,z; for (int i=1;i<=m;i++) { scanf("%d%d%d",&x,&y,&z); g[x][y]+=z; g[y][x]+=z; } tot++; printf("Network %d\n",tot); printf("The bandwidth is %d.\n",work(st,en)); cout<<endl; scanf("%d",&n); } return 0; }