【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(草地排水) 题目描述 在农夫约翰的农场上,每逢下雨,贝茜最喜欢的三叶草地就积聚了一潭水.这意味着草地被水淹没了,并且小草要继续生长还要花相当长一段时间.因此,农夫约翰 ...
随机推荐
- 仿51job.com城市选择框特效
650) this.width=650;" border="0" alt="" src="http://img1.51cto.com/att ...
- WPF PopupNonTopmost重写
之前做WPF遇到问题,在网上找到的一个类 public class PopupNonTopmost : System.Windows.Controls.Primitives.Popup { publi ...
- [工具]Mac平台开发几个网络抓包工具(sniffer)
Cocoa Packet Analyzer http://www.tastycocoabytes.com/cpa/ Cocoa Packet Analyzer is a native Mac OS X ...
- C++中构造函数详解及显式调用构造函数
C++构造函数详解及显式调用构造函数 c++类的构造函数详解 一. 构造函 ...
- ssi技术初探
http://blog.sina.com.cn/s/blog_765941620100wiir.html
- PHP程序员,因该养成 7 个面向对象的好习惯
在 PHP 编程早期,PHP 代码在本质上是限于面向过程的.过程代码 的特征在于使用过程构建应用程序块.过程通过允许过程之间的调用提供某种程度的重用. 但是,没有面向对象的语言构造,程序员仍然可以把 ...
- [codeforces 260]B. Ancient Prophesy
[codeforces 260]B. Ancient Prophesy 试题描述 A recently found Ancient Prophesy is believed to contain th ...
- localResizeIMG
http://think2011.net/localResizeIMG/test/ 演示一下 自己试试 点我直接进入演示页面 说明 在客户端压缩好要上传的图片可以节省带宽更快的发送给后端,特别适合在移 ...
- 详解mysql int类型的长度值问题
我的朋友海滨问我mysql在建表的时候int类型后的长度代表什么? 是该列允许存储值的最大宽度吗? 为什么我设置成int(1), 也一样能存10,100,1000呢. 当时我虽然知道int(1),这个 ...
- thinkcentre m8380t黑屏 解决办法
问题: 开机后,显示器上显示“无信号输入”,一直黑屏,但是主机的风扇.硬盘声音都正常. 解决办法: 把vga线等加紧了一些,屏幕还是不亮,然后我关掉插排电源,给cmos放了电,再重启就可以了. 这时需 ...