poj1273--Drainage Ditches(最大流Edmond-Karp算法 邻接表实现)
最大流模板题
大部分Edmond-Karp算法代码都是邻接矩阵实现,试着改成了邻接表。
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring> using namespace std; // 裸最大流
const int N = 2005;
const int M = 2005;
const int INF = 0x7fffffff; // 邻接表
struct Edge {
int from, to, next, cost;
} edge[M];
int head[N];
int cnt_edge;
void add_edge(int u, int v, int c)
{
edge[cnt_edge].to = v;
edge[cnt_edge].from = u;
edge[cnt_edge].cost = c;
edge[cnt_edge].next = head[u];
head[u] = cnt_edge++;
} int pre[N];
int flow[N];
queue<int> q;
int bfs(int src, int des)
{
memset(pre, -1, sizeof pre);
while (!q.empty()) q.pop();
q.push(src);
flow[src] = INF;
while (!q.empty())
{
int u = q.front();
q.pop();
if (u == des) break;
for (int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
int cost = edge[i].cost;
if (pre[v] == -1 && cost > 0)
{
flow[v] = min(flow[u], cost);
pre[v] = i; // 记录的是边
q.push(v);
}
}
}
if (pre[des] == -1) return -1;
return flow[des];
} int maxFlow(int src, int des)
{
int ans = 0;
int in;
while ((in = bfs(src, des)) != -1)
{
int k = des;
while (k != src)
{
int last = pre[k];
edge[last].cost -= in;
edge[last ^ 1].cost += in;
k = edge[last].from;
}
ans += in;
}
return ans;
} int main()
{
int n, m;
while (~scanf("%d%d", &m, &n))
{
int a, b, c;
memset(head, -1, sizeof head);
while (m--)
{
scanf("%d%d%d", &a, &b, &c);
add_edge(a, b, c);
add_edge(b, a, 0);
}
printf("%d\n", maxFlow(1, n));
}
}
另附邻接矩阵版
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#define pk puts("kk"); using namespace std; // 裸最大流
const int N = 205;
const int INF = 0x7fffffff; int cap[N][N];
int flow[N];
int pre[N];
queue<int> q; int n, m; int bfs(int src, int des)
{
while (!q.empty()) q.pop();
memset(pre, -1, sizeof pre);
q.push(src);
flow[src] = INF;
while (!q.empty())
{
int idx = q.front();
if (idx == des) break;
q.pop();
for (int i = 1; i <= n; ++i)
{
if (pre[i] == -1 && cap[idx][i] > 0)
{
flow[i] = min(flow[idx], cap[idx][i]);
pre[i] = idx;
q.push(i);
}
}
}
if (pre[des] == -1) return -1;
return flow[des];
} int maxFlow(int src, int des)
{
int ans = 0;
int in;
while ((in = bfs(src, des)) != -1)
{
int k = des;
while (k != src)
{
int last = pre[k];
cap[last][k] -= in;
cap[k][last] += in;
k = last;
}
ans += in;
}
return ans;
} int main()
{
while (~scanf("%d%d", &m, &n))
{
int a, b, c;
memset(cap, 0, sizeof cap);
for (int i = 0; i < m; ++i)
{
scanf("%d%d%d", &a, &b, &c);
cap[a][b] += c;
}
printf("%d\n", maxFlow(1, n));
}
return 0;
}
poj1273--Drainage Ditches(最大流Edmond-Karp算法 邻接表实现)的更多相关文章
- POJ1273:Drainage Ditches(最大流入门 EK,dinic算法)
http://poj.org/problem?id=1273 Description Every time it rains on Farmer John's fields, a pond forms ...
- HDU1532 Drainage Ditches —— 最大流(sap算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1532 Drainage Ditches Time Limit: 2000/1000 MS (Java/ ...
- poj-1273 Drainage Ditches(最大流基础题)
题目链接: Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 67475 Accepted ...
- POJ-1273 Drainage Ditches 最大流Dinic
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 65146 Accepted: 25112 De ...
- poj1273 Drainage Ditches (最大流板子
网络流一直没学,来学一波网络流. https://vjudge.net/problem/POJ-1273 题意:给定点数,边数,源点,汇点,每条边容量,求最大流. 解法:EK或dinic. EK:每次 ...
- [poj1273]Drainage Ditches(最大流)
解题关键:最大流裸题 #include<cstdio> #include<cstring> #include<algorithm> #include<cstd ...
- poj1273 Drainage Ditches Dinic最大流
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 76000 Accepted: 2953 ...
- Poj 1273 Drainage Ditches(最大流 Edmonds-Karp )
题目链接:poj1273 Drainage Ditches 呜呜,今天自学网络流,看了EK算法,学的晕晕的,留个简单模板题来作纪念... #include<cstdio> #include ...
- POJ 1273 - Drainage Ditches - [最大流模板题] - [EK算法模板][Dinic算法模板 - 邻接表型]
题目链接:http://poj.org/problem?id=1273 Time Limit: 1000MS Memory Limit: 10000K Description Every time i ...
- 2018.07.06 POJ1273 Drainage Ditches(最大流)
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Description Every time it rains on Farmer J ...
随机推荐
- MVC-EditorFor与TextBoxFor的区别
EditorFor会根据后面提供的数据类型自动判断生成的控件类型(比如TextBox,CheckBox等): TextBoxFor生成的只是一个TextBox.
- NGUI系列教程四(自定义Atlas,Font)
今天我们来看一下怎么自定义NGUIAtlas,制作属于自己风格的UI.第一部分:自定义 Atlas1 . 首先我们要准备一些图标素材,也就是我们的UI素材,将其导入到unity工程中.2. 全选我们需 ...
- Windows获取其他进程中Edit控件的内容
最近做的MFC项目中,有个获取其他进程中Edit控件内容的需求,本来以为是个很简单的问题,但是来来回回折腾了不少时间,发博记录一下. 刚开始拿到这个问题,很自然的就想到GetDlgItemText() ...
- SpringMVC的controller方法中注解方式传List参数使用@RequestBody
在SpringMVC控制器方法中使用注解方式传List类型的参数时,要使用@RequestBody注解而不是@RequestParam注解: //创建文件夹 @RequestMapping(value ...
- sort()排序
var ary = [8,9,10,3], compare = function(a,b){ return b.age-a.age; }, g = [ { name:"jim", ...
- yum和rpm命令详解
rpm,全称RPM Package Manager,是RedHat发布的,针对特定硬件,已经编译好的软件包.安装之后就可以使用,不需要自行编译,以及之前对软件和硬件的检测,目录的配置等动作. yum, ...
- long([x[, base]]) :将一个字符转换为long类型
python的int型最大值和系统有关,32位和64位系统看到的结果是不一样,分别为2的31次方减1和2的63次方减1,可以通过sys.maxint查看此值. >>> import ...
- 【NOIP 2015 DAY2 T3】 运输计划 (树链剖分-LCA)
题目背景 公元 2044 年,人类进入了宇宙纪元. 题目描述 L 国有 n 个星球,还有 n-1 条双向航道,每条航道建立在两个星球之间,这 n-1 条航道连通了 L 国的所有星球. 小 P 掌管一家 ...
- 汇编语言中,SP,BP ,SI,DI作用?
这个很简单: sp:表示栈顶指针,指向栈顶地址.与SS相配合使用.ss为栈段. bp:是基址指针,段地址默认在SS中.可以定位物理地址,比如:"mov ax,[bp+si+6]/mov ax ...
- [杂题]URAL1822. Hugo II's War
看懂题意的请直接跳过下一坨! 本人有表达障碍! ========================================== 题意: (题意真的很难很难懂啊!!! 去他娘的**) 有一个王国 ...