【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. 图解equals与hashcode方法相等/不相等的互相关系

    图解:比如equals相等的箭头指向hashcode相等,表示equals相等那么必有hashcode相等.而有两个箭头指向别人的表示可能是其中之一,比如hashcode相等,那么有可能equals相 ...

  2. java连接各种数据库代码大全

    1.Oracle8/8i/9i数据库(thin模式)Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();S ...

  3. 新浪微博客户端(13)-使用UIWebView加载OAuth授权界面

    使用UIWebView加载OAuth授权界面 DJOAuthViewController.m #import "DJOAuthViewController.h" @interfac ...

  4. iOS-Auto property synthesis will not synthesize property 'delegate'; it will be implemented by its super

    今天在XCode6.3上面重写TabBar的时候,自定义tabBar的代理遇到的一个问题 在重写tabBar的代理的时候遇到了一个警告. 解决方法: 在.m文件中 警告消失.

  5. stripslashes — 反引用一个引用字符串

    stripslashes (PHP 4, PHP 5) stripslashes — 反引用一个引用字符串 Report a bug  说明 string stripslashes ( string  ...

  6. matlab之waitbar() delete() close()

    matlab之waitbar() delete() close() 三者之间的关系: 在显示某个程序的进度时,用waitbar函数显示进度条,当程序进行完毕时,用close 或 delete函数关闭此 ...

  7. php面试题之四——Linux部分(高级部分)

    四.Linux部分 1.请解释下列10个shell命令的用途(新浪网技术部) top.ps.mv.find.df.cat.chmod.chgrp.grep.wc top:该命令提供了实时对系统处理器状 ...

  8. 将JSON转成DataSet(DataTable)

    方法1: /// <summary> /// 将JSON解析成DataSet只限标准的JSON数据 /// 例如:Json={t1:[{name:'数据name',type:'数据type ...

  9. [Effective JavaScript 笔记]第30条:理解prototype、getPrototypeOf和__ptoto__之间的不同

    原型包括三个独立但相关的访问器.这三个单词都是对单词prototype做了一些变化. C.prototype用于建立由new C()创建的对象的原型 Object.getPrototypeOf(obj ...

  10. [Effective JavaScript 笔记]第52条:数组字面量优于数组构造函数

    js的优雅很大程序要归功于程序中常见的构造块(Object,Function及Array)的简明的字面量语法.字面量是一种表示数组的优雅方法. var a=[1,2,3,5,7,8]; 也可以使用构造 ...