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 ...
随机推荐
- Shell面试题
1.用Shell编程,判断一文件是不是块或字符设备文件,如果是将其拷贝到 /dev 目录下. #!/bin/bash#1.sh#判断一文件是不是字符或块设备文件,如果是将其拷贝到 /dev 目录下#f ...
- 数组有N+M个数字, 数字的范围为1 ... N, 打印重复的元素, 要求O(M + N), 不可以用额外的空间
数组有N+M个数字, 数字的范围为1 ... N, 打印重复的元素, 要求O(M + N), 不可以用额外的空间 1.题目中要求我们不能使用额外的空间,那么我们能采用在原数组上做文章,这里的重点是如何 ...
- php调用whois接口域名查询
由两部分组成,一个index.php文件,一个whois的接口文件: <html> <head> <title>域名到期查询</title> <s ...
- CSS 背景颜色
颜色背景 CSS中背景颜色由background-color决定,这里的背景颜色会渲染padding和content,不会渲染border和margin部分. 在css3中可以通过background ...
- How Does #DeepDream Work?
How Does #DeepDream Work? Do neural networks hallucinate of electronic dogs? If you’ve been browsing ...
- android网络优化
Android---优化下载让网络访问更高效(二) ListView异步加载图片实现思路(优化篇) Android之ListView异步加载网络图片(优化缓存机制) android 网络加载图片,对图 ...
- 老鸟的Python入门教程
转自老鸟的Python入门教程 重要说明 这不是给编程新手准备的教程,如果您入行编程不久,或者还没有使用过1到2门编程语言,请移步!这是有一定编程经验的人准备的.最好是熟知Java或C,懂得命令行,S ...
- CF Rook, Bishop and King
http://codeforces.com/contest/370/problem/A 题意:车是走直线的,可以走任意多个格子,象是走对角线的,也可以走任意多个格子,而国王可以走直线也可以走对角线,但 ...
- javaweb学习总结(三十七)——获得MySQL数据库自动生成的主键
测试脚本如下: 1 create table test1 2 ( 3 id int primary key auto_increment, 4 name varchar(20) 5 ); 测试代码: ...
- 正则 ?<= 和 ?= 用法
参考网址:http://baike.baidu.com/link?url=2zORJF9GOjU8AkmuHDLz9cyl9yiL68PdW3frayzLwWQhDvDEM51V_CcY_g1mZ7O ...