最大流问题。ISAP算法。注意可能会有重边,不过我用的数据结构支持重边。距离d我直接初始化为0,也可以用BFS逆向找一次。

-----------------------------------------------------------------------

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#define rep(i,l,r) for(int i=l;i<r;i++)
#define dow(i,l,r) for(int i=l;i>r;i--)
#define clr(x,c) memset(x,c,sizeof(x))
using namespace std;
const int inf=0x3f3f3f3f,maxn=200+5;
struct edge {
    int from,to,cap,flow;
};
struct ISAP {
    int n,m,s,t;
    vector<edge> edges;
    vector<int> g[maxn];
    int d[maxn];
    int cur[maxn];
    int p[maxn];
    int num[maxn];
    
    void init(int n) {
        this->n=n;
        rep(i,0,n) g[i].clear();
        edges.clear();
    }
    
    void addEdge(int from,int to,int cap) {
        edges.push_back((edge){from,to,cap,0});
        edges.push_back((edge){to,from,0,0});
        m=edges.size();
        g[from].push_back(m-2);
        g[to].push_back(m-1);
    }
    
    int augment() {
        int x=t,a=inf;
        while(x!=s) {
            edge& e=edges[p[x]];
            a=min(a,e.cap-e.flow);
            x=edges[p[x]].from;
        }
        x=t;
        while(x!=s) {
            edges[p[x]].flow+=a;
            edges[p[x]^1].flow-=a;
            x=edges[p[x]].from;
        }
        return a;
    }
    
    int maxFlow(int s,int t) {
        this->s=s; this->t=t;
        int flow=0;
        clr(d,0);
        clr(num,0);
        rep(i,0,n) num[d[i]]++;
        int x=s;
        clr(cur,0);
        while(d[s]<n) {
            if(x==t) {
                flow+=augment();
                x=s;
            }
            int ok=0;
            rep(i,cur[x],g[x].size()) {
                edge& e=edges[g[x][i]];
                if(e.cap>e.flow && d[x]==d[e.to]+1) {
                    ok=1;
                    p[e.to]=g[x][i];
                    cur[x]=i;
                    x=e.to;
                    break;
                }
            }
            if(!ok) {
                int m=n-1;
                rep(i,0,g[x].size()) {
                    edge& e=edges[g[x][i]];
                    if(e.cap>e.flow) m=min(m,d[e.to]);
                }
                if(--num[d[x]]==0) break;
                num[d[x]=m+1]++;
                cur[x]=0;
                if(x!=s) x=edges[p[x]].from;
            }
        }
        return flow;
    }
} isap;
int s() {
    int n,m;
    cin>>m>>n;
    isap.init(n);
    rep(i,0,m) {
        int from,to,cap,pd=1;
        scanf("%d%d%d",&from,&to,&cap);
        isap.addEdge(from-1,to-1,cap);
    }
    return isap.maxFlow(0,n-1);
}
int main() {
    freopen("ditch.in","r",stdin);
    freopen("ditch.out","w",stdout);
    
    cout<<s()<<endl;
    
    return 0;
}

-----------------------------------------------------------------------

Drainage Ditches
Hal Burch

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. Note however, that there can be more than one ditch between two intersections.

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.

PROGRAM NAME: ditch

INPUT FORMAT

Line 1: 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.
Line 2..N+1: Each of 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.

SAMPLE INPUT (file ditch.in)

5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10

OUTPUT FORMAT

One line with a single integer, the maximum rate at which water may emptied from the pond.

SAMPLE OUTPUT (file ditch.out)

50

