• HD1532Drainage Ditches(最大流模板裸题 + 邻接表)


    Drainage Ditches

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 13273    Accepted Submission(s): 6288


    Problem 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
     
     
    第一道最大流题目!
     
     1 #include <iostream>
     2 #include <queue>
     3 #include<string.h>
     4 using namespace std;
     5 #define arraysize 201
     6 int maxData = 0x7fffffff;
     7 int capacity[arraysize][arraysize]; //记录残留网络的容量
     8 int flow[arraysize];                //标记从源点到当前节点实际还剩多少流量可用
     9 int pre[arraysize];                 //标记在这条路径上当前节点的前驱,同时标记该节点是否在队列中
    10 int n,m;
    11 queue<int> myqueue;
    12 int BFS(int src,int des)
    13 {
    14     int i,j;
    15     while(!myqueue.empty())       //队列清空
    16         myqueue.pop();
    17     for(i=1;i<m+1;++i)
    18     {
    19         pre[i]=-1;
    20     }
    21     pre[src]=0;
    22     flow[src]= maxData;
    23     myqueue.push(src);
    24     while(!myqueue.empty())
    25     {
    26         int index = myqueue.front();
    27         myqueue.pop();
    28         if(index == des)            //找到了增广路径
    29             break;
    30         for(i=1;i<m+1;++i)
    31         {
    32             if(i!=src && capacity[index][i]>0 && pre[i]==-1)
    33             {
    34                  pre[i] = index; //记录前驱
    35                  flow[i] = min(capacity[index][i],flow[index]);   //关键:迭代的找到增量
    36                  myqueue.push(i);
    37             }
    38         }
    39     }
    40     if(pre[des]==-1)      //残留图中不再存在增广路径
    41         return -1;
    42     else
    43         return flow[des];
    44 }
    45 int maxFlow(int src,int des)
    46 {
    47     int increasement= 0;
    48     int sumflow = 0;
    49     while((increasement=BFS(src,des))!=-1)
    50     {
    51          int k = des;          //利用前驱寻找路径
    52          while(k!=src)
    53          {
    54               int last = pre[k];
    55               capacity[last][k] -= increasement; //改变正向边的容量
    56               capacity[k][last] += increasement; //改变反向边的容量
    57               k = last;
    58          }
    59          sumflow += increasement;
    60     }
    61     return sumflow;
    62 }
    63 int main()
    64 {
    65     int i,j;
    66     int start,end,ci;
    67     while(cin>>n>>m)
    68     {
    69         memset(capacity,0,sizeof(capacity));
    70         memset(flow,0,sizeof(flow));
    71         for(i=0;i<n;++i)
    72         {
    73             cin>>start>>end>>ci;
    74             if(start == end)               //考虑起点终点相同的情况
    75                continue;
    76             capacity[start][end] +=ci;     //此处注意可能出现多条同一起点终点的情况
    77         }
    78         cout<<maxFlow(1,m)<<endl;
    79     }
    80     return 0;
    81 }
    View Code
    刘汝佳书上给的邻接表算法
     1 #include <iostream>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <cstdio>
     5 #include <queue>
     6 #include <vector>
     7 #include <string.h>
     8 using namespace std;
     9 const int MAX = 210;
    10 const int INF = 0x3f3f3f3f;
    11 struct Edge
    12 {
    13     int from,to,cap,flow;
    14     Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){};
    15 };
    16 int n,m;
    17 vector<Edge> edge;
    18 vector<int> g[MAX];
    19 int a[MAX],p[MAX];
    20 void init()
    21 {
    22     for(int i = 0; i <= m; i++)
    23         g[i].clear();
    24     edge.clear();
    25 }
    26 int Maxflow(int s,int t)
    27 {
    28     int flow = 0;
    29     while(true)
    30     {
    31         memset(a,0,sizeof(a));
    32         queue<int> q;
    33         q.push(s);
    34         a[s] = INF;
    35         while(q.size())
    36         {
    37             int x = q.front();
    38             q.pop();
    39             int len = g[x].size();
    40             for(int i = 0; i < len; i++)
    41             {
    42                 Edge e = edge[ g[x][i] ];
    43                 if(a[e.to] == 0 && e.cap > e.flow)
    44                 {
    45                     p[e.to] = g[x][i];
    46                     a[e.to] = min(a[x],e.cap - e.flow);
    47                     q.push(e.to);
    48                 }
    49             }
    50             if(a[t])
    51                 break;
    52         }
    53         if(a[t] == 0)
    54             break;
    55         for(int i = t; i != s; i = edge[ p[i] ].from)
    56         {
    57             edge[ p[i] ].flow += a[t];
    58             edge[ p[i] ^ 1 ].flow -= a[t];
    59         }
    60         flow += a[t];
    61     }
    62     return flow;
    63 }
    64 int main()
    65 {
    66     while(scanf("%d%d", &n,&m) != EOF)
    67     {
    68         int s,e,v,t;
    69         init();
    70         for(int i = 1; i <= n; i++)
    71         {
    72             scanf("%d%d%d",&s,&e,&v);
    73             edge.push_back(Edge(s,e,v,0));
    74             edge.push_back(Edge(e,s,0,0));
    75             t = edge.size();
    76             g[s].push_back(t - 2);
    77             g[e].push_back(t - 1);
    78         }
    79        
    80         printf("%d
    ",Maxflow(1,m));
    81     }
    82 }
    View Code
  • 相关阅读:
    扩展json序列化datatime类型数据
    用select实现socket的IO多路复用
    Python单例模式
    Django(信号相关)
    将字符串按固定长度分隔成子串
    Android Handler介绍
    Android activity生命周期
    Java 启动线程的方式
    java线程中的sleep和wait区别
    JAVA 统计字符串中中文,英文,数字,空格的个数
  • 原文地址:https://www.cnblogs.com/zhaopAC/p/5018095.html
Copyright © 2020-2023  润新知