• POJ1273 HDU1532 Drainage Ditches


    题目链接:http://poj.org/problem?id=1273

    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.

    裸最大流Dinic多路增广模版。CodeVS上AC,POJ上没看到The input includes several cases. WA了n次呵呵……

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <cstring>
     5 #include <queue>
     6 #define rep(i,l,r) for(int i=l; i<=r; i++)
     7 #define clr(x,y) memset(x,y,sizeof(x))
     8 #define travel(x) for(int i=last[x]; i!=-1; i=edge[i].pre)
     9 using namespace std;
    10 const int INF = 0x3f3f3f3f;
    11 const int maxn = 210;
    12 struct Edge{
    13     int pre,to,cost;
    14 }edge[maxn<<1];
    15 int m,n,x,y,z,now,tot,ans,last[maxn],d[maxn];
    16 queue <int> q;
    17 inline void addedge(int x,int y,int z){
    18     edge[++tot].pre = last[x];
    19     edge[tot].to = y;
    20     edge[tot].cost = z;
    21     last[x] = tot;
    22     edge[++tot].pre = last[y];
    23     edge[tot].to = x;
    24     edge[tot].cost = 0;
    25     last[y] = tot;
    26 }
    27 bool bfs(){
    28     while (!q.empty()) q.pop();
    29     clr(d,-1); d[1] = 0; q.push(1);
    30     while (!q.empty()){
    31         now = q.front(); q.pop();
    32         travel(now){
    33             if (d[edge[i].to] == -1 && edge[i].cost > 0){
    34                 d[edge[i].to] = d[now] + 1;
    35                 q.push(edge[i].to);
    36             }
    37         }
    38     }
    39     return d[n] != -1;
    40 }
    41 int dfs(int x,int in){
    42     if (x == n) return in; int w = 0;
    43     for(int i=last[x]; i != -1 && w < in; i=edge[i].pre){
    44         if (d[edge[i].to] == d[x] + 1 && edge[i].cost > 0){
    45             int delta = dfs(edge[i].to,min(in-w,edge[i].cost));
    46             edge[i].cost -= delta;
    47             edge[i^1].cost += delta;
    48             w += delta;
    49         }
    50     }
    51     if (w < in) d[x] = -1;
    52     return w;
    53 }
    54 int main(){
    55     while (scanf("%d%d",&m,&n) == 2){
    56         tot = -1; ans = 0; clr(last,-1);
    57         rep(i,1,m){
    58             scanf("%d%d%d",&x,&y,&z);
    59             addedge(x,y,z);
    60         }
    61         while (bfs()){
    62             int nowans = dfs(1,0x7fffffff);
    63             ans += nowans;
    64         }
    65         printf("%d
    ",ans);
    66     }
    67     return 0;
    68 }
    View Code

    2015.11.26 update: 加入当前弧优化

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <cstring>
     5 #include <queue>
     6 #define rep(i,l,r) for(int i=l; i<=r; i++)
     7 #define clr(x,y) memset(x,y,sizeof(x))
     8 #define travel(x) for(int i=last[x]; i!=-1; i=edge[i].pre)
     9 using namespace std;
    10 const int INF = 0x3f3f3f3f;
    11 const int maxn = 210;
    12 struct Edge{
    13     int pre,to,cost;
    14 }edge[maxn<<1];
    15 int m,n,x,y,z,now,tot,ans,last[maxn],d[maxn],cur[maxn];
    16 queue <int> q;
    17 inline void addedge(int x,int y,int z){
    18     edge[++tot].pre = last[x];
    19     edge[tot].to = y;
    20     edge[tot].cost = z;
    21     last[x] = tot;
    22     edge[++tot].pre = last[y];
    23     edge[tot].to = x;
    24     edge[tot].cost = 0;
    25     last[y] = tot;
    26 }
    27 bool bfs(){
    28     while (!q.empty()) q.pop();
    29     clr(d,-1); d[1] = 0; q.push(1);
    30     while (!q.empty()){
    31         now = q.front(); q.pop();
    32         travel(now){
    33             if (d[edge[i].to] == -1 && edge[i].cost > 0){
    34                 d[edge[i].to] = d[now] + 1;
    35                 q.push(edge[i].to);
    36             }
    37         }
    38     }
    39     return d[n] != -1;
    40 }
    41 int dfs(int x,int in){
    42     if (x == n) return in; int w = 0;
    43     for(int i=cur[x]; i != -1 && w < in; i=edge[i].pre){
    44         if (d[edge[i].to] == d[x] + 1 && edge[i].cost > 0){
    45             int delta = dfs(edge[i].to,min(in-w,edge[i].cost));
    46             edge[i].cost -= delta;
    47             edge[i^1].cost += delta;
    48             if (edge[i].cost) cur[x] = i;
    49             w += delta;
    50         }
    51     }
    52     if (w < in) d[x] = -1;
    53     return w;
    54 }
    55 int main(){
    56     while (scanf("%d%d",&m,&n) == 2){
    57         tot = -1; ans = 0; clr(last,-1);
    58         rep(i,1,m){
    59             scanf("%d%d%d",&x,&y,&z);
    60             addedge(x,y,z);
    61         }
    62         while (bfs()){
    63             rep(i,1,n) cur[i] = last[i];
    64             int nowans = dfs(1,0x7fffffff);
    65             ans += nowans;
    66         }
    67         printf("%d
    ",ans);
    68     }
    69     return 0;
    70 }
    View Code
  • 相关阅读:
    SQL Server 百万级数据提高查询速度的方法(转)
    sql优化的几种方法
    MyBatis中调用存储过程和函数
    android ipc通信机制之二序列化接口和Binder
    APK的目录结构
    android Handler错误,不同的包Handler
    BaiduMap开发,获取公交站点信息。
    GitHub托管项目步骤
    Mysql,JDBC封装
    简单工厂模式练习
  • 原文地址:https://www.cnblogs.com/jimzeng/p/poj1273.html
Copyright © 2020-2023  润新知