标签:
Description
Input
Output
Sample Input
5 4 1 2 40 1 4 20 2 4 20 2 3 30 3 4 10
Sample Output
50
题意:从1到m点有若干通道,通道流量有限制,求最大流量。
EK:
1 /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 2 //int main(int argc, char** argv) 3 4 #include<stdio.h> 5 #include<string.h> 6 #include<queue> 7 #include<algorithm> 8 9 using namespace std; 10 11 #define maxn 220 12 #define INF 0xfffffff 13 #define min(a, b)(a < b ? a : b) 14 15 int maps[maxn][maxn], pre[maxn]; 16 17 bool bfs(int s, int e) // 找路 18 { 19 memset(pre, 0, sizeof(pre)); 20 queue<int> Q; 21 Q.push(s); 22 23 while(Q.size()) 24 { 25 int i = Q.front(); 26 Q.pop(); 27 if(i == e) 28 return true; 29 30 for(int j = 1; j <= e; j++) 31 { 32 if(maps[i][j] && pre[j] == 0) // 此通道还有流量可以用,并且当前点在当前路上没有被用到 33 { 34 pre[j] = i; // 表示从i->j流向 35 Q.push(j); 36 } 37 } 38 } 39 return false; 40 } 41 42 int EK(int s, int e) 43 { 44 int i, Minflow; 45 int ans = 0; 46 47 while(bfs(s, e)) //找到一条路径 48 { 49 Minflow = INF; 50 for(i = e; i != s; i = pre[i]) 51 Minflow = min(Minflow, maps[pre[i]][i]); // 从后往前找,找当前路上最小的流量限制 52 for(i = e; i != s; i = pre[i]) 53 { 54 maps[pre[i]][i] -= Minflow; // 当前管道的流量减去已经用的就是剩下可以用的 55 maps[i][pre[i]] += Minflow; //反方向加~why 56 } 57 ans += Minflow; // 总流量相加 58 } 59 return ans; 60 } 61 int main() 62 { 63 int n, m, x, y, q; 64 65 while(scanf("%d%d", &n, &m) != EOF) 66 { 67 memset(maps, 0, sizeof(maps)); 68 69 while(n--) 70 { 71 scanf("%d%d%d", &x, &y, &q); 72 maps[x][y] += q; // 管道流量也是相加 73 } 74 printf("%d\n", EK(1, m)); // 75 } 76 return 0; 77 }
标签:
原文地址:http://www.cnblogs.com/Tinamei/p/4724988.html