zoukankan      html  css  js  c++  java
  • (网络流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
    ",ANS);
        }
        return 0;
    }
    

      

      

  • 相关阅读:
    qsort()的使用
    c语言不寻常的类型转换(类型提升)
    堆栈段的三个主要用途
    区分 声明与定义
    宏定义陷阱与typedef
    约瑟夫环解决方案
    线程中断测试
    Redis
    本地缓存
    tomcat优化
  • 原文地址:https://www.cnblogs.com/a972290869/p/4249227.html
Copyright © 2011-2022 走看看