HDU 1532 Drainage Ditches(网络流模板题)
题目大意:就是由于下大雨的时候约翰的农场就会被雨水给淹没,无奈下约翰不得不修建水沟,而且是网络水沟,并且聪明的约翰还控制了水的流速,
本题就是让你求出最大流速,无疑要运用到求最大流了。题中m为水沟数,n为水沟的顶点,接下来Si,Ei,Ci分别是水沟的起点,终点以及其容量。求源点1到终点m的最大流速。
EK模板
#include<iostream>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
#define MaxInt 0x3f3f3f3f
using namespace std;
int m,n,f[210][210],cap[210][210],a[210],p[210];
queue<int> que;
int Edmond_Karp(int s,int t)
{
int flow=0;
while(1){//BFS找增广路
memset(a,0,sizeof(a));
a[s]=MaxInt;
que.push(s);
while(!que.empty()){
int u=que.front();
que.pop();
for(int v=1;v<=m;v++){//m为节点个数
if(!a[v]&&cap[u][v]>f[u][v]){
p[v]=u;//记录v的前驱
que.push(v);
a[v]=min(a[u],cap[u][v]-f[u][v]);//a[v]为s-v路径上的最小流量
}
}
}
if(a[t]==0) break;//找不到,则当前已是最大流量
for(int v=t;v!=s;v=p[v])//从汇点往回走
{
f[p[v]][v]+=a[t];//更新正向流量
f[v][p[v]]-=a[t];//更新反向流量
}
flow+=a[t];//更新从s流出的总流量
}
return flow;
}
int main()
{
int u,v,i,c,ans;
while(scanf("%d%d",&n,&m)!=EOF){
memset(cap,0,sizeof(cap));
memset(f,0,sizeof(f));
for(i=1;i<=n;i++){
scanf("%d%d%d",&u,&v,&c);
cap[u][v]+=c;//注意重边
}
ans=Edmond_Karp(1,m);
printf("%d\n",ans);
}
return 0;
}
Dinic模板
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
#define inf 0x7fffffff
#define min(a,b) a<b?a:b
int n,m,u,v,w;
int level[];
struct Dinic
{
int cap;
int flow;
}edge[][];
//构造层次网络
bool Dinic_bfs(int s,int t)
{
queue<int> que;
memset(level,,sizeof(level));//初始化所有顶点的层次为 0
que.push(s);
level[s]=;
while(!que.empty()){
int u=que.front();
que.pop();
for(int v=;v<=m;v++){//即顶点v未被访问过,只考虑残量网络中顶点u,v是否存在边
if(!level[v]&&edge[u][v].flow<edge[u][v].cap){
level[v]=level[u]+;
que.push(v);
}
}
}
return level[t];
}
//u为当前节点,a为当前最小残量进行多路增广
int Dinic_dfs(int u,int a)
{
int flow=;
if(u==m||a==)
return a;
for(int v=;v<=m;v++){
if(level[u]+==level[v]){
if(edge[u][v].cap>edge[u][v].flow){
int r=Dinic_dfs(v,min(a,edge[u][v].cap-edge[u][v].flow));
edge[u][v].flow+=r;
edge[v][u].flow-=r;
a-=r,flow+=r;
}
}
}
return flow;
}
//求出最大流
int Dinic()
{
int flow=;
while(Dinic_bfs(,m))
flow+=Dinic_dfs(,inf);
return flow;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF){
memset(edge,,sizeof(edge));
for(int i=;i<=n;i++){
scanf("%d%d%d",&u,&v,&w);
edge[u][v].cap+=w;//防止重边
}
printf("%d\n",Dinic());
}
return ;
}
HDU 1532 Drainage Ditches(网络流模板题)的更多相关文章
- POJ 1273:Drainage Ditches 网络流模板题
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 63339 Accepted: 2443 ...
- USACO 4.2 Drainage Ditches(网络流模板题)
Drainage DitchesHal Burch Every time it rains on Farmer John's fields, a pond forms over Bessie's fa ...
- hdu 1532 Drainage Ditches(网络流)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1532 题目大意是:农夫约翰要把多个小池塘的水通过池塘间连接的水渠排出去,从池塘1到池塘M最多可以排多少 ...
- hdu 1532 Drainage Ditches(最大流模板题)
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1532 Drainage Ditches (网络流)
A - Drainage Ditches Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64 ...
- HDU 1532 Drainage Ditches (最大网络流)
Drainage Ditches Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) To ...
- HDU 1532 Drainage Ditches(最大流 EK算法)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1532 思路: 网络流最大流的入门题,直接套模板即可~ 注意坑点是:有重边!!读数据的时候要用“+=”替 ...
- HDU 1532 Drainage Ditches 分类: Brush Mode 2014-07-31 10:38 82人阅读 评论(0) 收藏
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- poj 1273 && hdu 1532 Drainage Ditches (网络最大流)
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 53640 Accepted: 2044 ...
随机推荐
- 初始Dubbo
1. Dubbo是什么? Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案.简单的说,dubbo就是个服务框架,如果没有分布式的需求,其实是不需 ...
- Vim相关优化和配置
升级pythonwget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgztar -xvf Python-3.6.5.tgzcd Pyt ...
- 四、Android Studio使用——什么样的Project都能导入Studio
1 导入Github源码(别人的Studio工程) 导入之前先看下(导入的工程)gradle-wrapper.properties文件里的gradle-版本是多少. 然后如果这个文件中的版本和你AS工 ...
- PMP 质量管理新7张图
亲和图.关联图.系统图.矩阵图.网络图.pdpc.矩阵数据分析法
- LeetCode 笔记系列11 First Missing Positive [为什么我们需要insight]
题目: Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2 ...
- java -jar后台启动
nohup java -jar XX.jar >logs.log &
- Ubuntu安装atom
sudo add-apt-repository ppa:webupd8team/atom sudo apt-get update sudo apt-get install atom 安装的时如果报错, ...
- HUD2647 Reward_反向建图拓扑排序
HDU2647 Reward 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题意:老板要发奖金了,有n个人,给你m对数,类似a b,这样的一对 ...
- Chrome cookies folder
w本地存储数据2种形式. http://superuser.com/questions/292952/chrome-cookies-folder-in-windows-7 chrome://setti ...
- nodejs(三)上之express
express 简介 Express 是一个简洁而灵活的 node.js Web应用框架, 提供了一系列强大特性帮助你创建各种 Web 应用,和丰富的 HTTP 工具. 使用 Express 可以快速 ...