【codevs1993】草地排水

题目描述 Description

在农夫约翰的农场上,每逢下雨,Bessie最喜欢的三叶草地就积聚了一潭水。这意味着草地被水淹没了,并且小草要继续生长还要花相当长一段时间。因此,农夫约翰修建了一套排水系统来使贝茜的草地免除被大水淹没的烦恼(不用担心,雨水会流向附近的一条小溪)。作为一名一流的技师,农夫约翰已经在每条排水沟的一端安上了控制器,这样他可以控制流入排水沟的水流量。

农夫约翰知道每一条排水沟每分钟可以流过的水量,和排水系统的准确布局(起点为水潭而终点为小溪的一张网)。需要注意的是,有些时候从一处到另一处不只有一条排水沟。

根据这些信息,计算从水潭排水到小溪的最大流量。对于给出的每条排水沟,雨水只能沿着一个方向流动,注意可能会出现雨水环形流动的情形。

输入描述 Input Description

第1行: 两个用空格分开的整数N (0 <= N <= 200) 和 M (2 <= M <= 200)。N是农夫John已经挖好的排水沟的数量,M是排水沟交叉点的数量。交点1是水潭,交点M是小溪。

第二行到第N+1行: 每行有三个整数,Si, Ei, 和 Ci。Si 和 Ei (1 <= Si, Ei <= M) 指明排水沟两端的交点,雨水从Si 流向Ei。Ci (0 <= Ci <= 10,000,000)是这条排水沟的最大容量。

输出描述 Output Description

输出一个整数,即排水的最大流量。

样例输入 Sample Input

5 4

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

样例输出 Sample Output

50

代码

ek算法

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <queue>
#include <typeinfo>
#include <map>
#include <stack>
typedef long long ll;
#define inf 0x7fffffff
using namespace std;
inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
//************************************************************************************** int pre[];
int mp[][],flow[][];
int n,m;
int father[];
int ek(int s,int y)
{
int q[];
int u;
int f=;
while()
{
int di=;
int front=;
memset(pre,,sizeof(pre));
pre[s]=0x7fffffff;
q[front]=s;
while(front<di)
{
u=q[front];
front++;
for(int v=;v<=n;v++)
{
if(pre[v]==&&mp[u][v]>flow[u][v])
{
father[v]=u;
q[di++]=v;
pre[v]=min(pre[u],mp[u][v]-flow[u][v]);
}
}
}
if(pre[y]==)return f;
for(u=y;u!=s;u=father[u])
{
flow[father[u]][u]+=pre[y];
flow[u][father[u]]-=pre[y];
}
f+=pre[y];
}
} int main()
{
scanf("%d%d",&m,&n);
for(int i=;i<=m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
mp[u][v]+=w;
}
printf("%d\n",ek(,n));
/* for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
printf("%d ",flow[i][j]);
printf("\n");
}*/
return ;
}

dinic矩阵版

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <queue>
#include <typeinfo>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
//**************************************************************************************
using namespace std;
const int MAX=0x5fffffff;//
int tab[][];//邻接矩阵
int dis[];//距源点距离,分层图
int q[],h,r;//BFS队列 ,首,尾
int N,M,ANS;//N:点数;M,边数
int BFS()
{
int i,j;
memset(dis,0xff,sizeof(dis));//以-1填充
dis[]=;
h=;
r=;
q[]=;
while (h<r)
{
j=q[++h];
for (i=; i<=N; i++)
if (dis[i]< && tab[j][i]>)
{
dis[i]=dis[j]+;
q[++r]=i;
}
}
if (dis[N]>)
return ;
else
return ;//汇点的DIS小于零,表明BFS不到汇点
}
//Find代表一次增广,函数返回本次增广的流量,返回0表示无法增广
int find(int x,int low)//Low是源点到现在最窄的(剩余流量最小)的边的剩余流量
{
int i,a=;
if (x==N)return low;//是汇点
for (i=; i<=N; i++)
if (tab[x][i] > //联通
&& dis[i]==dis[x]+ //是分层图的下一层
&&(a=find(i,min(low,tab[x][i]))))//能到汇点(a <> 0)
{
tab[x][i]-=a;
tab[i][x]+=a;
return a;
}
return ;
}
int main()
{
int i,j,f,t,flow,tans;
while (scanf("%d%d",&M,&N)!=EOF)
{
memset(tab,,sizeof(tab));
for (i=; i<=M; i++)
{
scanf("%d%d%d",&f,&t,&flow);
tab[f][t]+=flow;
}
//
ANS=;
while (BFS())//要不停地建立分层图,如果BFS不到汇点才结束
{
while(tans=find(,0x7fffffff))ANS+=tans;//一次BFS要不停地找增广路,直到找不到为止
}
printf("%d\n",ANS);
} }

