您的位置:首页 > 大数据 > 人工智能

POJ 1273 Drainage Ditches

2014-02-21 20:59 309 查看
[b]Drainage Ditches[/b]

Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 52160Accepted: 19830
[b]Description[/b]

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.
[b]Input[/b]

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.
[b]Output[/b]

For each case, output a single integer, the maximum rate at which water may emptied from the pond.
[b]Sample Input[/b]

5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10

[b]Sample Output[/b]

50

[b]Source[/b]

USACO 93

最基本的网络流问题,今天学习了一下网络流,就先写了这道,直接套Ford-Fulkerson算法模板即可

#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
#include<queue>
#define INF 0x7fffffff
#define MAX_V 210

using namespace std;

struct edge
{
int to;
int cap;
int rev;
};

int m,n;
int iter[MAX_V];
int level[MAX_V];
vector<edge> G[MAX_V];

void add_edge(int _from,int _to,int _cap)
{
edge temp;
temp.to=_to;
temp.cap=_cap;
temp.rev=G[_to].size();
G[_from].push_back(temp);
temp.to=_from;
temp.cap=0;
temp.rev=G[_from].size()-1;
G[_to].push_back(temp);
}

void bfs(int s)
{
memset(level,-1,sizeof(level));
queue<int> que;
level[s]=0;
que.push(s);
while(!que.empty())
{
int v=que.front();
que.pop();
for(int i=0;i<G[v].size();i++)
{
edge &e=G[v][i];
if(e.cap>0&&level[e.to]<0)
{
level[e.to]=level[v]+1;
que.push(e.to);
}
}
}
}

int dfs(int v,int t,int f)
{
if(v==t)
return f;

for(int &i=iter[v];i<G[v].size();i++)
{
edge &e=G[v][i];
if(e.cap>0&&level[v]<level[e.to])
{
int d=dfs(e.to,t,min(f,e.cap));
if(d>0)
{
e.cap-=d;
G[e.to][e.rev].cap+=d;
return d;
}
}
}

return 0;
}

int max_flow(int s,int t)
{
int flow=0;

while(true)
{
bfs(s);
if(level[t]<0)
return flow;
memset(iter,0,sizeof(iter));
int f;
while((f=dfs(s,t,INF))>0)
flow+=f;
}
}

int main()
{
while(scanf("%d %d",&n,&m)==2)
{
for(int i=1;i<=m;i++)
G[i].clear();

int s,e,c;
for(int i=1;i<=n;i++)
{
scanf("%d %d %d",&s,&e,&c);
add_edge(s,e,c);
}
printf("%d\n",max_flow(1,m));
}

return 0;
}


[C++]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: