Power Network(最大流基础_增广路算法:多源多汇,自建总源点和总汇点)
Description
An example is in figure 1. The label x/y of power station u shows that p(u)=x and p max(u)=y. The label x/y of consumer u shows that c(u)=x and c max(u)=y. The label x/y of power transport line (u,v) shows that l(u,v)=x and l max(u,v)=y. The power consumed is Con=6. Notice that there are other possible states of the network but the value of Con cannot exceed 6.
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
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <algorithm> #include <iostream> #include <queue> using namespace std; #define inf 9999999999 int flow[210][210]; int maxflow[210],father[210],vis[210]; int max_flow; int m,i; void EK(int s,int e) { queue<int >q; int u,v; max_flow=0; while(1) { memset(maxflow,0,sizeof(maxflow)); memset(vis,0,sizeof(vis)); maxflow[s]=inf; q.push(s); while(!q.empty()) { u=q.front(); q.pop(); for(v=s;v<=e;v++) { if(!vis[v]&&flow[u][v]>0) { vis[v]=1; father[v]=u; q.push(v); maxflow[v]=min(maxflow[u],flow[u][v]); } } if(maxflow[e]>0) { while(!q.empty()) q.pop(); break; } } if(maxflow[e]==0) break; for(i=e;i!=s;i=father[i]) { flow[father[i]][i]-=maxflow[e]; flow[i][father[i]]+=maxflow[e]; } max_flow+=maxflow[e]; } } int main() { int n,np,nc,m; int i,a,b,c; char ch; while(~scanf("%d%d%d%d ",&n,&np,&nc,&m))//输入注意后面的空格 { memset(flow,0,sizeof(flow)); for(i=1;i<=m;i++) { scanf("(%d,%d)",&a,&b); scanf("%d ",&c);//注意后面的空格 flow[a+1][b+1]=c; } for(i=1;i<=np;i++)//建立总源点 { scanf("(%d)%d ",&a,&b);//注意后面空格 flow[0][a+1]=b; } for(i=1;i<=nc;i++)//建立总汇点 { scanf("(%d)%d ",&a,&b);//注意后面的空格 flow[a+1][n+1]=b; } EK(0,n+1); printf("%d\n",max_flow); } return 0; }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。