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

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

USACO 93

#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;
}
时间: 2024-07-31 11:37:56

poj1273 网络流入门题 dinic算法解决,可作模板使用的相关文章

网络流最大流——dinic算法

前言 网络流问题是一个很深奥的问题,对应也有许多很优秀的算法.但是本文只会讲述dinic算法 最近写了好多网络流的题目,想想看还是写一篇来总结一下网络流和dinic算法以免以后自己忘了... 网络流问题简述 一个很普遍的例子就是--你家和自来水厂之间有许多中转站,中转站又由一些水管连接着.我们假设自来水厂的供水是无限的,并且中转站内能存储的水量也是无限的,但是管道有宽又窄,很显然管道内的流量必须小于等于管道的承载范围(否则管道就被撑爆了),那么问题就是要你求出你家最多能收到多大流量的水. emm

dinic 算法 基本思想及其模板

“网络流博大精深”—sideman语 一个基本的网络流问题 感谢WHD的大力支持 最早知道网络流的内容便是最大流问题,最大流问题很好理解: 解释一定要通俗! 如右图所示,有一个管道系统,节点{1,2,3,4},有向管道{A,B,C,D,E},即有向图一张. [1]是源点,有无限的水量,[4]是汇点,管道容量如图所示.试问[4]点最大可接收的水的流量? 这便是简单的最大流问题,显然[4]点的最大流量为50 死理性派请注意:流量是单位时间内的,总可以了吧! 然而对于复杂图的最大流方法是什么呢,有E

[转] 一些图论、网络流入门题总结、汇总

最短路问题此类问题类型不多,变形较少 POJ 2449 Remmarguts' Date(中等)http://acm.pku.edu.cn/JudgeOnline/problem?id=2449题意:经典问题:K短路解法:dijkstra+A*(rec),方法很多相关:http://acm.pku.edu.cn/JudgeOnline/showcontest?contest_id=1144该题亦放在搜索推荐题中 POJ 3013 - Big Christmas Tree(基础)http://ac

【转】一些图论、网络流入门题总结、汇总

最短路问题 此类问题类型不多,变形较少 POJ 2449 Remmarguts' Date(中等) http://acm.pku.edu.cn/JudgeOnline/problem?id=2449 题意:经典问题:K短路 解法:dijkstra+A*(rec),方法很多 相关:http://acm.pku.edu.cn/JudgeOnline/showcontest?contest_id=1144 该题亦放在搜索推荐题中 POJ 3013 - Big Christmas Tree(基础) ht

[转载] 一些图论、网络流入门题总结、汇总

转载 最短路问题此类问题类型不多,变形较少 POJ 2449 Remmarguts' Date(中等)http://acm.pku.edu.cn/JudgeOnline/problem?id=2449题意:经典问题:K短路解法:dijkstra+A*(rec),方法很多相关:http://acm.pku.edu.cn/JudgeOnline/showcontest?contest_id=1144该题亦放在搜索推荐题中 POJ 3013 - Big Christmas Tree(基础)http:/

hdu 3549 最大流入门题

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 [科普]什么是BestCoder?如何参加? Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 8862    Accepted Submission(s): 4168 Problem Description

网络流最经典的入门题 各种网络算法都能AC。

Drainage Ditches 题目抽象:给你m条边u,v,c.   n个定点,源点1,汇点n.求最大流.  最好的入门题,各种算法都可以拿来练习 (1):  一般增广路算法  ford() 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <string&g

POJ1273:Drainage Ditches(最大流入门 EK,dinic算法)

http://poj.org/problem?id=1273 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 Jo

hiho一下 第119周 #1398 : 网络流五&#183;最大权闭合子图 【最小割-最大流--Ford-Fulkerson 与 Dinic 算法】

#1398 : 网络流五·最大权闭合子图 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 周末,小Hi和小Ho所在的班级决定举行一些班级建设活动. 根据周内的调查结果,小Hi和小Ho一共列出了N项不同的活动(编号1..N),第i项活动能够产生a[i]的活跃值. 班级一共有M名学生(编号1..M),邀请编号为i的同学来参加班级建设活动需要消耗b[i]的活跃值. 每项活动都需要某些学生在场才能够进行,若其中有任意一个学生没有被邀请,这项活动就没有办法进行. 班级建设的活