最大流模板题

大部分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. 【JPA】query新对象 需要 构造函数

    构造函数 @Query("select g from Note g where id=?1" ) Note findById(Long id); @Query("sele ...

  2. 关于Qt5 UI设计的一些小知识

      (1) 获取textEdit中的值   QString str = ui->textedit->toPlainText(); // 这是普通文本 p=str.toInt();     ...

  3. 洛谷 P1064 金明的预算方案

    题目描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间金明自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:“你的房间需要购买哪些物品,怎么布置,你说了算,只要不超过N元钱就行”.今 ...

  4. C# - 中断模式下的调试

    1. 设置断点 选中需要设置断点的行,右键选择断点插入断点,此行左侧显示红色圆形标志.或者F9 有几个条件断点类型: a. 条件断点 b. 命中次数,大于,几倍于,大于等于你设置的断点次数此时中断 c ...

  5. 深入研究java.lang.ProcessBuilder类

     深入研究java.lang.ProcessBuilder类 一.概述       ProcessBuilder类是J2SE 1.5在java.lang中新添加的一个新类,此类用于创建操作系统进程,它 ...

  6. BZOJ 3971 Матрёшка 解题报告

    很自然想到区间 DP. 设 $Dp[i][j]$ 表示把区间 $[i, j]$ 内的套娃合并成一个所需要的代价,那么有: $Dp[i][i] = 0$ $Dp[i][j] = min\{Dp[i][k ...

  7. 一组神奇的 3D Gif 动图

    本文由 极客范 - 黄利民 翻译自 mymodernmet.欢迎加入极客翻译小组,同我们一道翻译与分享.转载请参见文章末尾处的要求. 虽然 gif 动图/动画似乎是无处不在现在了,但有些聪明人已经把 ...

  8. [Gauss]POJ2947 Widget Factory

    题意: 有n种小工具要加工,每种工具的加工时间为3到9天,给了m条加工记录.  每条记录 X $s_1$ $s_2$ 分别代表 这个工人在$s_1$到$s_2$(前闭后闭)的时间里加工了X件小工具   ...

  9. iOS上获得MAC地址

    很多时候我们都需要唯一来确定一台设备,苹果设备本来有个UDID号,可以实现这个目的.在iOS5.0以前,还有一个uniqueIdentifier的API用来获得这个number.不过iOS5之后,这个 ...

  10. PLSQL操作

    游标 SQL> set serveroutput onSQL> DECLARE  2    --定义游标,因为该游标中的数据需要更新,所以使用for update  3    CURSOR ...