HDU-1532-网络流最大流
2016-12-02 14:17
483 查看
题目大意:求两点之间的最大流;
题目解析:思想就是迭代dfs找增广路径,直到找不到位置,注意要更新残余网络;
AC代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
using namespace std;
const int inf=0x3fffffff;
const int maxn=1010;
struct node
{
int to,cap,rev;
node(int a,int b,int c)
{
to=a;
cap=b;
rev=c;
}
};
vector<node>vec[maxn];
bool used[maxn];
void add_edge(int from,int to,int cost)
{
vec[from].push_back(node(to,cost,vec[to].size()));
vec[to].push_back(node(from,0,vec[from].size()-1));
}
int dfs(int u,int v,int cost)
{
if(u==v) return cost;
used[u]=true;
int i;
for(i=0;i<vec[u].size();i++)
{
node &temp=vec[u][i];
if(!used[temp.to]&&temp.cap>0)
{
int d=dfs(temp.to,v,min(cost,temp.cap));
if(d>0)
{
temp.cap-=d;
vec[temp.to][temp.rev].cap+=d;
return d;
}
}
}
return 0;
}
int max_flow(int u,int v)
{
int flow=0;
while(1)
{
memset(used,0,sizeof(used));
int res=dfs(u,v,inf);
if(res==0) return flow;
flow+=res;
}
}
int main()
{
int n,m,i;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=0;i<maxn;i++)
{
vec[i].clear();
}
for(i=0;i<n;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add_edge(a,b,c);
}
printf("%d\n",max_flow(1,m));
}
return 0;
}
题目解析:思想就是迭代dfs找增广路径,直到找不到位置,注意要更新残余网络;
AC代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
using namespace std;
const int inf=0x3fffffff;
const int maxn=1010;
struct node
{
int to,cap,rev;
node(int a,int b,int c)
{
to=a;
cap=b;
rev=c;
}
};
vector<node>vec[maxn];
bool used[maxn];
void add_edge(int from,int to,int cost)
{
vec[from].push_back(node(to,cost,vec[to].size()));
vec[to].push_back(node(from,0,vec[from].size()-1));
}
int dfs(int u,int v,int cost)
{
if(u==v) return cost;
used[u]=true;
int i;
for(i=0;i<vec[u].size();i++)
{
node &temp=vec[u][i];
if(!used[temp.to]&&temp.cap>0)
{
int d=dfs(temp.to,v,min(cost,temp.cap));
if(d>0)
{
temp.cap-=d;
vec[temp.to][temp.rev].cap+=d;
return d;
}
}
}
return 0;
}
int max_flow(int u,int v)
{
int flow=0;
while(1)
{
memset(used,0,sizeof(used));
int res=dfs(u,v,inf);
if(res==0) return flow;
flow+=res;
}
}
int main()
{
int n,m,i;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=0;i<maxn;i++)
{
vec[i].clear();
}
for(i=0;i<n;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add_edge(a,b,c);
}
printf("%d\n",max_flow(1,m));
}
return 0;
}
相关文章推荐
- HDU 1532 Drainage Ditches (最大网络流)
- 图论 网络流 HDU 1532 最大流
- poj 1273 hdu 1532 网络流最大流 Dinic算法
- hdu 1532 Drainage Ditches(最大网络流)
- hdu 1532 网络流-最大流 DINIC
- hdu1532 Drainage Ditches网络流——最大流 EK算法模板
- hdu 1532 Drainage Ditches(网络流之最大流EdmondsKarp)
- 【网络流】HDU 1532 Drainage Ditches(最大流Ford-Fulkersson)+HDU 3549 Flow Problem(最大流Dinic算法)
- HDU 1532 Drainage Ditches (最大网络流)
- HDU 1532 Drainage Ditches (最大网络流)
- HDU - 1532 Drainage Ditches解题报告(网络流 最大流魔板题)
- hdu 1532(poj 1273) Drainage Ditches (网络流·最大流)
- hdu1532最大流EK与SAP算法
- HDU 3657 Game 网络流--最大独立集
- Hdu 1569 方格取数(2) (网络流最大点权独立集)
- hdu1532最大流EK与SAP算法
- 【网络流第二步】Hdu 1532——Drainage Ditches 邻接矩阵版
- HDU 1532 Drainage Ditches(最大流)
- 独立最小【网络流第五弹】最大点权独立集 ——HDU 1569 方格取数(2)
- HDU 1532 Drainage Ditches 网络流