Drainage Ditches
题目链接:POJ - 1273题意:给出有向图, 求由源点到汇点的最大流量;
网络流最大流的模板题;
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
const long long INF = 0x3f3f3f3f;
const int maxn=50010;
int N, M;
struct node{
int v, nex, w;
}edge[500];
int head[300];
int cnt;
int G[300][300];
int flow[300];
queue<int> que;
int vis[300];
int pre[300];
int bfs(int u, int v){
while(!que.empty()) que.pop();
flow[u]=INF;
memset(vis, 0, sizeof(vis));
memset(pre, -1, sizeof(pre));
que.push(u);
vis[u]=1;
while(!que.empty()){
int temp=que.front();
que.pop();
if(temp==v){
return flow[v];
}
for(int i=1; i<=M; i++){
if(!vis[i]&&G[temp][i]>0){
pre[i]=temp;
flow[i]=min(flow[temp], G[temp][i]);
vis[i]=1;
que.push(i);
}
}
}
return -1;
}
void maxflow(){
int minf=0;
int sumflow=0;
while(1){
minf=bfs(1, M);
if(minf==-1) break;
for(int i=M; i!=-1; i=pre[i]){
G[pre[i]][i]-=minf;
G[i][pre[i]]+=minf;
}
sumflow+=minf;
}
printf("%d\n", sumflow);
}
int main(){
while(~scanf("%d%d", &N, &M)){
int u, v, w;
memset(head, -1, sizeof(head));
cnt=0;
memset(G, 0, sizeof(G));
for(int i=0; i<N; i++){
scanf("%d%d%d", &u, &v, &w);
G[u][v]+=w;
}
maxflow();
}
return 0;
}