Drainage Ditches

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 45 Accepted Submission(s): 38
 
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
 
 
Source
USACO 93
 

题意:

裸的最大流

代码:

//Edmonds-Karp算法,紫书366页。模板。点的编号从0开始。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
const int maxn=,inf=0x7fffffff;
struct edge{
int from,to,cap,flow;
edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};
struct Edmonds_Karp{
int n,m;
vector<edge>edges;//边数的两倍
vector<int>g[maxn];//邻接表,g[i][j]表示节点i的第j条边在e数组中的序号
int a[maxn];//当起点到i的可改进量
int p[maxn];//最短路树上p的入弧编号
void init(int n){
for(int i=;i<n;i++) g[i].clear();
edges.clear();
}
void addedge(int from,int to,int cap){
edges.push_back(edge(from,to,cap,));
edges.push_back(edge(to,from,,));//反向弧
m=edges.size();
g[from].push_back(m-);
g[to].push_back(m-);
}
int Maxflow(int s,int t){
int flow=;
for(;;){
memset(a,,sizeof(a));
queue<int>q;
q.push(s);
a[s]=inf;
while(!q.empty()){
int x=q.front();q.pop();
for(int i=;i<(int)g[x].size();i++){
edge&e=edges[g[x][i]];
if(!a[e.to]&&e.cap>e.flow){
p[e.to]=g[x][i];
a[e.to]=min(a[x],e.cap-e.flow);
q.push(e.to);
}
}
if(a[t]) break;
}
if(!a[t]) break;
for(int u=t;u!=s;u=edges[p[u]].from){
edges[p[u]].flow+=a[t];
edges[p[u]^].flow-=a[t];
}
flow+=a[t];
}
return flow;
}
}EK;
int main()
{
int n,m,a,b,c;
while(scanf("%d%d",&n,&m)==){
EK.init(m);
for(int i=;i<n;i++){
scanf("%d%d%d",&a,&b,&c);
a--;b--;
EK.addedge(a,b,c);
}
printf("%d\n",EK.Maxflow(,m-));
}
return ;
}
//Dinic算法模板 白书358页,点的编号从0开始
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
const int maxn=;
const int inf=0x7fffffff;
struct Edge{
int from,to,cap,flow;
Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};
struct Dinic{
int n,m,s,t;
vector<Edge>edges;
vector<int>g[maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn];
void Init(int n){
this->n=n;
for(int i=;i<n;i++) g[i].clear();
edges.clear();
}
void Addedge(int from,int to,int cap){
edges.push_back(Edge(from,to,cap,));
edges.push_back(Edge(to,from,,));//反向弧
m=edges.size();
g[from].push_back(m-);
g[to].push_back(m-);
}
bool Bfs(){
memset(vis,,sizeof(vis));
queue<int>q;
q.push(s);
d[s]=;
vis[s]=;
while(!q.empty()){
int x=q.front();q.pop();
for(int i=;i<(int)g[x].size();i++){
Edge &e=edges[g[x][i]];
if(!vis[e.to]&&e.cap>e.flow){
vis[e.to]=;
d[e.to]=d[x]+;
q.push(e.to);
}
}
}
return vis[t];
}
int Dfs(int x,int a){
if(x==t||a==) return a;
int flow=,f;
for(int&i=cur[x];i<(int)g[x].size();i++){
Edge &e=edges[g[x][i]];
if(d[x]+==d[e.to]&&(f=Dfs(e.to,min(a,e.cap-e.flow)))>){
e.flow+=f;
edges[g[x][i]^].flow-=f;
flow+=f;
a-=f;
if(a==) break;
}
}
return flow;
}
int Maxflow(int s,int t){
this->s=s;this->t=t;
int flow=;
while(Bfs()){
memset(cur,,sizeof(cur));
flow+=Dfs(s,inf);
}
return flow;
}
}dc;
int main()
{
int n,m,a,b,c;
while(scanf("%d%d",&n,&m)==){
dc.Init(m);
while(n--){
scanf("%d%d%d",&a,&b,&c);
a--;b--;
dc.Addedge(a,b,c);
}
printf("%d\n",dc.Maxflow(,m-));
}
return ;
}
//anather
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int INF=0x7fffffff;
const int MAXN=;//点数
const int MAXM=;//边数
int n,m,tot,S,T,head[MAXN],h[MAXN],q[MAXN],ans;
struct Edge { int to,val,next; }edge[MAXM];
void init(int last)
{
S=;T=last-;//S源点,T汇点
tot=;
memset(head,-,sizeof(head));
}
void addedge(int x,int y,int z)
{
edge[tot].to=y;edge[tot].val=z;edge[tot].next=head[x];
head[x]=tot++;
}
bool bfs()
{
memset(h,-,sizeof(h));
int top=,last=;
q[top]=S;h[S]=;
while(top<last){
int now=q[top];top++;
for(int i=head[now];i!=-;i=edge[i].next){
if(edge[i].val&&h[edge[i].to]<){
q[last++]=edge[i].to;
h[edge[i].to]=h[now]+;
}
}
}
if(h[T]==-) return ;
return ;
}
int dfs(int x,int f)
{
if(x==T) return f;
int w,used=;
for(int i=head[x];i!=-;i=edge[i].next){
if(edge[i].val&&h[edge[i].to]==h[x]+){
w=f-used;
w=dfs(edge[i].to,min(w,edge[i].val));
edge[i].val-=w;
edge[i^].val+=w;
used+=w;
if(used==f) return f;
}
}
if(!used) h[x]=-;
return used;
}
int dinic()
{
int ans=;
while(bfs()) ans+=dfs(S,INF);
return ans;
}
int main()
{
int n,m,a,b,c;
while(scanf("%d%d",&n,&m)==){
init(m);//根据题目传参
while(n--){
scanf("%d%d%d",&a,&b,&c);
a--;b--;
addedge(a,b,c);
addedge(b,a,);//建反向边
//建边根据题目而定
}
int ans=dinic();
printf("%d\n",ans);
}
return ;
}

