码迷,mamicode.com
首页 > 编程语言 > 详细

poj1273 网络流入门题 dinic算法解决,可作模板使用

时间:2015-08-02 13:06:47      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:

Drainage Ditches
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 62078   Accepted: 23845

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

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

Sample Output

50

Source

 
 
 
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
int edge[300][300];//邻接矩阵
int dis[300];//距源点距离,分层图
int start,end;
int m,n;//N:点数;M,边数
int bfs(){
   memset(dis,-1,sizeof(dis));//以-1填充
   dis[1]=0;
   queue<int>q;
   q.push(start);
   while(!q.empty()){
        int u=q.front();
        q.pop();
        for(int i=1;i<=n;i++){
            if(dis[i]<0&&edge[u][i]){
                dis[i]=dis[u]+1;
                q.push(i);

            }
        }
   }
   if(dis[n]>0)
    return 1;
   else
    return 0;//汇点的DIS小于零,表明BFS不到汇点
}
//Find代表一次增广,函数返回本次增广的流量,返回0表示无法增广
int find(int x,int low){//Low是源点到现在最窄的(剩余流量最小)的边的剩余流量
    int a=0;
    if(x==n)
        return low;//是汇点
    for(int i=1;i<=n;i++){
        if(edge[x][i]>0&&dis[i]==dis[x]+1&&//联通,,是分层图的下一层
           (a=find(i,min(low,edge[x][i])))){//能到汇点(a <> 0)
            edge[x][i]-=a;
            edge[i][x]+=a;
            return a;
           }

    }
    return 0;
}
int main(){
   while(scanf("%d%d",&m,&n)!=EOF){
       memset(edge,0,sizeof(edge));
       for(int i=1;i<=m;i++){
          int u,v,w;
          scanf("%d%d%d",&u,&v,&w);
          edge[u][v]+=w;
       }
       start=1;
       end=n;
       int ans=0;
       while(bfs()){//要不停地建立分层图,如果BFS不到汇点才结束
        ans+=find(1,0x7fffffff);//一次BFS要不停地找增广路,直到找不到为止
       }
       printf("%d\n",ans);
   }
   return 0;
}

 

 
 

poj1273 网络流入门题 dinic算法解决,可作模板使用

标签:

原文地址:http://www.cnblogs.com/13224ACMer/p/4695423.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!