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 ...
随机推荐
- 加解密算法二:非对称加解密及RSA算法的实现
加密和解密使用不同的密钥的一类加密算法.这类加密算法通常有两个密钥A和B,使用密钥A加密数据得到的密文,只有密钥B可以进行解密操作(即使密钥A也无法解密):相反,使用密钥B加密数据得到的密文,只有密钥 ...
- Spring核心框架 - AOP的原理及源码解析
一.AOP的体系结构 如下图所示:(引自AOP联盟) 层次3语言和开发环境:基础是指待增加对象或者目标对象:切面通常包括对于基础的增加应用:配置是指AOP体系中提供的配置环境或者编织配置,通过该配置A ...
- 深度(Depth)概念
强化对深度的理解 在老版本的NGUI中,UI的显示层次关系是依靠z轴进行的.在新版本的NGUI中,所有UI的z轴都被统一,然后用深度来决定和管理显示的层次关系.关于深度,要记住一下关键点: 1.每一个 ...
- Statusbar出现透明及界面下方出现空白
步骤1.在ViewController中 loadView #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000 if ( IOS7_OR_LATER ) ...
- Python按照索引访问list
由于list是一个有序集合,所以,我们可以用一个list按分数从高到低表示出班里的3个同学: >>> L = ['Adam', 'Lisa', 'Bart'] 那我们如何从list中 ...
- AppExtention - today
声明: 本文转自王巍 WWDC 2014 Session笔记 - iOS 通知中心扩展制作入门 本文是我的 WWDC 2014 笔记 中的一篇,涉及的 Session 有 Creating Exten ...
- SqlServer2008 设置修改表设计限制
我记起来了 SQL Server 2008 对操作的安全性进行了限制 你要在Management Studio菜单栏 -工具-选项,弹出选项窗口:把 “阻止保存要求重新创建表的更改” 请的勾去掉.
- java单例模式使用及注意事项
1. 说明 1)单例模式:确保一个类只有一个实例,自行实例化并向系统提供这个实例 2)单例模式分类:饿单例模式(类加载时实例化一个对象给自己的引用),懒单例模式(调用取得实例的方法如getInstan ...
- 异步IO模型和Overlapped结构
.NET中的 Overlapped 类 异步IO模型和Overlapped结构(http://blog.itpub.net/25897606/viewspace-705867/) 数据结构 OVERL ...
- win7 不能启动 memcached 总是反回failde to start service
原地址: http://zhidao.baidu.com/link?url=Ul9hJxFckU9IHWRy0pcxT11f2c0-p2uXkXhLria73mLNxYuV7IiaKYRtIl6vED ...