POJ 1273 + HDU 3549(Edmods_Karp)(基础最大流)

版权声明:get busy living...or get busy dying https://blog.csdn.net/qq_41444888/article/details/88648924

https://cn.vjudge.net/problem/POJ-1273

https://vjudge.net/problem/HDU-3549

昨天又被lls训了,哎

我们队没人学过图论,给我一个月时间搞一搞,搞起来

跟着bin巨的板子来的http://www.cnblogs.com/kuangbin/archive/2011/07/26/2117636.html

简单最大流的暴力EK算法,直接bfs求增广路径就行,原理很简单,挂着当个板子用也行

HDU 3549:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#define ll long long
#define mod 1000000007
#define inf 0x3f3f3f3f
using namespace std;
const int maxn = 205;
queue<int> q;
int n, m, st, en;
int ma[maxn][maxn], path[maxn], flow[maxn];
int bfs()
{
    while(! q.empty()) q.pop();
    memset(path, -1, sizeof(path));
    flow[st] = inf;
    q.push(st);
    while(! q.empty())
    {
        int h = q.front();
        q.pop();
        if(h == en) break;
        for(int i = 1; i <= m; i ++)
        {
            if(i != st && path[i] == -1 && ma[h][i])
            {
                flow[i] = min(flow[h], ma[h][i]);
                q.push(i);
                path[i] = h;
            }
        }
    }
    if(path[en] == -1) return -1;
    return flow[m];
}
int Edmods_Karp()
{
    int max_flow = 0, step, now, pre;
    while((step = bfs()) != -1)
    {
        max_flow += step;
        now = en;
        while(now != st)
        {
            pre = path[now];
            ma[pre][now] -= step;
            ma[now][pre] += step;
            now = pre;
        }
    }
    return max_flow;
}
int main()
{
    int u, v, w;
    while(scanf("%d%d", &n, &m) != EOF)
    {
        memset(ma, 0, sizeof(ma));
        for(int i = 1; i <= n; i ++)
        {
            scanf("%d%d%d", &u, &v, &w);
            ma[u][v] += w;
        }
        st = 1, en = m;
        printf("%d\n", Edmods_Karp());
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41444888/article/details/88648924
今日推荐