Drainage Ditches(最大流)
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 64044 | Accepted: 24718 |
Description
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
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
Sample Input
5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10
Sample Output
50
代码:醉醉的超时。。。入门题。两种方法:
代码1:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
#define mem(x,y) memset(x,y,sizeof(x))
#include<queue>
using namespace std;
typedef long long LL;
const int INF=0x3f3f3f3f;
const int MAXN=;
int map[MAXN][MAXN];
queue<int>dl;
int vis[MAXN],pre[MAXN];
int N;
bool bfs(int s,int e){
mem(vis,);
mem(pre,);
while(!dl.empty())dl.pop();
vis[s]=;dl.push(s);
int a;
while(!dl.empty()){
a=dl.front();dl.pop();
if(a==e)return true;
for(int i=;i<=N;i++){
if(!vis[i]&&map[a][i]){
vis[i]=;
dl.push(i);
pre[i]=a;
}
}
}
return false;
}
LL maxflow(int s,int e){
LL flow=;
while(bfs(s,e)){
int r=e;
int temp=INF;
while(r!=s){
temp=min(temp,map[pre[r]][r]);
r=pre[r];
}
r=e;
while(r!=s){
map[pre[r]][r]-=temp;
map[r][pre[r]]+=temp;
r=pre[r];//这句话不能少。。
}
flow+=temp;
}
return flow;
}
int main(){
int M;
while(~scanf("%d%d",&M,&N)){
mem(map,);
int u,v,w;
while(M--){
scanf("%d%d%d",&u,&v,&w);
map[u][v]+=w;
}
printf("%I64d\n",maxflow(,N));
}
return ;
}
代码2:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
#define mem(x,y) memset(x,y,sizeof(x))
using namespace std;
const int INF=0x3f3f3f3f;
typedef long long LL;
const int MAXN=210;
const int MAXM=2020;
int head[MAXM];
int vis[MAXN],dis[MAXN];
int edgnum;
struct Node{
int from,to,next,cup,flow;
};
Node edg[MAXM];
queue<int>dl;
void initial(){
mem(head,-1);edgnum=0;
}
void add(int u,int v,int w){
Node E={u,v,head[u],w,0};
edg[edgnum]=E;
head[u]=edgnum++;
E={v,u,head[v],0,0};
edg[edgnum]=E;
head[v]=edgnum++;
}
bool bfs(int s,int e){
mem(vis,0);mem(dis,-1);
while(!dl.empty())dl.pop();
vis[s]=1;dis[s]=0;dl.push(s);
while(!dl.empty()){
int u=dl.front();dl.pop();
for(int i=head[u];i!=-1;i=edg[i].next){
Node v=edg[i];
if(!vis[v.to]&&v.cup>v.flow){//应该是>
vis[v.to]=1;
dis[v.to]=dis[u]+1;
if(v.to==e)return true;
dl.push(v.to);
}
}
}
return false;
}
int dfs(int x,int la,int e){
if(x==e||la==0)return la;
int temp;
LL flow=0;
for(int i=head[x];i!=-1;i=edg[i].next){
Node &v=edg[i];
if(dis[v.to]==dis[x]+1&&(temp=dfs(v.to,min(la,v.cup-v.flow),e))>0){//这里也应该要>
v.flow+=temp;
edg[i^1].flow-=temp;
la-=temp;
flow+=temp;
if(la==0)break;//这个要判断
}
}
return flow;
}
LL maxflow(int s,int e){
LL flow=0;
while(bfs(s,e)){
flow+=dfs(s,INF,e);
}
return flow;
}
int main(){
int N,M;
while(~scanf("%d%d",&N,&M)){
initial();
int u,v,w;
while(N--){
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
}
printf("%I64d\n",maxflow(1,M));
}
return 0;
}
Drainage Ditches(最大流)的更多相关文章
- Poj 1273 Drainage Ditches(最大流 Edmonds-Karp )
题目链接:poj1273 Drainage Ditches 呜呜,今天自学网络流,看了EK算法,学的晕晕的,留个简单模板题来作纪念... #include<cstdio> #include ...
- poj 1273 Drainage Ditches 最大流入门题
题目链接:http://poj.org/problem?id=1273 Every time it rains on Farmer John's fields, a pond forms over B ...
- POJ 1273 || HDU 1532 Drainage Ditches (最大流模型)
Drainage DitchesHal Burch Time Limit 1000 ms Memory Limit 65536 kb description Every time it rains o ...
- TZOJ 4085 Drainage Ditches(最大流)
描述 Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. Th ...
- POJ 1273 - Drainage Ditches - [最大流模板题] - [EK算法模板][Dinic算法模板 - 邻接表型]
题目链接:http://poj.org/problem?id=1273 Time Limit: 1000MS Memory Limit: 10000K Description Every time i ...
- HDU1532 Drainage Ditches —— 最大流(sap算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1532 Drainage Ditches Time Limit: 2000/1000 MS (Java/ ...
- poj-1273 Drainage Ditches(最大流基础题)
题目链接: Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 67475 Accepted ...
- hdu 1532 Drainage Ditches(最大流)
Drainage Dit ...
- POJ-1273 Drainage Ditches 最大流Dinic
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 65146 Accepted: 25112 De ...
- POJ1273:Drainage Ditches(最大流入门 EK,dinic算法)
http://poj.org/problem?id=1273 Description Every time it rains on Farmer John's fields, a pond forms ...
随机推荐
- TCP各种连接状态注释
TCP各种连接状态注释 连接进程是通过一系列状态表示的,这些状态有:LISTEN,SYN-SENT,SYN-RECEIVED,ESTABLISHED,FIN-WAIT-1,FIN-WAIT-2,CL ...
- Windows 配置JAVA的环境变量
Java是由Sun公司开发的一种应用于分布式网络环境的程序设计语言,Java语言拥有跨平台的特性,它编译的程序能够运行在多种操作系统平台上,可以实现“一次编写,到处运行”的强大功能. 工具/原料 JD ...
- 两台linux机器文件传输之scp
0.写在前面:一定要注意我们是否有源文件的读权限,是否有目标文件夹的写权限!没有的话要先把权限设置好! *.设置权限的方法:切换到有权限操作文件或文件夹的用户,利用chmod命令修改权限 1.安装: ...
- python自学笔记(十一)关于函数及书写格式
1.函数是抽象的第一步 1.1 有关高压锅 1.2 函数是抽象出来的结构,是总结,是方法 1.3 多用函数 2.如何定义函数 2.1 def是关键词, ...
- HTML+CSS笔记 CSS中级 缩写入门
盒子模型代码简写 回忆盒模型时外边距(margin).内边距(padding)和边框(border)设置上下左右四个方向的边距是按照顺时针方向设置的:上右下左. 语法: margin:10px 15p ...
- linux文件系统操作——底层文件访问
在不使用标准I/O的情况下,使用write,read,open实现对文件的复制操作,这些调用都是直接使用底层系统调用,完成从用户代码到内核代码的切换,消耗大量的系统资源,今天对此进行研究主要是 ...
- wsdl透明解析
1.逐个分析wsdl文件中的元素: <types>:数据类型定义的容器,一般使用 xml schema类型系统. <message>:通信消息的数据结构的抽象化定义,使用< ...
- javascript变量说明
定义变量 var test = "hi"; 在这个例子中,声明了变量 test,并把它的值初始化为 "hi"(字符串).由于 ECMAScript 是弱类型的, ...
- 转: css3: display:box详解
示例见: css3: flexbox (BTW: blog不能包含iframe script真不方便啊~~) display:box;box-flex是css3新添加的盒子模型属性,它的出现可以解决 ...
- angulajs 当input使用 bootstrap的email类型时,如果是无效的email格式,则ng-model无效的情况
当使用bootstrap的如下input时 <input type="email" ng-model="userid"> 如果输入的内容是无效的em ...