POJ1273:Drainage Ditches(最大流入门 EK,dinic算法)
http://poj.org/problem?id=1273
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
Output
Sample Input
5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10
Sample Output
50 EK算法:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <queue>
#define inf 0x3f3f3f3f
using namespace std;
int map[][],n,m,v[],pre[];
int bfs(int s,int t)
{
queue<int>q;
q.push(s);
memset(pre,-,sizeof(pre));
memset(v,,sizeof(v));
pre[s]=s;
v[s]=;
while(!q.empty())
{
int w=q.front();
q.pop();
for(int i=; i<=n; i++)
{
if(map[w][i]&&!v[i])
{
pre[i]=w;
v[i]=;
if(i==t)
{
return ;
}
q.push(i);
}
}
}
return ;
}
void EK(int s,int t)
{
int ans=,minx;
while(bfs(s,t)==)
{
minx=inf;
for(int i=t; i!=s; i=pre[i])
{
minx=min(minx,map[pre[i]][i]);
}
for(int i=t; i!=s; i=pre[i])
{
map[pre[i]][i]-=minx;
map[i][pre[i]]+=minx;
}
ans+=minx;
}
printf("%d\n",ans);
return ;
}
int main()
{
int xx,yy,zz;
while(scanf("%d%d",&m,&n)!=EOF)
{
memset(map,,sizeof(map));
while(m--)
{
scanf("%d%d%d",&xx,&yy,&zz);
map[xx][yy]+=zz;
}
EK(,n);
}
return ;
}
dinic算法:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <queue>
#define inf 0x3f3f3f3f
using namespace std;
int map[][],dis[];
int m,n;
int bfs(int s,int t)
{
memset(dis,-,sizeof(dis));
dis[s]=;
queue<int>q;
q.push(s);
while(!q.empty())
{
int y=q.front();
q.pop();
for(int i=; i<=n; i++)
{
if(dis[i]==-&&map[y][i])
{
dis[i]=dis[y]+;
q.push(i);
}
}
}
if(dis[t]>) return ;
return ;
}
int dinic(int s,int maxt)
{
if(s==n) return maxt;
int a,sum=maxt;
for(int i=; i<=n&∑ i++)
{
if(dis[i]==dis[s]+&&map[s][i]>)
{
a=dinic(i,min(sum,map[s][i]));
map[s][i]-=a;
map[i][s]+=a;
sum-=a;
}
}
return maxt-sum;
}
int main()
{
int x,y,z,ans;
while(scanf("%d%d",&m,&n)!=EOF)
{
ans=;
memset(map,,sizeof(map));
while(m--)
{
scanf("%d%d%d",&x,&y,&z);
map[x][y]+=z;
}
while(bfs(,n)==)
{
ans+=dinic(,inf);
}
printf("%d\n",ans);
}
return ;
}
POJ1273:Drainage Ditches(最大流入门 EK,dinic算法)的更多相关文章
- POJ-1273 Drainage Ditches 最大流Dinic
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 65146 Accepted: 25112 De ...
- 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 Drainage Ditches(最大流基础题)
题目链接: Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 67475 Accepted ...
- poj1273 Drainage Ditches (最大流板子
网络流一直没学,来学一波网络流. https://vjudge.net/problem/POJ-1273 题意:给定点数,边数,源点,汇点,每条边容量,求最大流. 解法:EK或dinic. EK:每次 ...
- [poj1273]Drainage Ditches(最大流)
解题关键:最大流裸题 #include<cstdio> #include<cstring> #include<algorithm> #include<cstd ...
- poj1273 Drainage Ditches Dinic最大流
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 76000 Accepted: 2953 ...
- Poj 1273 Drainage Ditches(最大流 Edmonds-Karp )
题目链接:poj1273 Drainage Ditches 呜呜,今天自学网络流,看了EK算法,学的晕晕的,留个简单模板题来作纪念... #include<cstdio> #include ...
- POJ 1273 - Drainage Ditches - [最大流模板题] - [EK算法模板][Dinic算法模板 - 邻接表型]
题目链接:http://poj.org/problem?id=1273 Time Limit: 1000MS Memory Limit: 10000K Description Every time i ...
- 2018.07.06 POJ1273 Drainage Ditches(最大流)
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Description Every time it rains on Farmer J ...
随机推荐
- mysql数据库使用mysqldump工具针对一个数据库备份,使用--databases选项与不使用该参数的区别
需求描述: 今天在做mysqldump备份某个数据库的试验,在备份某个数据库的时候可以使用 --databases参数,也可以直接进行某个数据库的备份,那么这里记录下两者的区别 操作过程: 1.使用- ...
- K - problem 问题
Leetcode 有几个题目, 分别是 2sum, 3sum(closest), 4sum 的求和问题和 single Number I II, 这些题目难点在于用最低的时间复杂度找到结果 2-sum ...
- python2.0_s12_day10_Twsited异步网络框架
Twsited异步网络框架 Twisted是一个事件驱动的网络框架,其中包含了诸多功能,例如:网络协议.线程.数据库管理.网络操作.电子邮件等. Package application Configu ...
- mysql 小数处理
1)四舍五入,保留小数 使用ROUND函数(注意不要使用FORMAT函数,FORMAT函数返回值带有逗号,赋值时会出现错误) 示例:保留两位小数 ROUND(price,2) 2)向上取整 CEIL ...
- docker rmi 详解
docker rmi 用于删除指定的镜像,常见用法如下: [root@localhost ~]$ docker rmi centos # 根据 REPOSITORY 来删除镜像 [root@local ...
- jq如何实现内容的无限滚动
html: <div> <ul> <li>1</li> <li>2</li> <li>3</li> &l ...
- mysql concat
CONCAT_WS() 代表 CONCAT With Separator ,是CONCAT()的特殊形式. 第一个参数是其它参数的分隔符.分隔符的位置放在要连接的两个字符串之间. 分隔符可以是一个字符 ...
- phpsession配置
1. 介绍 1.1 作用: 主要用于服务器端的会话保持. 1.2 结构: Session分以下几部分: 1)Session id 用户Session的唯一标识(随机生成,具有唯一性,随机性) 2)Se ...
- Go基础---->go的基础学习(二)
这里记录的是go中函数的一些基础知识.道听途说终是浅,身临其境方知深. go的基础知识 一.go中函数的基础使用 package main import ( "fmt" " ...
- HTTP/2探索第二篇——工具及应用
版权声明:本文由张浩然原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/88 来源:腾云阁 https://www.qclou ...