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 ...
随机推荐
- android.util.AndroidRuntimeException: requestFeature() must be called before adding content解决办法
最近在学习第一行代码这本书,里面的关于activity生命周期有一段例子,但是我自己用mac上装的as运行一直出问题,看log的话就是android.util.AndroidRuntimeExcept ...
- JavaSE生成随机数
今天呢,老师讲了一下怎么用jvm实现随机数(本人对此很感兴趣),一个简单的随机100以内整数的代码如下: /** 生成随机数 */ import java.util.Random; public cl ...
- Java中多线程的使用!!
简介: 1.要了解多线程,首先我们得先了解进程和线程.那么什么是进程?进程就是一个正在运行的程序分配内存让应用程序能够运行的叫做进程.那么什么又是线程呢?线程:在一个程序中,负责代码的执行 ...
- IO流中的文件创建并且写入读取
package com.java.inoutputstreamDmeo.www; import java.io.File;import java.io.FileInputStream;import j ...
- webstorm的默认project编码为系统编码GBK.
使用新的IDE,而不会设置,会给你带来灾难. 如下为我是用webstorm时遇到的文件编码问题. 纳闷很久,终于发现是IDE的设置问题. 参考
- 窗口 namedWindow(), destroyWindow(), destroyAllWindows()[OpenCV 笔记6]
void namedWindow(const string& winname, int flags=WINDOW_AUTOSIZE); 创建一个窗口.imshow直接指定窗口名,可以省去此函数 ...
- IE 动态绑定click事件
//必须先清除原有的事件 $(dom).attr("onclick", ""); //再重新绑定新的事件 $(dom).bind("click&quo ...
- jquery 之事件 多库共存(noConflict)
/*jquery 之 简单事件jquery 与其它库共存,提供了 .noConflict() 方法,用法如下<script src="jquery 库"><scr ...
- DIV+CSS 网页布局之:三列布局
1.宽度自适应三列布局 三列布局的原理和两列布局的原理是一样的,只不过多了一列,只需给宽度自适应两列布局中间再加一列,然后重新计算三列的宽度,就实现了宽度自适应的三列布局. 同样的道理,更多列的布局, ...
- python基础教程笔记—画幅好画(详解)
今天写一下基础教程里面的第二个项目,主要使用python来做一个pdf的图,比较简单. 首先我们需要安装用到的模块pip install reportlab即可. 书上是用urlopen从往上下了一个 ...