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 ...
随机推荐
- import com.sun.image.codec.jpeg.JPEGCodec不通过 Eclipse找不到包
Eclipse默认把这些受访问限制的API设成了ERROR.只要把Windows-Preferences-Java-Complicer-Errors/Warnings里面的Deprecated and ...
- AppDelegate 方法详解
iOS 程序启动时总会调用application:didFinishLaunchingWithOptions:,其中第二个参数launchOptions为NSDictionary类型的对象,里面存储有 ...
- use mkisofs 重新打包beini,tinycore linux
mkisofs -r -J -V Beini-Custom -v --boot-info-table --boot-load-size 4 -b boot/isolinux/isolinux.bin ...
- javax.el.PropertyNotFoundException: Property 'aDesc' not found on type
这个问题是是在我使用jeesite自动代码是产生的,原因是实体类的属性命名规范不合格,我在网上看到类的属性前三个字母不能出现大写 解决办法:将类的属性大小写改一下
- Gradle教程之任务管理
简要概述依赖管理 不算完美的依赖管理技术 自动管理依赖的重要性 自动依赖管理面临的挑战 声明依赖 外部模块依赖 文件依赖 配置远程仓库 这一章我将介绍Gradle对依赖管理的强大支持,学习依赖分组和定 ...
- vim plugin 原理
vim 个性化设置与功能扩展均通过 script 来实现,这种 script 又叫 plugin.plugin 是 vim 的核心与精髓. 最常用的配置文件 vimrc,也是一种 plugin.换句话 ...
- IOS 学习笔记 2015-04-15 手势密码(原)
// // WPSignPasswordView.h // 网投网 // // Created by wangtouwang on 15/4/9. // Copyright (c) 2015年 wan ...
- HTML5 的绘图支持- canvas
Canvas HTML5新增了一个canvas元素,它是一张空画布,开发者需要通过JavaScript脚本进行绘制. 在canvas上绘图,经过如下3步 (1) 获取canvas元素对应的DOM对象. ...
- VMWare Workstation 占用443端口导致apache启动不了
中午安装vm,装linux 系统,搞了好几次才装成功,下午启动apache 忽然发现apache启动不了,各种郁闷啊,打开错误日志,NameVirtualHost无效,各种郁闷呐,试着修改端口,修改配 ...
- MVC + JQUERY + AJAX