USACO 93, POJ 1273 排水沟(Drainage Ditches) 最大流
2017-11-28 10:21
337 查看
题目链接:https://cn.vjudge.net/problem/10499/origin
Drainage Ditches
Description
Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage ditches so that Bessie's
clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch.
Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network.
Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle.
Input
The input includes several cases. For each case, the first line contains two space-separated integers, N (0 <= N <= 200) and M (2 <= M <= 200). N is the number of ditches that Farmer John has dug. M is the number of intersections points
for those ditches. Intersection 1 is the pond. Intersection point M is the stream. Each of the following N lines contains three integers, Si, Ei, and Ci. Si and Ei (1 <= Si, Ei <= M) designate the intersections between which this ditch flows. Water will flow
through this ditch from Si to Ei. Ci (0 <= Ci <= 10,000,000) is the maximum rate at which water will flow through the ditch.
Output
For each case, output a single integer, the maximum rate at which water may emptied from the pond.
Sample Input
Sample Output
网络最大流模板题,题意即题解
AC代码
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#define INF 0x3f3f3f3f
using namespace std;
const int maxe=220;
const int maxv=220;
int M,N;
struct edge
{
int to,w,next;
}e[maxe<<1];
int head[maxv<<1],cnt;
int depth[maxv];
void init()
{
memset(head,-1,sizeof(head));
cnt=-1;
}
void add_edge(int u,int v,int w)
{
e[++cnt].to=v;
e[cnt].w=w;
e[cnt].next=head[u];
head[u]=cnt;
}
void _add(int u,int v,int w)
{
add_edge(u,v,w);
add_edge(v,u,0);
}
bool bfs()
{
memset(depth,0,sizeof(depth));
queue<int> que;
while(!que.empty()) que.pop();
depth[1]=1;
que.push(1);
while(!que.empty())
{
int u=que.front();que.pop();
for(int i=head[u];i!=-1;i=e[i].next)
{
if(!depth[e[i].to] && e[i].w>0)
{
depth[e[i].to]=depth[u]+1;
que.push(e[i].to);
}
}
}
if(!depth
) return false;
return true;
}
int dfs(int u,int flow)
{
if(u==N) return flow;
int ret=0;
for(int i=head[u];i!=-1 && flow;i=e[i].next)
{
if(depth[u]+1==depth[e[i].to] && e[i].w>0)
{
int tmp=dfs(e[i].to,min(flow,e[i].w));
if(tmp>0)
{
flow-=tmp;
ret+=tmp;
e[i].w-=tmp;
e[i^1].w+=tmp;
}
}
}
return ret;
}
void Dinic()
{
int ans=0;
while(bfs())
ans+=dfs(1,INF);
cout<<ans<<endl;
}
int main()
{
ios::sync_with_stdio(false);
while(cin>>M>>N)
{
init();
for(int i=1;i<=M;i++)
{
int u,v,w;
cin>>u>>v>>w;
_add(u,v,w);
}
Dinic();
}
return 0;
}
Drainage Ditches
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 79129 | Accepted: 30844 |
Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage ditches so that Bessie's
clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch.
Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network.
Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle.
Input
The input includes several cases. For each case, the first line contains two space-separated integers, N (0 <= N <= 200) and M (2 <= M <= 200). N is the number of ditches that Farmer John has dug. M is the number of intersections points
for those ditches. Intersection 1 is the pond. Intersection point M is the stream. Each of the following N lines contains three integers, Si, Ei, and Ci. Si and Ei (1 <= Si, Ei <= M) designate the intersections between which this ditch flows. Water will flow
through this ditch from Si to Ei. Ci (0 <= Ci <= 10,000,000) is the maximum rate at which water will flow through the ditch.
Output
For each case, output a single integer, the maximum rate at which water may emptied from the pond.
Sample Input
5 4 1 2 40 1 4 20 2 4 20 2 3 30 3 4 10
Sample Output
50
网络最大流模板题,题意即题解
AC代码
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#define INF 0x3f3f3f3f
using namespace std;
const int maxe=220;
const int maxv=220;
int M,N;
struct edge
{
int to,w,next;
}e[maxe<<1];
int head[maxv<<1],cnt;
int depth[maxv];
void init()
{
memset(head,-1,sizeof(head));
cnt=-1;
}
void add_edge(int u,int v,int w)
{
e[++cnt].to=v;
e[cnt].w=w;
e[cnt].next=head[u];
head[u]=cnt;
}
void _add(int u,int v,int w)
{
add_edge(u,v,w);
add_edge(v,u,0);
}
bool bfs()
{
memset(depth,0,sizeof(depth));
queue<int> que;
while(!que.empty()) que.pop();
depth[1]=1;
que.push(1);
while(!que.empty())
{
int u=que.front();que.pop();
for(int i=head[u];i!=-1;i=e[i].next)
{
if(!depth[e[i].to] && e[i].w>0)
{
depth[e[i].to]=depth[u]+1;
que.push(e[i].to);
}
}
}
if(!depth
) return false;
return true;
}
int dfs(int u,int flow)
{
if(u==N) return flow;
int ret=0;
for(int i=head[u];i!=-1 && flow;i=e[i].next)
{
if(depth[u]+1==depth[e[i].to] && e[i].w>0)
{
int tmp=dfs(e[i].to,min(flow,e[i].w));
if(tmp>0)
{
flow-=tmp;
ret+=tmp;
e[i].w-=tmp;
e[i^1].w+=tmp;
}
}
}
return ret;
}
void Dinic()
{
int ans=0;
while(bfs())
ans+=dfs(1,INF);
cout<<ans<<endl;
}
int main()
{
ios::sync_with_stdio(false);
while(cin>>M>>N)
{
init();
for(int i=1;i<=M;i++)
{
int u,v,w;
cin>>u>>v>>w;
_add(u,v,w);
}
Dinic();
}
return 0;
}
相关文章推荐
- POJ_1273_Drainage Ditches(USACO 93)_最大流
- POJ 1273 USACO 93 Drainage Ditches 最大流
- Drainage Ditches poj 1273 最大流
- poj 1273 Drainage Ditches(最大流的EK算法模板)
- poj 1273 Drainage Ditches--最大流--sap
- poj1273最大流
- poj 1273 Drainage Ditches(最大流,E-K算法)
- POJ-1273-Drainage Ditches(裸的最大流)
- POJ 1273 Drainage Ditches 最大流
- poj 1273 EK 最大流
- 【最大流】北大 poj 1273 Drainage Ditches
- POJ - 1273 Drainage Ditches 最大流 模板题
- poj 1273 Drainage Ditches---EK求最大流
- poj 1273 Drainage Ditches(最大流,E-K算法)
- poj1273 Drainage Ditches 最大流 dinic算法
- POJ-1273 Drainage Ditches 最大流裸题
- POJ-1273(最大流-Augment Path,EK,BFS)
- POJ 1273-Drainage Ditches(最大流-Edmond-Karp算法/模板)
- 【代码系列】POJ 1273( 单纯形解最大流 )
- poj 1273 (nyoj 323) Drainage Ditches : 最大流