标签:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 59210 | Accepted: 22737 |
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
Source
1 #include<stdio.h> 2 #include<string.h> 3 #include<queue> 4 #include<algorithm> 5 using namespace std; 6 const int M = 210 , inf = 0x3f3f3f3f ; 7 int m , n ; 8 int map[M][M] ; 9 int dis[M]; 10 11 bool bfs () 12 { 13 queue <int> q ; 14 while (!q.empty ()) 15 q.pop () ; 16 memset (dis , 0xff , sizeof(dis)) ; 17 dis[1] = 0 ; 18 q.push (1) ; 19 while (!q.empty () ) { 20 int u = q.front () ; 21 q.pop () ; 22 for (int v = 1 ; v <= n ; v++) { 23 if (map[u][v] && dis[v] == -1) { 24 dis[v] = dis[u] + 1 ; 25 q.push (v) ; 26 } 27 } 28 } 29 if (dis[n] > 0) 30 return true ; 31 return false ; 32 } 33 34 int find (int u , int low) 35 { 36 int a = 0 ; 37 if (u == n) 38 return low ; 39 for (int v = 1 ; v <= n ; v++) { 40 if (map[u][v] && dis[v] == dis[u] + 1 && (a = find (v , min(map[u][v] , low)))) { 41 map[u][v] -= a ; 42 map[v][u] += a ; 43 return a ; 44 } 45 } 46 return 0 ; 47 } 48 49 int main () 50 { 51 // freopen ("a.txt" , "r" , stdin) ; 52 int u , v , w ; 53 int ans ; 54 while (~ scanf ("%d%d" , &m , &n)) { 55 memset (map , 0 , sizeof(map)) ; 56 // printf ("m = %d , n = %d\n" , m , n) ; 57 while (m--) { 58 scanf ("%d%d%d" , &u , &v , &w) ; 59 map[u][v] += w ; 60 } 61 int maxn = 0 ; 62 while (bfs()) { 63 if (ans = find(1 , inf)) 64 maxn += ans ; 65 } 66 printf ("%d\n" , maxn) ; 67 } 68 return 0 ; 69 }
标签:
原文地址:http://www.cnblogs.com/get-an-AC-everyday/p/4318160.html