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;
}
题意:
农田在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;
}
相关文章推荐
- 《剑指Offer:名企面试官精讲典型编程题》一书中IT名企经典面试题
- 113. Path Sum II LeetCode
- html5和css篇有关浮动以及如何清除浮动
- 文件操作一:基于C语言的文件操作
- CodeForces 625B - War of the Corporations
- root = Tk() 和 root = Tkinter.Tk() 区别
- 安卓第十四天笔记-内容提供者(ContentProvider)
- Failed to notify ProjectEvaluationListener.afterEvaluate(), but primary configuration failure takes
- SLIC Superpixels 算法学习笔记
- CodeForces 625A - Guest From the Past
- 第六章:JavaScript面向对象编程
- 计算1!+2!+.....+20!的和
- C++ 虚函数表解析
- 17. Letter Combinations of a Phone Number LeetCode
- Aizu - 0189 Convenient Location(floyd)
- Spring常用注解
- 217. Contains Duplicate
- liunx系统下 锐捷使用以及虚拟拨号教程
- 统计细胞数量
- 基于windows IIS的C语言CGI WEB服务器环境搭建