Drainage Ditches(Dinic最大流)
http://poj.org/problem?id=1273
用Dinic求最大流的模板题,注意会有重边。
邻接矩阵建图
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn = ;
const int INF = 0x3f3f3f3f;
int flow[maxn][maxn];//残量
int m,n;
int dis[maxn];
int bfs()//按层数“建”图,就是对每层的点用dis标记它到源点的层数
{
queue<int>que;
memset(dis,-,sizeof(dis));
while(!que.empty())
que.pop();
dis[] = ;
que.push(); while(!que.empty())
{
int u = que.front();
que.pop(); for(int i = ; i <= n; i++)
{
if(flow[u][i] > && dis[i] < )
{
dis[i] = dis[u]+;
que.push(i);
}
}
} if(dis[n] > )
return ;
else return ;
}
//dfs寻找路径上的最小流量
int dfs(int s, int mf)
{
int a;
if(s == n)
return mf;
for(int i = ; i <= n; i++)
{
if(dis[i] == dis[s] + && flow[s][i] > && (a = dfs(i,min(mf,flow[s][i]))))
{
flow[s][i] -= a;
flow[i][s] += a;
return a;
}
}
return ;
} int main()
{
while(~scanf("%d %d",&m,&n))
{
int u,v,w;
memset(flow,,sizeof(flow));
for(int i = ; i < m; i++)
{
scanf("%d %d %d",&u,&v,&w);
flow[u][v] += w;
}
int ans = ,res;
while(bfs())//bfs寻找源点到汇点是否有路
{
res = dfs(,INF);
if(res > )
ans += res;
}
printf("%d\n",ans);
}
return ; }
邻接表建图
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn = ;
const int INF = 0x3f3f3f3f;
int m,n,cnt;
struct node
{
int u,v,w;
int next;
}edge[maxn];
int p[maxn];
int dis[maxn];
void add(int u, int v, int w)
{ edge[cnt].u = u;
edge[cnt].v = v;
edge[cnt].w = w;
edge[cnt].next = p[u];
p[u] = cnt++; edge[cnt].u = v;
edge[cnt].v = u;
edge[cnt].w = ;
edge[cnt].next = p[v];
p[v] = cnt++;
} int bfs()
{
queue<int> que;
while(!que.empty())
que.pop();
memset(dis,-,sizeof(dis));
dis[] = ;
que.push(); while(!que.empty())
{
int u = que.front();
que.pop(); for(int i = p[u]; i != -; i = edge[i].next)
{
if(edge[i].w > && dis[ edge[i].v ] < )
{
dis[ edge[i].v ] = dis[u] + ;
que.push(edge[i].v);
}
}
}
if(dis[n] > )
return ;
else return ;
} int dfs(int s, int mf)
{
if(s == n)
return mf;
int a;
int tf = ;
for(int i = p[s]; i != -; i = edge[i].next)
{
int v = edge[i].v;
int w = edge[i].w;
if(dis[v] == dis[s] + && w > && (a = dfs(v,min(mf,w))))
{
edge[i].w -= a;
edge[i^].w += a;
return a;
}
}
if(!tf)//若找不到小流,从这个点到不了汇点,把这个点从分层图删去
dis[s] = -;
return tf;
} int main()
{
while(~scanf("%d %d",&m,&n))
{
int u,v,w;
cnt = ;
memset(p,-,sizeof(p));
for(int i = ; i < m; i++)
{
scanf("%d %d %d",&u,&v,&w);
add(u,v,w);
}
int ans = ;
int res;
while(bfs())
{
res = dfs(,INF);
if(res > )
ans += res;
}
printf("%d\n",ans);
} return ;
}
Drainage Ditches(Dinic最大流)的更多相关文章
- poj1273 Drainage Ditches Dinic最大流
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 76000 Accepted: 2953 ...
- POJ1273&&Hdu1532 Drainage Ditches(最大流dinic) 2017-02-11 16:28 54人阅读 评论(0) 收藏
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- poj 1273 (nyoj 323) Drainage Ditches : 最大流
点击打开链接 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 49648 Accepte ...
- poj 1273 Drainage Ditches(最大流)
http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Subm ...
- 2018.07.06 POJ1273 Drainage Ditches(最大流)
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Description Every time it rains on Farmer J ...
- poj 1273 && hdu 1532 Drainage Ditches (网络最大流)
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 53640 Accepted: 2044 ...
- HDU 1532||POJ1273:Drainage Ditches(最大流)
pid=1532">Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/327 ...
- POJ 1273 Drainage Ditches -dinic
dinic版本 感觉dinic算法好帅,比Edmonds-Karp算法不知高到哪里去了 Description Every time it rains on Farmer John's fields, ...
- poj 1273 Drainage Ditches【最大流入门】
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 63924 Accepted: 2467 ...
随机推荐
- unity3d最新面试题与参考答案汇总
1.在类的构造函数前加上static会报什么错?为什么? 构造函数格式为 public+类名,如果加上static会报错(静态构造函数不能有访问修饰符)原因:静态构造函数不允许访问修饰符,也不接受任何 ...
- Unity3D用vistual studio打卡C#脚本卡死解决
小黑已经跟我3年了,不仅很喜欢他方正酷黑的外表,而且稳定性绝对没的说.我已经3年没有重装过系统了,而且现在装了3个系统!虽然小黑很适合程序员,但是他最大的缺点就是做设计比较吃力,显卡512M.像uni ...
- 20160127 linux 学习笔记
Linux学习笔记第一天 Linux基本介绍 Linux的起源和发展: 简单说linux是一种操作系统,可以安装在包括服务器.个人电脑,乃至PDA.手机.打印机等各类设备中. 起源: Linux起源于 ...
- 《Think in Java》读书笔记一:对象
一.抽象过程 Alan Kay曾经总结了第一个成功的面向对象语言.同时也是Java所基于的语言之一的SmallTalk的五个基本特性,这些特性表现了一种纯粹的面向对象程序设计方式: 1.万物皆为对象. ...
- oc for in 的时候nsscanner: nil string argument
今天偶然发现,oc for in 动态的给一数组加东西,然后嵌套for in 会报nsscanner: nil string argument. 换成for循环就好了,暂时还没找到原因
- Java 编译解释
JDK提供的主要开发工具有:编译程序,解释执行程序.调试程序.Applet执行程序.文档管理程序.包管理程序等. 1.编译程序:javac.exe,对应的javac命令将Java源程序转换为字节码. ...
- (hdu)5652 India and China Origins 二分+dfs
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5652 Problem Description A long time ago there ...
- Parameters
Quote from: http://ss64.com/nt/syntax-args.html Parameters A parameter (or argument) is any value pa ...
- linux管理文件系统指令
就一个基本的linux系统而言,其计算机硬盘只能有三个分区:一个交换分区(用于处理物理内存存不下的信息),一个包含引导转载程序的内核的启动分区,一个根文件系统分区,后两个常采用 ext3文件系统 与e ...
- 【原创】解决国内Android SDK无法更新问题更新
使用代理,推荐使用shadowsock 在SDK Manage的tools-options填好代理 服务器地址127.0.0.1 端口1080