标签:class push div logs blog code -- 网络流 down
占坑
#include <iostream> #include <stdio.h> #include <algorithm> #include <string.h> #include <queue> using namespace std; const int MAX=1000000; int map[20][20],flow[20],pre[20],n,m; bool vis[20]; int BFS() { int up; queue<int> q; vis[1]=1; memset(pre,-1,sizeof(pre)); memset(vis,0,sizeof(vis)); for(int i=1; i<=n; i++) flow[i]=MAX; q.push(1); while(!q.empty()) { up=q.front(); q.pop(); if(up==n) break; for(int i=1; i<=n; i++) { if(!vis[i]&&map[up][i]>0) { vis[i]=1; flow[i]=min(flow[up],map[up][i]); pre[i]=up; q.push(i); } } } if(!vis[n]||n==1) return -1; return flow[n]; } int EK() { int d,maxflow=0,up,down; maxflow=0; while((d=BFS())!=-1) { maxflow+=d; down=n; while(down!=1) { up=pre[down]; map[up][down]-=d; map[down][up]+=d; down=up; } } return maxflow; } int main() { int T,a,b,c,h=1,i; scanf("%d",&T); while(T--)//T组测试样例 { memset(map,0,sizeof(map)); scanf("%d%d",&n,&m);//n个点,m条边 for(i=1; i<=m; i++) { scanf("%d%d%d",&a,&b,&c); map[a][b]+=c; } printf("Case %d: %d\n",h++,EK());//输出从1到n号点的最大流 } return 0; }
标签:class push div logs blog code -- 网络流 down
原文地址:http://www.cnblogs.com/aiguona/p/7638357.html