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
| #include<stdio.h> #include<string.h> #include<queue> #define INF 99999999 const int N=1010; using namespace std; int m,n,edge[N][N],pre[N]; bool vis[N]; int bfs(int s,int t) { queue<int> q; memset(vis,0,sizeof(vis)); memset(pre,0,sizeof(pre)); q.push(s); vis[s]=true; while(!q.empty()){ int pos=q.front(); q.pop(); if(pos==t) return true; for(int i=1;i<=n;i++){ if(!vis[i]&&edge[pos][i]>0){ pre[i]=pos; q.push(i); vis[i]=true; } } } return false; } int maxFlow(int s,int t) { int flow=0; while(bfs(s,t)){ int d=INF; for(int i=t;i!=s;i=pre[i]) d=min(d,edge[pre[i]][i]); for(int i=t;i!=s;i=pre[i]){ edge[pre[i]][i]-=d; edge[i][pre[i]]+=d; } flow+=d; } return flow; } int main() { int T; scanf("%d",&T); for(int k=1;k<=T;k++){ scanf("%d%d",&n,&m); memset(edge,0,sizeof(edge)); int x,y,c; while(m--){ scanf("%d%d%d",&x,&y,&c); edge[x][y]+=c; } int flow=maxFlow(1,n); printf("Case %d: %d\n",k,flow); } return 0; }
|