Drainage Ditches
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10854 Accepted Submission(s): 5121
Problem 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
一个星期没A题,,练练手。
刚参加完蓝桥杯,感觉自己的实战经验和能力还有待提升。
我也是刚学最大流,看的这个人的博客 http://www.cnblogs.com/zsboy/archive/2013/01/27/2878810.html
看了一下午,,稍微有点懂了。
最大流的实现方法貌似有很多种。。。头疼啊
EK代码:
与君共勉
#include <iostream>
#include <vector>
#include <cstring>
#include <queue>
#define SIZE 250
#define INF 1000000000
using namespace std ;
int capacity[SIZE][SIZE] , pre[SIZE] , flow[SIZE];
int n , m ;
int min(int a , int b)
{
return a>b?b:a ;
}
int bfs(int src , int des)
{
queue<int> que ;
memset(pre,-1,sizeof(pre)) ;
memset(flow,0,sizeof(flow)) ;
pre[src] = 0 ;
flow[src] = INF ;
que.push(src) ;
while(!que.empty())
{
int index = que.front() ;
que.pop() ;
if(index == des)
{
break ;
}
for(int i = 1 ; i <= m ; ++i)
{
if(i!=src && capacity[index][i]>0 && pre[i] == -1)
{
pre[i] = index ;
flow[i] = min(capacity[index][i],flow[index]) ;
que.push(i) ;
}
}
}
if(pre[des] == -1)
return -1 ;
else
return flow[des] ;
}
int maxFlow(int src , int des)
{
int increasement = 0 ;
int sumFlow = 0 ;
while( (increasement=bfs(src,des)) != -1)
{
int k = des ;
while(k!=src)
{
int last = pre[k] ;
capacity[last][k] -= increasement ;
capacity[k][last] += increasement ;
k = last ;
}
sumFlow += increasement ;
}
return sumFlow ;
}
int main()
{
while(cin>>n>>m)
{
int start , end , c ;
memset(capacity,0,sizeof(capacity)) ;
for(int i = 0 ; i < n ; ++i)
{
cin>>start>>end>>c ;
capacity[start][end] += c ;
}
cout<<maxFlow(1,m)<<endl ;
}
return 0 ;
}
Dinic代码:
#include <stdio.h>
#include <string.h>
#define MAX 250
#define INF 1000000000
int graph[MAX][MAX] , dis[MAX] ;
int q[MAX*10] , h , r ;
int n , m ;
bool bfs()
{
memset(dis,-1,sizeof(dis)) ;
dis[1] = 0 ;
h = 0 , r = 1 ;
q[1] = 1 ;
int j ;
while(h<r)
{
j = q[++h] ;
for(int i = 1 ; i <= n ; ++i)
{
if(dis[i]<0 && graph[j][i]>0)
{
dis[i] = dis[j]+1 ;
q[++r] = i ;
}
}
}
if(dis[n]>0)
return true ;
return false ;
}
int find(int x , int low)
{
int deta = 0 ;
if(x == n)
return low ;
for(int i = 1 ; i <= n ; ++i)
{
if(graph[x][i]>0 && dis[i]==dis[x]+1 && (deta=find(i,low>graph[x][i]?graph[x][i]:low)))
{
graph[x][i] -= deta ;
graph[i][x] += deta ;
return deta ;
}
}
return 0 ;
}
int main()
{
while(~scanf("%d%d",&m,&n))
{
memset(graph,0,sizeof(graph));
for(int i = 0 ; i < m ; ++i)
{
int x , y , w;
scanf("%d%d%d",&x,&y,&w) ;
graph[x][y] += w ;
}
int ans = 0 ;
while(bfs())
{
int deta = 0 ;
while((deta=find(1,INF)))
{
ans += deta ;
}
}
printf("%d\n",ans) ;
}
return 0 ;
}
与君共勉