标签:numbers nes ports eve ber power connect tput ext
题目链接:http://poj.org/problem?id=1459
题目:
Description
Input
Output
Sample Input
2 1 1 2 (0,1)20 (1,0)10 (0)15 (1)20 7 2 3 13 (0,0)1 (0,1)2 (0,2)5 (1,0)1 (1,2)8 (2,3)1 (2,4)7 (3,5)2 (3,6)5 (4,2)7 (4,3)5 (4,5)1 (6,0)5 (0)5 (1)2 (3)2 (4)1 (5)4
Sample Output
15 6
Hint
1 //EK算法 2 #include <queue> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 8 const int N=233; 9 const int INF=0x3f3f3f3f; 10 int n,m,s,t,Map[N][N],path[N],flow[N]; 11 12 int bfs(){ 13 int tmp; 14 queue <int> Q; 15 while(!Q.empty()) Q.pop(); 16 memset(path,-1,sizeof(path)); 17 path[s]=0;flow[s]=INF; 18 Q.push(s); 19 while(!Q.empty()){ 20 tmp=Q.front();Q.pop(); 21 if(tmp==t) break; 22 for(int i=0;i<=n+1;i++){ 23 if(i!=s&&path[i]==-1&&Map[tmp][i]){ 24 flow[i]=flow[tmp]<Map[tmp][i]?flow[tmp]:Map[tmp][i]; 25 Q.push(i); 26 path[i]=tmp; 27 } 28 } 29 } 30 if(path[t]==-1) return -1; 31 return flow[n+1]; 32 } 33 34 int Edmonds_Karp(){ 35 int max_flow=0,step,now,pre; 36 while((step=bfs())!=-1){ 37 max_flow+=step; 38 now=t; 39 while(now!=s){ 40 pre=path[now]; 41 Map[pre][now]-=step; 42 Map[now][pre]+=step; 43 now=pre; 44 } 45 } 46 return max_flow; 47 } 48 49 int main(){ 50 int np,nc; 51 while(cin>>n>>np>>nc>>m){ 52 memset(Map,0,sizeof(Map)); 53 char zf; 54 int a,b,c; 55 s=n;t=n+1; 56 for(int i=1;i<=m;i++){ 57 cin>>zf>>a>>zf>>b>>zf>>c; 58 Map[a][b]=c; 59 } 60 for(int i=1;i<=np;i++){ 61 cin>>zf>>b>>zf>>c; 62 Map[s][b]=c; 63 } 64 for(int i=1;i<=nc;i++){ 65 cin>>zf>>a>>zf>>c; 66 Map[a][t]=c; 67 } 68 cout<<Edmonds_Karp()<<endl; 69 } 70 return 0; 71 }
1 //Dinic算法 2 #include <queue> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 8 const int N=233; 9 const int M=23333; 10 const int INF=0x3f3f3f3f; 11 int n,m,s,t,cnt; 12 int Head[N],Depth[N],Next[M],V[M],W[M],cur[N]; 13 14 void init(){ 15 cnt=-1; 16 memset(Head,-1,sizeof(Head)); 17 memset(Next,-1,sizeof(Next)); 18 } 19 20 void add_edge(int u,int v,int w){ 21 cnt++; 22 Next[cnt]=Head[u]; 23 V[cnt]=v; 24 W[cnt]=w; 25 Head[u]=cnt; 26 cnt++; 27 Next[cnt]=Head[v]; 28 V[cnt]=u; 29 W[cnt]=0; 30 Head[v]=cnt; 31 } 32 33 bool bfs(){ 34 queue <int> Q; 35 while(!Q.empty()) Q.pop(); 36 memset(Depth,0,sizeof(Depth)); 37 Depth[s]=1; 38 Q.push(s); 39 while(!Q.empty()){ 40 int u=Q.front();Q.pop(); 41 for(int i=Head[u];i!=-1;i=Next[i]){ 42 if((W[i]>0)&&(Depth[V[i]]==0)){ 43 Depth[V[i]]=Depth[u]+1; 44 Q.push(V[i]); 45 } 46 } 47 } 48 if(Depth[t]==0) return 0; 49 return 1; 50 } 51 52 int dfs(int u,int dist){ 53 if(u==t) return dist; 54 for(int& i=cur[u];i!=-1;i=Next[i]){ 55 if((Depth[V[i]]==Depth[u]+1)&&W[i]!=0){ 56 int di=dfs(V[i],min(dist,W[i])); 57 if(di>0){ 58 W[i]-=di; 59 W[i^1]+=di; 60 return di; 61 } 62 } 63 } 64 return 0; 65 } 66 67 int Dinic(){ 68 int Ans=0; 69 while(bfs()){ 70 for(int i=0;i<=n+1;i++) cur[i]=Head[i]; 71 while(int d=dfs(s,INF)) Ans+=d; 72 } 73 return Ans; 74 } 75 76 int main(){ 77 int np,nc; 78 while(cin>>n>>np>>nc>>m){ 79 int a,b,c; 80 char zf; 81 init(); 82 s=n;t=n+1; 83 for(int i=1;i<=m;i++){ 84 cin>>zf>>a>>zf>>b>>zf>>c; 85 add_edge(a,b,c); 86 } 87 for(int i=1;i<=np;i++){ 88 cin>>zf>>b>>zf>>c; 89 add_edge(s,b,c); 90 } 91 for(int i=1;i<=nc;i++){ 92 cin>>zf>>a>>zf>>c; 93 add_edge(a,t,c); 94 } 95 cout<<Dinic()<<endl; 96 } 97 return 0; 98 }
POJ 1459 Power Network(多源点/汇点最大流问题)
标签:numbers nes ports eve ber power connect tput ext
原文地址:http://www.cnblogs.com/Leonard-/p/7822436.html