最大流模板题

大部分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算法 邻接表实现)的更多相关文章

  1. POJ1273:Drainage Ditches(最大流入门 EK,dinic算法)

    http://poj.org/problem?id=1273 Description Every time it rains on Farmer John's fields, a pond forms ...

  2. HDU1532 Drainage Ditches —— 最大流(sap算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1532 Drainage Ditches Time Limit: 2000/1000 MS (Java/ ...

  3. poj-1273 Drainage Ditches(最大流基础题)

    题目链接: Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 67475   Accepted ...

  4. POJ-1273 Drainage Ditches 最大流Dinic

    Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 65146 Accepted: 25112 De ...

  5. poj1273 Drainage Ditches (最大流板子

    网络流一直没学,来学一波网络流. https://vjudge.net/problem/POJ-1273 题意:给定点数,边数,源点,汇点,每条边容量,求最大流. 解法:EK或dinic. EK:每次 ...

  6. [poj1273]Drainage Ditches(最大流)

    解题关键:最大流裸题 #include<cstdio> #include<cstring> #include<algorithm> #include<cstd ...

  7. poj1273 Drainage Ditches Dinic最大流

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 76000   Accepted: 2953 ...

  8. Poj 1273 Drainage Ditches(最大流 Edmonds-Karp )

    题目链接:poj1273 Drainage Ditches 呜呜,今天自学网络流,看了EK算法,学的晕晕的,留个简单模板题来作纪念... #include<cstdio> #include ...

  9. POJ 1273 - Drainage Ditches - [最大流模板题] - [EK算法模板][Dinic算法模板 - 邻接表型]

    题目链接:http://poj.org/problem?id=1273 Time Limit: 1000MS Memory Limit: 10000K Description Every time i ...

  10. 2018.07.06 POJ1273 Drainage Ditches(最大流)

    Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Description Every time it rains on Farmer J ...

随机推荐

  1. 【软件分享】文本对比工具 Beyond Compare

    转载自公众号:EmbeddDeveloper 对嵌入式感兴趣可以关注原作者博客: http://blog.csdn.net/ybhuangfugui 此处转载为分享用 Ⅰ.摘要 Beyond Comp ...

  2. 区域生长算法的一种C++实现

    区域生长算法是一种图像分割方法,能够将图像中具有相同特征的连通区域分割出来,同时保证较好的边缘信息. 区域生长算法的优点是简单,容易实现:但空间和时间复杂度较高,对分割图像要求较高,否则容易形成孔洞和 ...

  3. 项目中常用功能,如:流媒体、健康数据(步数等)等-b

    整理iOS开发中使用的各种流媒体和常用的高级功能.由于时间关系,目前只写了一部分功能,全部都采用的是系统方法,没用第三方,截图如下: screen1.png screen2.png 个人比较懒,不爱多 ...

  4. 设置html滚动条(陶庭飞问题)

    var height = document.body.scrollHeight; parent.document.all("rightFrame").style.height = ...

  5. input标签文字点击变颜色

    <input type="text" class="ser_input"value="从这里搜索(^_^)" onfocus=&quo ...

  6. 机器学习算法与Python实践之(二)支持向量机(SVM)初级

    机器学习算法与Python实践之(二)支持向量机(SVM)初级 机器学习算法与Python实践之(二)支持向量机(SVM)初级 zouxy09@qq.com http://blog.csdn.net/ ...

  7. JS获取IP、MAC和主机名的几种方法

    方法一(只针对IE且客户端的IE允许AcitiveX运行,通过平台:XP,SERVER03,2000): 获取客户端IP. <HTML> <HEAD> <TITLE> ...

  8. IBInspectable的使用

    IBInspectable的使用 创建一个分类 定义属性 把此分类的头文件引入到pch里面  此时查看属性面板  就有了可以供我们勾选的选项 在.m中实现一下set和get方法

  9. asp.net DropDownList无刷新ajax二级联动实现详细过程

    只适合新手制作DropDownList无刷新ajax二级联动效果: 数据库实现,添加两表如图:表1,pingpai,表2,type,具体数据库实现看自己的理解: //页面主要代码: <asp:S ...

  10. BZOJ_1613_ [Usaco2007_Jan]_Running_贝茜的晨练计划_(动态规划)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1613 n分钟,疲劳值上限是m,开始时疲劳值为0.第i分钟可以跑d[i]米.在某一时刻,如果疲劳 ...