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 ...
随机推荐
- NFS文件共享系统
1.NFS介绍 NFS是Network File System的缩写,主要功能是通过网络让不同的机器系统之间可以彼此共享文件或目录.NFS服务器可以允许NFS客户端将远端NFS服务端的共享目录挂载到本 ...
- 我也来学着写写WINDOWS服务-解析xml抓取数据并插入数据库
项目告一段落,快到一年时间开发了两个系统,一个客户已经在试用,一个进入客户测试阶段,中间突然被项目经理(更喜欢叫他W工)分派一个每隔两小时用windows服务去抓取客户提供的外网xml,解析该xml, ...
- sublimeText3中 less2css error: `lessc` is not available的解决办法
昨天在sublimeText3中安装了less 以及 less2css插件,但是在编译保存的时候总是出现错误: less2css error: `lessc` is not available 就go ...
- jQuery 源码分析2: jQuery.fn.init
//jQuery.fn.intit 中使用到的外部变量: // 判断是否为HTML标签或#id rquickExpr = /^(?:\s*(<[\w\W]+>)[^>]*|#([\w ...
- VS2010配置目录,解决:error MSB6006: “CL.exe”已退出,代码为 5问题
配置属性->VC++目录 可执行文件目录:$(VCInstallDir)bin;$(WindowsSdkDir)bin\NETFX 4.0 Tools;$(WindowsSdkDir)bin;$ ...
- IDE开发<LER-Studio>(2)::登录模块
软件中写登录模块是为了防止软件的恶意传播,内测阶段可以忽略登录. 以下为登录模块主要源代码: void CLoginDlg::OnBnClickedBtnLogin() { // TODO: Add ...
- 用C#实现MD5算法
/// <summary> /// 一个实现MD5散列字符串的类 /// </summary> public sealed class MD5Hash ...
- 一个PDO类
下面是在网上借鉴的一个PDO类: <?php class Database{ private $host = DB_HOST; private $user = DB_USER; private ...
- C语言-06复杂数据类型-01数组
01-数组 #include <stdio.h> int main() { // 使用注意 // 都是正确写法 //int ages[5] = {10 , 11, 12, 67, 56}; ...
- slivelight5和数据库交互
最近开始研究sliverlight和数据库交互了,无奈网上资料较少,查阅了大量资料终于成功了,但是我记得还有别的方法,希望大家讨论一下 数据访问层我的用的是ado.net实体数据模型 然后新建了一个w ...