dinic灵界表

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <queue>
#include <typeinfo>
#include <map>
#include <stack>
typedef long long ll;
#define inf 0x7fffffff
using namespace std;
inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
//**************************************************************************************
struct ss
{
int to,v,next;
} e[];
int ans,q[];
int head[];
int vis[];
int n,m;
int t=;
void add(int u,int v,int w)
{
e[t].to=v;
e[t].next=head[u];
e[t].v=w;
head[u]=t++;
}
void insert(int u,int v,int w)
{
add(u,v,w);
add(v,u,);
}
bool bfs()
{
memset(vis,-,sizeof(vis));
int di=,front=,now;
q[front]=;
vis[]=;
while(front<di)
{
now=q[front];
front++;
if(front==)front=;
for(int i=head[now]; i; i=e[i].next)
{
if(e[i].v&&vis[e[i].to]==-)
{
vis[e[i].to]=vis[now]+;
q[di++]=e[i].to;
if(di==)di=;
}
}
}
if(vis[n]==-)return ;
else return ;
}
int dfs(int x,int f)
{
if(x==n) return f;
int w,used=;
for(int i=head[x]; i; i=e[i].next)
{
if(e[i].v&&vis[e[i].to]==vis[x]+)
{
if(w=dfs(e[i].to,min(f,e[i].v)))
{
e[i].v-=w;
e[i+].v+=w;
return w;
}
}
}
return ;
}
void dinic()
{
while(bfs())ans+=dfs(,inf);
}
int main()
{ scanf("%d%d",&m,&n);
for(int i=; i<=m; i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
insert(a,b,c);
}
dinic();
printf("%d\n",ans);
return ;
}

【codevs1993】草地排水 最大流的更多相关文章

  1. codevs1993 草地排水(最大流)

    1993 草地排水 USACO  时间限制: 2 s  空间限制: 256000 KB  题目等级 : 钻石 Diamond   题目描述 Description 在农夫约翰的农场上,每逢下雨,Bes ...

  2. codevs1993草地排水(最大流)

    最近学了最大流,于是去codevs找了几道最大流裸题(这是我第一次写网络流). 题目大意:求一个图的最大流(就是这样的裸题) 第一次A网络流的题,发个博客纪念一下. var n,m,i,j,k,h,t ...

  3. POJ1273 USACO 4.2.1 Drainage Ditches CodeVS1993草地排水 网络流 最大流 SAP

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 传送门 - POJ 传送门 - CodeVS 题意概括 给出一个图,告诉你边和容量,起点是1,汇点是n,让你求最大流. 题解 ...

  4. - > 网络流(【最大流】草地排水模板题)

    1993 草地排水 USACO  时间限制: 2 s  空间限制: 256000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Description 在农夫约翰的农场上,每 ...

  5. 草地排水 洛谷P2740 最大流 入门题目

    草地排水 洛谷P2740 最大流入门题目 题意 在农夫约翰的农场上,每逢下雨,贝茜最喜欢的三叶草地就积聚了一潭水.这意味着草地被水淹没了,并且小草要继续生长还要花相当长一段时间.因此,农夫约翰修建了一 ...

  6. 【USACO】草地排水

    Drainage Ditches 草地排水 usaco 4.2.1描述在农夫约翰的农场上,每逢下雨,Bessie最喜欢的三叶草地就积聚了一潭水.这意味着草地被水淹没了,并且小草要继续生长还要花相当长一 ...

  7. AC日记——草地排水 codevs 1993

    1993 草地排水 USACO  时间限制: 2 s  空间限制: 256000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Description 在农夫约翰的农场上,每 ...

  8. 题解 【USACO 4.2.1】草地排水

    [USACO 4.2.1]草地排水 Description 在农夫约翰的农场上,每逢下雨,贝茜最喜欢的三叶草地就积聚了一潭水.这意味着草地被水淹没了,并且小草要继续生长还要花相当长一段时间.因此,农夫 ...

  9. [网络流]Drainage Ditches(草地排水)

    Drainage Ditches(草地排水) 题目描述 在农夫约翰的农场上,每逢下雨,贝茜最喜欢的三叶草地就积聚了一潭水.这意味着草地被水淹没了,并且小草要继续生长还要花相当长一段时间.因此,农夫约翰 ...