USACO Section 4.2 Drainage Ditches(最大流)的更多相关文章

  1. USACO Section 4.2: Drainage Ditches

    最大流的模板题 /* ID: yingzho1 LANG: C++ TASK: ditch */ #include <iostream> #include <fstream> ...

  2. POJ 1273 || HDU 1532 Drainage Ditches (最大流模型)

    Drainage DitchesHal Burch Time Limit 1000 ms Memory Limit 65536 kb description Every time it rains o ...

  3. POJ 1273 - Drainage Ditches - [最大流模板题] - [EK算法模板][Dinic算法模板 - 邻接表型]

    题目链接:http://poj.org/problem?id=1273 Time Limit: 1000MS Memory Limit: 10000K Description Every time i ...

  4. Poj 1273 Drainage Ditches(最大流 Edmonds-Karp )

    题目链接:poj1273 Drainage Ditches 呜呜,今天自学网络流,看了EK算法,学的晕晕的,留个简单模板题来作纪念... #include<cstdio> #include ...

  5. poj 1273 Drainage Ditches 最大流入门题

    题目链接:http://poj.org/problem?id=1273 Every time it rains on Farmer John's fields, a pond forms over B ...

  6. TZOJ 4085 Drainage Ditches(最大流)

    描述 Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. Th ...

  7. HDU1532 Drainage Ditches —— 最大流(sap算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1532 Drainage Ditches Time Limit: 2000/1000 MS (Java/ ...

  8. poj-1273 Drainage Ditches(最大流基础题)

    题目链接: Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 67475   Accepted ...

  9. hdu 1532 Drainage Ditches(最大流)

                                                                                            Drainage Dit ...

随机推荐

  1. 佩特来项目经验小集合(2)___组合查询存储过程,报错 &quot;varchar JBID=&#39;&#39; 转换成数据类型 int 时失败&quot;

       今天写一个组合查询的存储过程遇到这样一个问题:在将 varchar 值 'SELECT * FROM View_DLS_WXJD_Customer WHERE 1=1 and JBID ='' ...

  2. Linux内核源代码解析——用户发送数据包的起源之sendto

    本文原创为freas_1990,转载请标明出处:http://blog.csdn.net/freas_1990/article/details/10162853 Jack:我想知道用户如何把数据发送到 ...

  3. 如何更快速加载你的JS页面

    确保代码尽量简洁 不要什么都依赖JavaScript.不要编写重复性的脚本.要把JavaScript当作糖果工具,只是起到美化作用.别给你的网站添加大量的JavaScript代码.只有必要的时候用一下 ...

  4. Windows消息传递函数SendMessage参数属性

    Windows消息传递函数SendMessage参数属性 转载于:http://www.cr173.com/html/5605_1.html Windows是一个消息驱动式系统,SendMessage ...

  5. linux安装桌面软件

    CentOS 作为服务器的操作系统是很常见的,但是因为需要稳定而没有很时髦的更新,所以很少做为桌面环境.在服务器上通常不需要安装桌面环境,最小化地安装 CentOS(也就是 minimal CentO ...

  6. Java学习之Comparable与Comparator的区别

    Comparable & Comparator 都是用来实现集合中元素的比较.排序的,只是 Comparable 是在集合内部定义的方法实现的排序,Comparator 是在集合外部实现的排序 ...

  7. Mysql中表名作为参数的问题

    近期由于程序的异常,导致数据库中创建了大量的表(约4000个),纠结的是表中的数据还都是有用的. 需要合并到一个表中,首先想到的就是使用存储过程来处理,但由于表名都是动态生成的,需要解决在存储过程中处 ...

  8. RapidMiner的基本使用(一个医疗数据的简单决策树算法分析)

    RapidMiner的基本使用(一个医疗数据的简单决策树算法分析) RapidMiner的基本使用(一个医疗数据的简单决策树算法分析) 需要分析的文件: 右键分别创建读取excel数据,选择属性,设置 ...

  9. ThinkPHP+uploadify+upload+PHPExcel 无刷新导入数据

    前端HTML+JQuery  备注Jquery需要1.x版本,不能用2.x版本 1.引入必要文件及上传input <load file="__PUBLIC__/js/jquery-1. ...

  10. CentOS下Mysql安装调试

    一.安装   yum安装:yum install -y mysql-server mysql mysql-devel 设置自启动:chkconfig mysqld on 启动MySQL:service ...