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

nyoj 323 Drainage Ditches(dinic模板)

2015-09-11 11:26 639 查看


Drainage Ditches

时间限制:1000 ms | 内存限制:65535 KB
难度:4

描述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.

输入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.
输出For each case, output a single integer, the maximum rate at which water may emptied from the pond.
样例输入
5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10


样例输出
50


来源USACO 93
上传者
张洁烽
网络流裸题

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
#define M 500
#define inf 0x3f3f3f3f
struct node{
int v,w,next;
}mp[M*4];
int head[M],dis[M],n,m,cnt;
void add(int u,int v,int w){
mp[cnt].v=v;
mp[cnt].w=w;
mp[cnt].next=head[u];
head[u]=cnt++;
}
int bfs(){
memset(dis,-1,sizeof(dis));
queue<int> Q;
while(!Q.empty()) Q.pop();
dis[1]=0;
Q.push(1);
while(!Q.empty()){
int u=Q.front();
Q.pop();
for(int i=head[u];i!=-1;i=mp[i].next){
int v=mp[i].v;
if(dis[v]==-1 && mp[i].w>0){
dis[v]=dis[u]+1;
if(v==m) return 1;
Q.push(v);
}
}
}
return 0;
}
int dinic(int s,int flow){
if(s==m || flow==0) return flow;
int a,i,ans=flow;
for(i=head[s];i!=-1;i=mp[i].next){
int v=mp[i].v;
if(mp[i].w && dis[v]==dis[s]+1 && (a=dinic(v,min(ans,mp[i].w)))){
mp[i].w-=a;
mp[i^1].w+=a;
ans-=a;
if(ans==0) return flow;
}
}
return flow-ans;
}
int main(){
int sum,i,a,b,c;
while(scanf("%d%d",&n,&m)!=EOF){
sum=cnt=0;
memset(head,-1,sizeof(head));
for(i=0;i<n;i++){
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
add(b,a,0);
sum+=c;
}
int ans=0;
while(bfs()){
ans+=dinic(1,inf);
}
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: