您的位置:首页 > 其它

hdu 1532 最大流入门题

2016-03-15 22:52 363 查看
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1532

题意:

农田在1点,河流在n点,中间有一些通路,问从1到n的最大流量

分析:

最大流的入门题

#include<iostream>
#include <algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
const int INF=100000000;
const int N=209;
struct edge
{
int from,to,cap,flow; //两个端点,最大容量和流量
edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};
int n;
vector<edge> e; //存放所有的边
vector<int>g
; //记录每个点对应的边集
int a
,p
; //分别记录路径上的最小残量和路径
void init()
{
for(int i=0;i<=n;i++)g[i].clear();
e.clear();
}
void addedge(int from,int to,int cap)
{
e.push_back(edge(from,to,cap,0));
e.push_back(edge(to,from,0,0));
int m=e.size();
g[from].push_back(m-2);
g[to].push_back(m-1);
}
int maxflow(int s,int t)
{
int flow=0;
while(1){
memset(a,0,sizeof(a));
queue<int>q;
q.push(s);
a[s]=INF;
while(!q.empty()){
int x=q.front();q.pop();
for(int i=0;i<g[x].size();i++){
edge& t=e[g[x][i]];
if(!a[t.to]&&t.cap>t.flow){
p[t.to]=g[x][i];
a[t.to]=min(a[x],t.cap-t.flow);
q.push(t.to);
}
}
if(a[t])break;
}
if(!a[t])break;
for(int u=t;u!=s;u=e[p[u]].from){
e[p[u]].flow+=a[t];
e[p[u]^1].flow-=a[t];
}
flow+=a[t];
}
return flow;
}
int main()
{
int m;
while(~scanf("%d%d",&m,&n)){
init();
int u,v,w;
for(int i=0;i<m;i++){
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w);
}
printf("%d\n",maxflow(1,n));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: