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;
    }
    

      

      

  • 相关阅读:
    uml系列(四)——类图
    Linux设备驱动实现自己主动创建设备节点
    收集了三年的最好的设计站点
    CoInitialize浅析一
    iOS 单例
    Android开发之异步具体解释(二)之AsyncTask
    ComboBox控件
    下拉框Html.DropDownList 和DropDownListFor 的经常用法
    好记心不如烂笔头,ssh登录 The authenticity of host 192.168.0.xxx can&#39;t be established. 的问题
    cidaemon.exe进程cpu占用率高及关闭cidaemon.exe进程方法
  • 原文地址:https://www.cnblogs.com/a972290869/p/4249227.html
Copyright © 2011-2022 走看看