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;
}

  

时间: 2024-10-14 10:48:26

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 over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer Jo

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

题目链接: Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 67475   Accepted: 26075 Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is cover

POJ1273 Drainage Ditches 最大流模板题(dinic)

最大流的模板题 给出边数M,顶点数N 以及每条边的容量 求1到N的最大流 注意可以有重边 邻接矩阵模板: #include<iostream> #include<cstdio> #include<cstring> #define maxx 0x3f3f3f #define M 205 using namespace std; int arc[M][M]; //弧的剩余流量 int level[M]; int n; int min(int a,int b) { retur

poj1273 Drainage Ditches(裸最大流)

Drainage Ditches Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Drainage Ditches Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description Every time it rains on Farmer Joh

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

题目链接:poj1273 Drainage Ditches 呜呜,今天自学网络流,看了EK算法,学的晕晕的,留个简单模板题来作纪念... 1 #include<cstdio> 2 #include<vector> 3 #include<queue> 4 #include<cstring> 5 #include<set> 6 #include<algorithm> 7 #define CLR(a,b) memset((a),(b),si

POJ 1273 Drainage Ditches(我的EK算法模板)

题意:给你n条边,目标位置t:接下来是每条边,包括起点,终点,容量: 感想:第一道最大流的代码,这道题是我更深地理解了Ek算法,不过这道题有个超坑的情况,那就是出现重边的情况==! 思路:EK算法 AC代码: #include<stdio.h> #include<string.h> #include<algorithm> #include<queue> using namespace std; #define INF 100000000 #define N

POJ 1273 Drainage Ditches 最大流

很裸的最大流问题,不过注意会有重边,o(╯□╰)o,被阴了WA了一发 还有就是要用long long #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include

POJ1273 Drainage Ditches 【最大流Dinic】

Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 56870   Accepted: 21863 Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by

POJ-1273 Drainage Ditches 【最大流】

Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage

poj 1273 Drainage Ditches (最大流入门)

1 /****************************************************************** 2 题目: Drainage Ditches(POJ 1273) 3 链接: http://poj.org/problem?id=1273 4 题意: 现在有m个池塘(从1到m开始编号,1为源点,m为汇点),及n条 5 水渠,给出这n条水渠所连接的池塘和所能流过的水量,求水 6 渠中所能流过的水的最大容量.水流是单向的. 7 算法: 最大流之增广路(入门)