标签:des style blog http java color os io
5 4 1 2 40 1 4 20 2 4 20 2 3 30 3 4 10
50
解题:哈哈 直接求最大流就是了!模板一刷,AC到手。。。。。。。。^_^
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <vector> 6 #include <climits> 7 #include <algorithm> 8 #include <cmath> 9 #include <queue> 10 #define LL long long 11 #define INF 0x3f3f3f3f 12 using namespace std; 13 const int maxn = 500; 14 int cap[maxn][maxn],flow[maxn][maxn],a[maxn],link[maxn]; 15 queue<int>q; 16 int main(){ 17 int n,m,i,j,u,v,w,ans; 18 while(~scanf("%d%d",&n,&m)){ 19 memset(cap,0,sizeof(cap)); 20 memset(flow,0,sizeof(flow)); 21 for(i = 0; i < n; i++){ 22 scanf("%d%d%d",&u,&v,&w); 23 cap[u][v] += w; 24 } 25 while(!q.empty()) q.pop(); 26 ans = 0; 27 while(true){ 28 memset(a,0,sizeof(a)); 29 a[1] = INF; 30 q.push(1); 31 while(!q.empty()){ 32 u = q.front(); 33 q.pop(); 34 for(v = 1; v <= m; v++){ 35 if(!a[v] && cap[u][v] > flow[u][v]){ 36 link[v] = u; 37 q.push(v); 38 a[v] = min(a[u],cap[u][v]-flow[u][v]); 39 } 40 } 41 } 42 if(a[m] == 0) break; 43 for(u = m; u != 1; u = link[u]){ 44 flow[link[u]][u] += a[m]; 45 flow[u][link[u]] -= a[m]; 46 } 47 ans += a[m]; 48 } 49 printf("%d\n",ans); 50 } 51 return 0; 52 }
XTU 二分图和网络流 练习题 J. Drainage Ditches,布布扣,bubuko.com
XTU 二分图和网络流 练习题 J. Drainage Ditches
标签:des style blog http java color os io
原文地址:http://www.cnblogs.com/crackpotisback/p/3868384.html