Drainage Ditches
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 77379 | Accepted: 30136 |
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.
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
直接用模板,重要在于对算法的消化。
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
#define maxv 205
#define INF 0x6fffffff
int N,M;
struct edge
{
int to; //流向
int cap; //容量
int rev; //反向边在G中的标号
};
vector<edge>G[maxv];
bool used[maxv];
//加一条边及其反向边
void add_edge(int from,int to,int cap)
{
edge u;
u.to=to;
u.cap=cap;
u.rev=G[to].size();
G[from].push_back(u);
u.to=from;
u.cap=0;
u.rev=G[from].size()-1;
G[to].push_back(u);
}
//通过dfs寻找到增广路 v代表现在的点,t代表终点,f代表目前为止当前整条增广路上的最大流量
int dfs(int v,int t,int f)
{
if(v==t)return f; //到达终点则返回流量
used[v]=true; //已经经过这个顶点
for(int i=0;i<G[v].size();i++)
{
edge&e=G[v][i]; //不是单纯地复制,而是引用,对e的改变,也会改变Gvi
if(!used[e.to]&&e.cap>0) //未经过这个点而且朝这边还可以流
{
int d=dfs(e.to,t,min(f,e.cap)); //继续搜索
if(d>0) //流量大于0
{
e.cap-=d; //正向边容量减d
G[e.to][e.rev].cap+=d; //反向边容量加d
return d;
}
}
}
return 0;
}
int max_flow(int s,int t)
{
int flow=0; //代表总的最大流
for(;;)
{
memset(used,0,sizeof(used));
int f=dfs(s,t,INF); //f为找到的一条增广路上的流量
if(f==0)return flow; //不再有流量增加就返回
flow+=f;
}
}
int main()
{
while(cin>>N>>M)
{
for(int i=0;i<maxv;i++) //这是对G[maxv]的清空
{
while(G[i].size()!=0)
{
// delete G[i][G[i].size()-1]; //加上这条无法编译
G[i].pop_back();
}
}
int s,e,c;
for(int i=0;i<N;i++)
{
cin>>s>>e>>c;
add_edge(s,e,c);
}
cout<<max_flow(1,M)<<endl;
// G[maxv].clear(); //这个则会runtime error
}
}