HDU1532最大流 Edmonds-Karp,Dinic算法 模板的更多相关文章

  1. POJ 1815 - Friendship - [拆点最大流求最小点割集][暴力枚举求升序割点] - [Dinic算法模板 - 邻接矩阵型]

    妖怪题目,做到现在:2017/8/19 - 1:41…… 不过想想还是值得的,至少邻接矩阵型的Dinic算法模板get√ 题目链接:http://poj.org/problem?id=1815 Tim ...

  2. POJ 1273 Drainage Ditches(网络流dinic算法模板)

    POJ 1273给出M条边,N个点,求源点1到汇点N的最大流量. 本文主要就是附上dinic的模板,供以后参考. #include <iostream> #include <stdi ...

  3. hdu 2435 dinic算法模板+最小割性质

    #include<stdio.h> #include<queue> #include<string.h> using namespace std; #define ...

  4. 最大流EK和Dinic算法

    最大流EK和Dinic算法 EK算法 最朴素的求最大流的算法. 做法:不停的寻找增广路,直到找不到为止 代码如下: @Frosero #include <cstdio> #include ...

  5. POJ 3469.Dual Core CPU 最大流dinic算法模板

    Dual Core CPU Time Limit: 15000MS   Memory Limit: 131072K Total Submissions: 24830   Accepted: 10756 ...

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

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

  7. hdu-3549 Flow Problem---最大流模板题(dinic算法模板)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3549 题目大意: 给有向图,求1-n的最大流 解题思路: 直接套模板,注意有重边 传送门:网络流入门 ...

  8. 求最大流dinic算法模板

    //最短增广路,Dinic算法 struct Edge { int from,to,cap,flow; };//弧度 void AddEdge(int from,int to,int cap) //增 ...

  9. POJ 1459 Power Network(网络最大流,dinic算法模板题)

    题意:给出n,np,nc,m,n为节点数,np为发电站数,nc为用电厂数,m为边的个数.      接下来给出m个数据(u,v)z,表示w(u,v)允许传输的最大电力为z:np个数据(u)z,表示发电 ...

随机推荐

  1. Python数据分析工具库-Numpy 数组支持库(一)

    1 Numpy数组 在Python中有类似数组功能的数据结构,比如list,但在数据量大时,list的运行速度便不尽如意,Numpy(Numerical Python)提供了真正的数组功能,以及对数据 ...

  2. markdown语法示例

    现在是我在学习Markdown时做的笔记.学完这些Markdown的基本使用已经不成问题. 1. 标题设置(让字体变大,和word的标题意思一样)在Markdown当中设置标题,有两种方式:第一种:通 ...

  3. Vue 入门之概念

    Vue 简介 Vue 是一个前端的双向绑定类的框架,发音[读音 /vjuː/, 类似于 [view].新的 Vue 版本参考了 React 的部分设计,当然也有自己独特的地方,比如 Vue 的单文件组 ...

  4. C++ 类的定义与实现

    摘自这篇博客https://blog.csdn.net/xulingxin/article/details/81335030   一."类" 的介绍     在C++中, 用 &q ...

  5. 《Java学习笔记JDK8》学习总结

    chapter 6   继承与多态 6.1何谓继承 1.继承的定义:继承就是避免多个类间重复定义共同行为. 2.总结:教材中通过设计一款RPG游戏的部分代码向我们展示了“重复”程序代码的弊端,为了改进 ...

  6. Leetcode题库——7.反转整数

    @author: ZZQ @software: PyCharm @file: IntReverse.py @time: 2018/9/16 16:36 要求:整数反转(给定一个 32 位有符号整数,将 ...

  7. 【贪心算法】POJ-1017

    一.题目 Description A factory produces products packed in square packets of the same height h and of th ...

  8. PHP学习心得1

    php是动态网站开发的优秀语言,在学习的时候万万不能冒进.在系统的学习前,我认为不应该只是追求实现某种效果,因为即使你复制他人的代码调试成功,实现了你所期望的效果,你也不了解其中的原理,这样你很难利用 ...

  9. vue 组件 单选切换控制模板 v-bind-is

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>T ...

  10. Thread的start和run的区别

    最近看到一个题目,代码如下: public static void main(String args[]) { Thread t = new Thread() { public void run() ...