zoukankan      html  css  js  c++  java
  • POJ1273 Drainage Ditches[最大流Dinic()]

    Drainage Ditches
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 42080   Accepted: 15783

    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

    code:

      1 #include<iostream>
      2 #include<queue>
      3 using namespace std;
      4 
      5 #define inf 0x6fffffff
      6 
      7 int n,m;
      8 int edgenum;
      9 
     10 int vst[220];
     11 int level[220];
     12 int dis[220];
     13 
     14 typedef struct 
     15 {
     16     int from,to,dis;
     17     int next;
     18 }Edge;
     19 Edge edge[200*100];
     20 int head[210];
     21 
     22 void addedge(int a,int b,int c)
     23 {
     24     edge[edgenum].from=a,edge[edgenum].to=b,edge[edgenum].dis=c,edge[edgenum].next=head[a],head[a]=edgenum++;
     25     edge[edgenum].from=b,edge[edgenum].to=a,edge[edgenum].dis=0,edge[edgenum].next=head[b],head[b]=edgenum++;
     26 }
     27 
     28 int dinic(int st,int et)                    //建立层次图
     29 {
     30     int que[220];int sta[220];                  //模拟队列
     31     int ans=0;
     32     while(true)
     33     {
     34         int front,tail;
     35         int u,v;
     36         memset(dis,-1,sizeof(dis));
     37         front=tail=0;
     38         dis[st]=0;
     39         que[tail++]=st;
     40         while(front<tail)
     41         {
     42             v=que[front++];
     43             for(int i=head[v];i!=-1;i=edge[i].next)
     44             {
     45                 u=edge[i].to;
     46                 if(dis[u]==-1&&edge[i].dis>0)
     47                 {
     48                     dis[u]=dis[v]+1;
     49                     que[tail++]=u;
     50                     if(u==et)
     51                     {
     52                         front=tail;
     53                         break;
     54                     }
     55                 }
     56             }
     57         }
     58         if(dis[et]==-1)          
     59             break;
     60         int t=0;               //模拟栈
     61         int s=st;
     62         while(true)
     63         {
     64             if(s!=et)                             //找增广路
     65             {
     66                 int i;
     67                 for(i=head[s];i!=-1;i=edge[i].next)
     68                     if(edge[i].dis>0&&dis[edge[i].to]==dis[edge[i].from]+1)
     69                         break;
     70                 if(i!=-1)
     71                 {
     72                     sta[t++]=i;
     73                     s=edge[i].to;
     74                 }
     75                 else
     76                 {
     77                     if(t==0)                    //说明已经没有增广路可以找了
     78                         break;
     79                     dis[edge[sta[--t]].to]=-1;
     80                     s=edge[sta[t]].from;
     81                 }
     82             }
     83             else
     84             {
     85                 int mindis=inf;
     86                 int tag;
     87                 for(int i=0;i<t;i++)
     88                     if(mindis>edge[sta[i]].dis)              //寻找最短的那条边
     89                     {
     90                         mindis=edge[sta[i]].dis;
     91                         tag=i;
     92                     }
     93                 ans+=mindis;
     94                 for(int i=0;i<t;i++)
     95                 {
     96                     edge[sta[i]].dis-=mindis;
     97                     edge[sta[i]^1].dis+=mindis;
     98                 }
     99                 s=edge[sta[tag]].from;
    100                 t=tag;
    101             }
    102         }
    103     }
    104     return ans;
    105 }
    106 
    107 int main()
    108 {
    109     while(~scanf("%d%d",&n,&m))
    110     {
    111         memset(head,-1,sizeof(head));
    112         edgenum=0;
    113         for(int i=0;i<n;i++)
    114         {
    115             int a,b,c;
    116             scanf("%d%d%d",&a,&b,&c);
    117             addedge(a,b,c);
    118         }
    119         printf("%d\n",dinic(1,m));
    120     }
    121     return 0;
    122 }
  • 相关阅读:
    10月27号
    10月23号
    10月26号
    10月30号
    10月28号
    10月29号
    JAVA日报
    JAVA日报
    JAVA日报
    JAVA日报
  • 原文地址:https://www.cnblogs.com/XBWer/p/2671240.html
Copyright © 2011-2022 走看看