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. 浅谈!DOCTYPE声明的作用?严格模式与混杂模式的区别?

    !DOCTYPE的作用: DOCTYPE是Document Type(文档类型)的缩写,<!DOCTYPE>声明必须是html文档的第一行,位于<html>标签之前.<! ...

  2. TCP/IP,HTTP,HTTPS,WEBSocket协议

    我看看着挺多的,我暂时没时间自己写,有需要的请借鉴 http://mp.weixin.qq.com/s?__biz=MzI0MDQ4MTM5NQ==&mid=2247486927&id ...

  3. JAVA之路(二)

    学道酬勤,这是第二次学习JAVA,感觉如醍醐灌顶一样,理解很多思想和道理. 本博只是自己对JAVA的一些理解,具体定义以及用法百科里有. 我为什么在博客园内记录自己的学习过程呢,因为我想有人知道我在学 ...

  4. bootstrap使用总结

    bootstrap是一个webcss框架,集合了html/css/jquery为一家,创建响应式的页面.所谓的响应式就是适配不同的上网设备. 使用bootstrap的步骤: 1.下载bootstrap ...

  5. Delphi中比较两个对象是否一致及地址是否相同[转]

    在delphi中,C#也是如此,对象的地址与对象变量(引用)的地址不是同一个概念.要加以区别. procedure TForm1.btn1Click(Sender: TObject); var    ...

  6. cxDBVerticalGrid

    定位在第一行并显示内置编辑器 cxDBVerticalGrid1.FocusedRow := cxDBVerticalGrid1.Rows[0]; cxDBVerticalGrid1.ShowEdit ...

  7. mysql 时间格式化参数表笔记

    DATE_FORMAT() 函数用于以不同的格式显示日期/时间数据. 语法: DATE_FORMAT(date,format) 实例: DATE_FORMAT(NOW(),'%b %d %Y %h:% ...

  8. date format 参数表

    format 必需.规定输出日期字符串的格式.可使用下列字符: d - 一个月中的第几天(从 01 到 31) D - 星期几的文本表示(用三个字母表示) j - 一个月中的第几天,不带前导零(1 到 ...

  9. ACM数论之旅8---组合数(组合大法好(,,• ₃ •,,) )

    组合数并不陌生(´・ω・`) 我们都学过组合数 会求组合数吗 一般我们用杨辉三角性质 杨辉三角上的每一个数字都等于它的左上方和右上方的和(除了边界) 第n行,第m个就是,就是C(n, m) (从0开始 ...

  10. Windows Server 2008 R2 安装WinDbg以及符号路径设置

    1.下载WinDbg安装包(Debuggers And Tools-x64_en-us v6.12.0002.633 AMD64.msi),双击安装 2.从网站http://msdn.microsof ...