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 中有哪些难以理解的概念?

    老手都是从新手一路过来的,提起Python中难以理解的概念,可能很多人对于Python变量赋值的机制有些疑惑,不过对于习惯于求根究底的程序员,只有深入理解了某个事物本质,掌握了它的客观规律,才能得心应 ...

  2. string类型和int类型之间的转换

    一.string转int 1. 使用string流 /* 字符串转整型 */ /* * istringstream:从 string 读取数据 * ostringstream:向 string 写入数 ...

  3. No.111_第四次团队会议

    后端的偏执 啊,这次又轮到我写团队博客了. 此时又是深夜,窗外漫天繁星.舍友的呼噜声惊吓了月亮,它害羞地跑回了云里去. 我关上灯拔掉机械,悄悄拿着电脑上了床,写这次的团队博客.曾经觉得自己绝对不会晚睡 ...

  4. java-switch语句

    switch语句是常用的java循环判断语句,但是有的知识点并不一定清楚. 首先是switch语句括号中的判断条件,判断条件只能是整型或者字符和整型或者字符组成的表达式. 再就是case语句,可以称之 ...

  5. WebGL学习笔记七点一

    第六章讲的是一些GL的一些语法,前面已经涉及,学习时直接跳过,来看第七章,第七章是真正意义的三维立体的出现,其实图形绘制方法是差不多的,就是Z坐标此时不再为0,所以很容易能构造出一些立体图形,但是立体 ...

  6. CefSharp,Winform程序中加载web网页

    源码地址:https://github.com/cefsharp/CefSharp 开源相关:https://github.com/cefsharp/CefSharp/tree/master/CefS ...

  7. mongodb的命令介绍

    db.help() 查看库级别的命令 db.stats() 查看数据库状态 db.version() 查看数据库版本 db.serverStatus() 查看数据库服务器状态 db.mycoll.he ...

  8. Spring Cloud之Eureka服务注册与发现

    解决什么问题 ➟阐述微服务以及服务注册发现的部分概念 ➟阐述Eureka服务注册与发现的部分原理及细节 为什么需要服务中心 过去,每个应用都是一个CPU,一个主机上的单一系统.然而今天,随着大数据和云 ...

  9. 对synchronized的一点理解

    一.synchronized的使用(一).synchronized同步方法1. “非线程安全”问题存在于“实例变量”中,如果是方法内部的私有变量,则不存在“非线程安全”问题.2. 如果多个线程共同访问 ...

  10. 用join取代not in

    写了好几个页面,速度都上不去,瓶颈在于SQL查询.太多的表,太多的not in,总是从一大推表和数据中筛选出一点数据.看了很多关于SQL优化的文章,都强烈要求不要太多使用not in查询,最好用表连接 ...