(网络流Dinic) poj 1273

Drainage Ditches

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 58607   Accepted: 22508

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
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std;
#define INF 0x7fffffff
queue<int> q;
int tab[250][250];
int dis[250];
int N,M,ANS;
int BFS()
{
     memset(dis,-1,sizeof(dis));
     dis[1]=0;
     q.push(1);
     while (!q.empty())
     {
           int x=q.front();
           q.pop();
           for (int i=1;i<=N;i++)
               if (dis[i]<0&&tab[x][i]>0)
               {
                  dis[i]=dis[x]+1;
                  q.push(i);
               }
     }
     if(dis[N]>0)
        return 1;
     else
        return 0;
}
int find(int x,int low)
{
    int a=0;
    if (x==N)return low;
    for (int i=1;i<=N;i++)
    if (tab[x][i]>0&&dis[i]==dis[x]+1&&(a=find(i,min(low,tab[x][i]))))
    {
       tab[x][i]-=a;
       tab[i][x]+=a;
       return a;
    }
    return 0;
}
int main()
{
    int f,t,flow,tans;
    while(scanf("%d%d",&M,&N)!=EOF)
    {
            memset(tab,0,sizeof(tab));
            for(int i=1;i<=M;i++)
            {
                  scanf("%d%d%d",&f,&t,&flow);
                  tab[f][t]+=flow;
            }
            ANS=0;
            while(BFS())
            {
                  while(tans=find(1,INF))ANS+=tans;
            }
            printf("%d\n",ANS);
    }
    return 0;
}

  

  

时间: 2025-01-01 21:12:53

(网络流Dinic) poj 1273的相关文章

POJ 1273 Drainage Ditches(网络流dinic算法模板)

POJ 1273给出M条边,N个点,求源点1到汇点N的最大流量. 本文主要就是附上dinic的模板,供以后参考. #include <iostream> #include <stdio.h> #include <algorithm> #include <queue> #include <string.h> /* POJ 1273 dinic算法模板 边是有向的,而且存在重边,且这里重边不是取MAX,而是累加和 */ using namespace

POJ 1273 Drainage Ditches (网络流Dinic模板)

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

POJ 1273 最大流 Dinic

Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 56802 Accepted: 21824 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 wate

POJ 1273 Drainage Ditches(网络流 最大流)

Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 55893   Accepted: 21449 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

POJ 1273 Drainage Ditches 网络流基础

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

poj 1149 PIGS(网络流dinic)

PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16054   Accepted: 7185 Description Mirko works on a pig farm that consists of M locked pig-houses and Mirko can't unlock any pighouse because he doesn't have the keys. Customers come t

POJ 1273 Drainage Ditches(初识网络流)

开始研究网络流了,看了两个晚上吧,今天总算动手实践一下,有了更深的理解 总结一下:在最大流中,容量与实际流量满足3点: 1.实际流量<=容量 2.任意两点之间   : 流量(a->b)==流量(b->a) 3.流量守恒原则   :从s流出的流量 == t流入的流量 一.为什么叫增广路,因为在所有的流量网络中,会存在一个残量,所以在整个残量网络中,找到一个最小值,加到所有的流量线路里,便叫增广. 二.为什么要修改反向流量,因为在更新流量网时,当前选择的并不一定就是最优解,比如u->v

[网络流dinic]日常翻新

之前的排版简直辣眼睛,重写一遍好了 模板题是草地排水poj1273 网络流的基础思想就是瞎基本搜 但是搜要搜得有技巧,有特色 最简单的搜,无限深搜直到终点 稍微改进一下,宽搜先标号然后按层搜 再改进一下,把某些确定不再使用的点剔除 要点在于建立反向边给自己一个反悔的机会,用^1找到反向边 #include <stdio.h> #include <queue> #include <algorithm> #include <string.h> using nam

POJ 1273 Drainage Ditches (网络最大流)

http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 55235   Accepted: 21104 Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means

poj 1273 Drainage Ditches (最大流入门)

1 /****************************************************************** 2 题目: Drainage Ditches(POJ 1273) 3 链接: http://poj.org/problem?id=1273 4 题意: 现在有m个池塘(从1到m开始编号,1为源点,m为汇点),及n条 5 水渠,给出这n条水渠所连接的池塘和所能流过的水量,求水 6 渠中所能流过的水的最大容量.水流是单向的. 7 算法: 最大流之增广路(入门)