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. 爬虫_处理js动态加载

    1.selenium模块下载网页提取url,[煎蛋网] https://www.cnblogs.com/fat39/p/9865949.html#tag5 2.该网页加密了url,通过js获取图片.分 ...

  2. (第二周)scrum站立会议

    敏捷流程scrum中的很重要的一个制度之一每日站立会议 了解的内容: 问题:为啥不用email汇报代替每日会议? E-mail不能取代每日会议,E-mail只会增加沟通成本,而且不能提供细节信息或者给 ...

  3. iOS开发学习-NSUserDefaults的介绍和用法

    NSUserDefaults类提供了一个与默认系统进行交互的编程接口.NSUserDefaults对象是用来保存,恢复应用程序相关的偏好设置,配置数据等等.默认系统允许应用程序自定义它的行为去迎合用户 ...

  4. Go Going软件需求规格说明书

    1.目标是什么,目标不包括什么? 我们软件的目标是让大学生走出校园,用最小的花费到更多的地方去,开阔视野,读万卷书再行万里路. 目标暂且不包括外校学生 2.用户和典型场景是什么? 用户:在校大学生 典 ...

  5. Task 4.4二维环形数组求最大子矩阵之和

    任务: (1)输入一个二维整形数组,数组里有正数也有负数. (2)二维数组首尾相接,象个一条首尾相接带子一样. (3)数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和. (4)求所有子数 ...

  6. Leetcode题库——8.字符串转为整数【##】

    @author: ZZQ @software: PyCharm @file: myAtoi.py @time: 2018/9/20 20:54 要求:实现 atoi,将字符串转为整数. 1)根据需要丢 ...

  7. Internet History, Technology and Security (Week3)

    Week3. Welcome to week 3! This is our fourth and final week of History where we make the connection ...

  8. 对WEB url 发送POST请求

    package com.excellence.spark; import java.util.List; import com.excellence.spark.test.test; import c ...

  9. 001_JavaWeb之Servlet的路径映射问题

    001_JavaWeb之Servlet的路径映射问题 在web.xml中写入: <servlet> <servlet-name>DeleteStudent</servle ...

  10. Redis有序集内部实现原理分析

    Redis技术交流群481804090 Redis:https://github.com/zwjlpeng/Redis_Deep_Read Redis中支持的数据结构比Memcached要多的多啦,如 ...