题目链接
http://poj.org/problem?id=1273
题目
基本最大流输入
题解
Edmonds-Karp算法
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int maxn = 500;
const int INF = 0x3f3f3f3f;
typedef struct node{
int from,to,cap,flow;
node(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){};
}Edge;
typedef struct Edmondskarp
{
vector<Edge> edges;
vector<int> G[maxn];
int path[maxn];
int alpha[maxn];
void init() {
for(int i=0;i<maxn;i++) G[i].clear();
edges.clear();
}
void AddEdge(int u,int v,int c)
{
edges.push_back(Edge(u,v,c,0));
edges.push_back(Edge(v,u,0,0));
int len = edges.size();
G[u].push_back(len-2);
G[v].push_back(len-1);
}
int MaxFlow(int s,int t)
{
int maxflow = 0;
while(true) {
queue<int> qu;
qu.push(s);
memset(alpha,0,sizeof(alpha));
alpha[s] = INF;
while(!qu.empty()&&!alpha[t])
{
int x = qu.front();qu.pop();
for(int i=0;i<G[x].size();i++)
{
Edge &e = edges[G[x][i]];
if(!alpha[e.to] && e.cap > e.flow) {
alpha[e.to] = min(alpha[e.from],e.cap-e.flow);
path[e.to] = G[x][i];
qu.push(e.to);
}
}
}
if(!alpha[t]) break;
for(int u=t;u!=s;u=edges[path[u]].from)
{
edges[path[u]].flow+=alpha[t];
edges[path[u]^1].flow -= alpha[t];
}
maxflow += alpha[t];
}
return maxflow;
}
}EK;
int main()
{
int n,m;
while(~scanf("%d%d",&m,&n))
{
EK solve;
solve.init();
for(int i=0;i<m;i++)
{
int u,v,c;
scanf("%d%d%d",&u,&v,&c);
solve.AddEdge(u,v,c);
}
printf("%d\n",solve.MaxFlow(1,n));
}
return 0;
}