POJ 1273 Drainage Ditches (网络流Dinic模板)
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 网络流Dinic模板
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
#include <string>
#include <cstring>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = ;
int c[maxn][maxn];
int dep[maxn];
int cur[maxn];
int n,m;
void pt()
{
for (int i=;i<=n;++i){
for (int j=;j<=n;++j)
printf("%d ",c[i][j]);
printf("\n");
}
printf("==================\n");
}
int bfs (int s,int t)
{
memset(dep,-,sizeof dep);
queue<int> q;
while (!q.empty()) q.pop();
dep[s] = ;
q.push(s);
while (!q.empty()){
int u = q.front();
q.pop();
for (int v=;v<=n;++v){
if (c[u][v]>&&dep[v]==-){//能到达该节点的条件是这条边有流量,而且这个点没有被访问
dep[v] = dep[u]+;
q.push(v);
}
}
}
return dep[t]!=-;
}
int dfs (int u,int mi,int t)
{
if (u==t)
return mi;
int tmp;
for (int &v=cur[u];v<=n;++v){//
if (c[u][v]>&&dep[v]==dep[u]+&&(tmp=dfs(v,min(mi,c[u][v]),t))){//下一节点的深度是当前节点+1
c[u][v]-=tmp;
c[v][u]+=tmp;
return tmp;
}
}
return ;//别忘写返回0!!!
}
int dinic ()
{
int ans = ;
int tmp;
while (bfs(,n)){//每次按照深度建立分层图,这样每次dfs的时候下一节点的深度是当前节点+1
while (){
for (int i=;i<maxn;++i) cur[i]=;//当前弧优化
tmp = dfs(,inf,n);
//printf("%d\n",tmp);
if (tmp==)
break;
//pt();
ans+=tmp;
}
}
return ans;
}
int main()
{
//freopen("de.txt","r",stdin);
while (~scanf("%d%d",&m,&n)){
memset(c,,sizeof c);
for (int i=;i<m;++i){
int u,v,cap;
scanf("%d%d%d",&u,&v,&cap);
c[u][v]+=cap;
}
printf("%d\n",dinic());
}
return ;
}
#include <iostream>#include <cstdio>#include <cmath>#include <algorithm>#include <queue>#include <string>#include <cstring>using namespace std;const int inf = 0x3f3f3f3f;const int maxn = 220;int c[maxn][maxn];int dep[maxn];int cur[maxn];int n,m;void pt(){ for (int i=1;i<=n;++i){ for (int j=1;j<=n;++j) printf("%d ",c[i][j]); printf("\n"); } printf("==================\n");}int bfs (int s,int t){ memset(dep,-1,sizeof dep); queue<int> q; while (!q.empty()) q.pop(); dep[s] = 0; q.push(s); while (!q.empty()){ int u = q.front(); q.pop(); for (int v=1;v<=n;++v){ if (c[u][v]>0&&dep[v]==-1){//能到达该节点的条件是这条边有流量,而且这个点没有被访问 dep[v] = dep[u]+1; q.push(v); } } } return dep[t]!=-1;}int dfs (int u,int mi,int t){ if (u==t) return mi; int tmp; for (int &v=cur[u];v<=n;++v){// if (c[u][v]>0&&dep[v]==dep[u]+1&&(tmp=dfs(v,min(mi,c[u][v]),t))){//下一节点的深度是当前节点+1 c[u][v]-=tmp; c[v][u]+=tmp; return tmp; } } return 0;//别忘写返回0!!!}int dinic (){ int ans = 0; int tmp; while (bfs(1,n)){//每次按照深度建立分层图,这样每次dfs的时候下一节点的深度是当前节点+1 while (1){ for (int i=0;i<maxn;++i) cur[i]=1;//当前弧优化 tmp = dfs(1,inf,n); //printf("%d\n",tmp); if (tmp==0) break; //pt(); ans+=tmp; } } return ans;}int main(){ //freopen("de.txt","r",stdin); while (~scanf("%d%d",&m,&n)){ memset(c,0,sizeof c); for (int i=0;i<m;++i){ int u,v,cap; scanf("%d%d%d",&u,&v,&cap); c[u][v]+=cap; } printf("%d\n",dinic()); } return 0;}
POJ 1273 Drainage Ditches (网络流Dinic模板)的更多相关文章
- POJ 1273 - Drainage Ditches - [最大流模板题] - [EK算法模板][Dinic算法模板 - 邻接表型]
题目链接:http://poj.org/problem?id=1273 Time Limit: 1000MS Memory Limit: 10000K Description Every time i ...
- poj 1273 Drainage Ditches 网络流最大流基础
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 59176 Accepted: 2272 ...
- POJ 1273 Drainage Ditches 网络流 FF
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 74480 Accepted: 2895 ...
- poj 1273 Drainage Ditches (网络流 最大流)
网络流模板题. ============================================================================================ ...
- POJ 1273 Drainage Ditches | 最大流模板
#include<cstdio> #include<algorithm> #include<cstring> #include<queue> #defi ...
- poj 1273 Drainage Ditches(最大流)
http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Subm ...
- POJ 1273 Drainage Ditches
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 67387 Accepted: 2603 ...
- POJ 1273 Drainage Ditches (网络最大流)
http://poj.org/problem? id=1273 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Sub ...
- POJ 1273 Drainage Ditches(网络流dinic算法模板)
POJ 1273给出M条边,N个点,求源点1到汇点N的最大流量. 本文主要就是附上dinic的模板,供以后参考. #include <iostream> #include <stdi ...
随机推荐
- 威胁预警|Solr velocity模板注入远程命令执行已加入watchbog武器库,漏洞修补时间窗口越来越短
概述 近日,阿里云安全团队监测到挖矿团伙watchbog更新了其使用的武器库,增加了最新Solr Velocity 模板注入远程命令执行漏洞的攻击方式,攻击成功后会下载门罗币挖矿程序进行牟利.建议用户 ...
- <三剑客> 老二:sed命令用法
sed命令的用法: sed是一种流编辑器,它是文本处理中非常中的工具,能够完美的配合正则表达式使用,功能不同凡响.处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space ...
- Dllregisterserver调用失败解决方法
在做一个注册com组件时,出现这样的情况 出现这个错误一般是和权限问题有关,命令提示符需要以管理员权限运行才可以注册成功. 最简单的解决方法就是: 在开始菜单右击—>命令提示符(管理员)(A) ...
- 26 October in 614
Practice tower 有 \(N\,(2\le N\le 600000)\) 块砖,要搭一个 \(N\) 层的塔,要求:如果砖 \(A\) 在砖 \(B\) 上面,那么 \(A\) 不能比 \ ...
- 关于设置shadowPath的重要性
这是超级容易添加阴影到iOS中的任何视图.所有您需要做的是 添加QuartzCore框架到项目中(如果不存在的话) 导入QuartzCore到您的执行文件 添加一行如[myView.layer set ...
- java并发编程笔记(九)——多线程并发最佳实践
java并发编程笔记(九)--多线程并发最佳实践 使用本地变量 使用不可变类 最小化锁的作用域范围 使用线程池Executor,而不是直接new Thread执行 宁可使用同步也不要使用线程的wait ...
- Windows 08R2_AD图文详解
目录 目录 软件环境 Active Directory域服务 AD的应用 创建ADDS域 使用Windows窗口来创建ADDS域控制器 使用Powershell来创建ADDS域控制器 检查ADDC域控 ...
- mysql null 值查询问题
我在开发公司内部的一个项目时遇到一个问题:select student_quality_id from STUDENT_QUALITY where mark_status=0 and batch_st ...
- hdu1158 Employment Planning(dp)
题目传送门 Employment Planning Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- shell数学运算
shell的数学运算 branches@ubuntu:~$ var1=$[ * ] branches@ubuntu:~$ echo $var1 branches@ubuntu:~$ var2=$[$v ...