随机推荐

  1. (转) 新的开始之Win7、CentOS 6.4 双系统 硬盘安装

    http://blog.csdn.net/cnclenovo/article/details/11358447

  2. 转:this的用法

    this指针的含义及其用法: 1. this指针是一个隐含于每一个成员函数中的特殊指针.它指向正在被该成员函数操作的那个对象.2. 当对一个对象调用成员函数时,编译程序先将对象的地址赋给this指针, ...

  3. Linux 下 Redis 安装详解

    文章来源:www.oschina.net/question/12_18065 redis作为NoSQL数据库的一种应用,响应速度和命中率上还是比较高效的.项目中需要用集中式可横向扩展的缓存框架,做了一 ...

  4. 磁盘 I/O 性能监控指标和调优方法

    在介绍磁盘 I/O 监控命令前,我们需要了解磁盘 I/O 性能监控的指标,以及每个指标的所揭示的磁盘某方面的性能.磁盘 I/O 性能监控的指标主要包括:指标 1:每秒 I/O 数(IOPS 或 tps ...

  5. [Effective JavaScript 笔记]第22条:使用arguments创建可变参数的函数

    第21条讲述使用可变参数的函数average.该函数可处理任意数量的参数并返回这些参数的平均值. 如何创建可变参数的函数 1.实现固定元数的函数 书上的版本 function averageOfArr ...

  6. sql注入之你问我答小知识

    /*每日更新,珍惜少年时博客*/ 1.问:为啥order by 是排序.而在注入当中后面加的却不是字段而是数字捏? 答:第N个字段嘛.order by 5就是第五个字段,如果5存在,6不存在.就说明只 ...

  7. Linux system V

    Sysvinit 的小结 Sysvinit 的优点是概念简单.Service 开发人员只需要编写启动和停止脚本,概念非常清楚:将 service 添加/删除到某个 runlevel 时,只需要执行一些 ...

  8. backbone杂记

    国人的一个不错的分享:http://gavin.iteye.com/blog/1446277 backbone项目如何组织文件结构 引用: http://bocoup.com/weblog/organ ...

  9. 【Spring】Spring系列5之Spring支持事务处理

    5.Spring支持事务处理 5.1.事务准备 以上代码结构与AOP的前置通知.返回通知.异常通知.后置通知一样. 5.2.声明式事务 5.2.1.基于注解 5.2.2.基于配置文件 5. 3.事务传 ...

  10. 【SpringMVC】SpringMVC系列3之@PathVariable映射URL占位符参数

    3.@PathVariable映射URL占位符参数 3.1.概述 带占位符的 URL 是 Spring3.0 新增的功能,该功能在SpringMVC 向 REST 目标挺进发展过程中具有里程碑的意义. ...