poj1459 Power Network *

/*
* 最大流 Edmonds-Karp
*
* 加入两个节点 S 和 T
* S 与 所有 power stations相连 ; 所有 consumers 与 T 相连
*
*/

#include
<iostream>
#include
<cstring>
using namespace std;

const int inf = 100000000;
const int maxN = 100 + 5;
int n, np, nc, m, l[maxN][maxN], s, t;
int q[maxN], maxFlow, pre[maxN];

void addFlow(){
int minL = inf, cur = t;
while(cur != s){
if(l[pre[cur]][cur] < minL)
minL
= l[pre[cur]][cur];
cur
= pre[cur];
}

cur
= t;
while(cur != s){
l[pre[cur]][cur]
-= minL;
l[cur][pre[cur]]
+= minL;
cur
= pre[cur];
}

maxFlow
+= minL;
}


void fulk(){
while(true){
memset(pre,
-1, sizeof(pre));
int head = 0, tail = 0, cur;
q[tail
++] = s;

while(head != tail){
cur
= q[head++];
for(int j=0; j<=n+1; j++){
if(l[cur][j] != 0 && pre[j] == -1){
pre[j]
= cur;
q[tail
++] = j;
}
}
if(pre[t] != -1) break;
}
if(pre[t] != -1) addFlow();
else break;
}
}

int main(){
while(cin >> n){
cin
>> np >> nc >> m;

memset(l,
0, sizeof(l));
s
= 0, t = n + 1, maxFlow = 0;
char tmp;
int u, v, z;
for(int i=1; i<=m; i++){
cin
>> tmp >> u >> tmp >> v >> tmp >> z;
l[u
+1][v+1] = z;
}
for(int i=1; i<=np; i++){
cin
>> tmp >> u >> tmp >> z;
l[s][u
+1] = z;
}
for(int i=1; i<=nc; i++){
cin
>> tmp >> u >> tmp >> z;
l[u
+1][t] = z;
}


fulk();

cout
<< maxFlow << endl;




}


return 0;
}
原文地址:https://www.cnblogs.com/longdouhzt/p/2166203.html