【codevs1993】草地排水 最大流
【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
样例输出 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】草地排水 最大流的更多相关文章
- codevs1993 草地排水(最大流)
1993 草地排水 USACO 时间限制: 2 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题目描述 Description 在农夫约翰的农场上,每逢下雨,Bes ...
- codevs1993草地排水(最大流)
最近学了最大流,于是去codevs找了几道最大流裸题(这是我第一次写网络流). 题目大意:求一个图的最大流(就是这样的裸题) 第一次A网络流的题,发个博客纪念一下. var n,m,i,j,k,h,t ...
- POJ1273 USACO 4.2.1 Drainage Ditches CodeVS1993草地排水 网络流 最大流 SAP
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 传送门 - POJ 传送门 - CodeVS 题意概括 给出一个图,告诉你边和容量,起点是1,汇点是n,让你求最大流. 题解 ...
- - > 网络流(【最大流】草地排水模板题)
1993 草地排水 USACO 时间限制: 2 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description 在农夫约翰的农场上,每 ...
- 草地排水 洛谷P2740 最大流 入门题目
草地排水 洛谷P2740 最大流入门题目 题意 在农夫约翰的农场上,每逢下雨,贝茜最喜欢的三叶草地就积聚了一潭水.这意味着草地被水淹没了,并且小草要继续生长还要花相当长一段时间.因此,农夫约翰修建了一 ...
- 【USACO】草地排水
Drainage Ditches 草地排水 usaco 4.2.1描述在农夫约翰的农场上,每逢下雨,Bessie最喜欢的三叶草地就积聚了一潭水.这意味着草地被水淹没了,并且小草要继续生长还要花相当长一 ...
- AC日记——草地排水 codevs 1993
1993 草地排水 USACO 时间限制: 2 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description 在农夫约翰的农场上,每 ...
- 题解 【USACO 4.2.1】草地排水
[USACO 4.2.1]草地排水 Description 在农夫约翰的农场上,每逢下雨,贝茜最喜欢的三叶草地就积聚了一潭水.这意味着草地被水淹没了,并且小草要继续生长还要花相当长一段时间.因此,农夫 ...
- [网络流]Drainage Ditches(草地排水)
Drainage Ditches(草地排水) 题目描述 在农夫约翰的农场上,每逢下雨,贝茜最喜欢的三叶草地就积聚了一潭水.这意味着草地被水淹没了,并且小草要继续生长还要花相当长一段时间.因此,农夫约翰 ...
随机推荐
- struts2 + ajax + json的结合使用,实例讲解
struts2用response怎么将json值返回到页面javascript解析,这里介绍一个struts2与json整合后包的用法. 1.准备工作 ①ajax使用Jquery:jquery-1.4 ...
- Css transition
CSS的transition允许CSS的属性值在一定的时间区间内平滑地过渡.这种效果可以在鼠标单击.获得焦点.被点击或对元素任何改变中触发,并圆滑地以动画效果改变CSS的属性值. <!DOCTY ...
- Debian普通用户添加sudo权限
转自:http://chenpeng.info/html/964 刚安装好的Debian默认还没有sudo功能.1.安装sudo# apt-get install sudo2.修改 /etc/sudo ...
- 草根玩微博 中产玩微信 土豪玩什么?支持Yo的iWatch?
<中国新媒体发展报告(2014)>发布了一些新媒体的使用情况数据,25.6%无收入群体人数在玩微博,32.0%的微信用户属于月收入3000~5000元的中产阶层,那么土豪会玩什么新媒体呢? ...
- Atlas安装及配置
==============linux下快捷键==================ctrl+insert 复制shift +insert 粘贴 输入文件名的前三个字母,按tab键自动补全文件名 在vi ...
- GLUT教程 - 安装
转载:http://www.cnblogs.com/live41/p/3368830.html glut库 - 下载频道 - CSDN.NEThttp://download.csdn.net/down ...
- HDU 1176免费馅饼 DP数塔问题转化
L - 免费馅饼 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- BZOJ 3540 realtime-update 解题
分析一下题意,大约是给定一串牛,然后找到一个跨越距离最长的牛子串使得在这个范围内白牛和花牛一样多. 白牛可以任意涂成花牛. 既然"白牛可以任意涂成花牛",那么我们需要找到一个最长的 ...
- springMVC 上传文件
spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方1.form的enctype=”multipart/form-data” 这个是上传文件必须的2.applicationConte ...
- tesseract3.02识别验证码需要注意的问题
1.安装tesseract3.02后,在命令行里输入tesseract,看能否出现使用方法,不出现则是环境变量问题,可调整其顺序. 2.找到如下文件 C:\Python27\Lib\site-pack ...