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 ...
随机推荐
- SpringMvc的过滤器。
一:过滤器的原理: 过滤器放在web资源之前,可以在请求抵达它所应用的web资源(可以是一个Servlet.一个Jsp页面,甚至是一个HTML页面)之前截获进入的请求,并且在它返回到客户之前截获输出请 ...
- shell脚本学习(6)awk 编排字段
awk能取出文本字段重新编排 1 awk的用法 awk ‘program’ [file] 2 其中program 可以写成 ‘parrtern {action}’ pattern 或 actio ...
- 08-图7 公路村村通(30 分)Prim
现有村落间道路的统计数据表中,列出了有可能建设成标准公路的若干条道路的成本,求使每个村落都有公路连通所需要的最低成本. 输入格式: 输入数据包括城镇数目正整数N(≤1000)和候选道路数目M(≤3N) ...
- operator函数操作符
函数操作数() 可以实现将对象当函数使用. class Square{ public: double operator()(double x)const{return x*x;} };
- delphi 加入超链接
delphi 加入超链接//在uses中加入ShellAPI//通过该lpFile参数可以实现链接到主页或ftp站点 ShellExecute(handle,nil,pchar('http://www ...
- Borůvka (Sollin) 算法求 MST 最小生成树
基本思路: 用定点数组记录每个子树的最近邻居. 对于每一条边进行处理: 如果这条边连成的两个顶点同属于一个集合,则不处理,否则检测这条边连接的两个子树,如果是连接这两个子树的最小边,则更新 (合并). ...
- vue数据渲染、条件判断及列表循环
1.数据渲染 {{msg}} <template> <div id="app"> {{msg}} </div> </template&g ...
- 用K-近邻算法分类和回归
import numpy as npfrom matplotlib import pyplot as plt X_train = np.array([ [158, 64], [170, 66], [1 ...
- 基于Springmvc的登录权限拦截器
1.什么是拦截器 拦截器是指通过统一拦截从浏览器发往服务端的请求来完成功能的增强. 使用场景:解决请求的共性问题(如:乱码问题,权限验证问题等) 2.拦截器的基本工作原理 springmvc可以通过配 ...
- slideshare原本是一个专业的幻灯片存储与展示的网站
slideshare就是其中一个.slideshare原本是一个专业的幻灯片存储与展示的网站,它支持扩展名为ppt.pps和odp三种格式的幻灯片,用户上传成功以后slideshare会提供给用户一个 ...