题目:http://acm.hdu.edu.cn/showproblem.php?pid=1532
标准的网络流裸题
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <map>
#include <queue>
#include <vector>
#define PI 3.1415926
#define INF 1e18
#define inf 1e9
#define min(a,b) a<b?a:b
#define max(a,b) a>b?a:b
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define IOS ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std ;
typedef long long ll;
typedef unsigned long long ull;
const int _max = 205;
int c[_max][_max],f[_max],pre[_max];
int n,m;
queue<int> q;
int bfs(int src,int des){
while(!q.empty()) q.pop();
memset(pre,-1,sizeof(pre));
f[src] = inf;
q.push(src);
int now;
while(!q.empty()){
now = q.front();
q.pop();
if(now == des) break;
for(int next = 1 ; next <= m ; next++){
if(next == src || c[now][next] <= 0 || pre[next] != -1) continue;
pre[next] = now;
f[next] = min(f[now],c[now][next]);
q.push(next);
}
}
if(pre[des] == -1) return -1;
else return f[des];
}
int maxflow(int src,int des){
int cnt,ans = 0;
int front,now;
while((cnt = bfs(src,des)) != -1){
front = pre[des];
now = des;
while(front != -1){
c[front][now] -= cnt;
c[now][front] += cnt;
now = front;
front = pre[now];
}
ans += cnt;
}
return ans;
}
int main(){
IOS;
while(cin>>n>>m){
memset(c,0,sizeof(c));
int u,v,val;
for(int i = 1 ; i <= n ; i++){
cin>>u>>v>>val;
if(u == v) continue;
c[u][v]+=val;
}
cout<<maxflow(1,m)<<endl;
}
return 0;
}