http://poj.org/problem?id=1459
题意:有一个电路网络,每个节点可以产生、传递、消耗若干电量,有点线连接结点,每个电线有最大传输量,求这个网络的最大消费量。
思路:从源点到发电站连边,流量为发电量,从用户到汇点连边,流量为消费量,再根据电线连双向边,求最大流即可。
1 #include<stdio.h> 2 #include<string.h> 3 #include<queue> 4 const int N=220; 5 const int INF=1<<28; 6 using namespace std; 7 int map[N][N]; 8 int pre[N]; 9 int n,np,nc,m; 10 11 int bfs(int s,int d) 12 { 13 queue<int>q; 14 memset(pre,-1,sizeof(pre)); 15 pre[s] = 0; 16 int k; 17 q.push(s); 18 while(!q.empty()) 19 { 20 k = q.front(); 21 q.pop(); 22 for (int i = 0; i <= n+1; i ++) 23 { 24 if (pre[i]==-1 && map[k][i] > 0) 25 { 26 pre[i] = k; 27 if (i==d) 28 return 1; 29 q.push(i); 30 } 31 } 32 } 33 return 0; 34 } 35 int maxflow(int s,int d) 36 { 37 int maxf = 0; 38 while(bfs(s,d)) 39 { 40 int minf = INF; 41 for (int i = d; i!=s; i = pre[i]) 42 //minf = minf < map[pre[i]][i] ? minf:map[pre[i]][i]; 43 minf = min(minf,map[pre[i]][i]); 44 for (int i = d; i!=s; i = pre[i]) 45 { 46 map[pre[i]][i] -= minf; 47 map[i][pre[i]] += minf; 48 } 49 maxf += minf; 50 } 51 return maxf; 52 } 53 int main() 54 { 55 int s,d; 56 int u,v,w; 57 char str[32]; 58 while(~scanf("%d%d%d%d",&n,&np,&nc,&m)) 59 { 60 memset(map,0,sizeof(map)); 61 s = n; 62 d = n+1; 63 for (int i = 0; i < m; i ++) 64 { 65 scanf("%s",str); 66 sscanf(str,"(%d,%d)%d",&u,&v,&w); 67 map[u][v] = w; 68 69 } 70 for (int i = 0; i < np; i ++) 71 { 72 scanf("%s",str); 73 sscanf(str,"(%d)%d",&v,&w); 74 map[s][v] = w; 75 } 76 for (int i = 0; i < nc; i ++) 77 { 78 scanf("%s",str); 79 sscanf(str,"(%d)%d",&u,&w); 80 map[u][d] = w; 81 } 82 printf("%d ",maxflow(s,d)); 83 } 84 return 0; 85 }