poj 1273 EK最大流入门题

题意:

给n条边,从 fr 到 to,有一个容量 num。

然后问从1 流到 m,最多能流多少。


解析:

用EK求最大流,数据量比较小。


代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <climits>
#include <cassert>
#define LL long long
#define lson lo, mi, rt << 1
#define rson mi + 1, hi, rt << 1 | 1

using namespace std;
const int maxn = 200 + 10;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double pi = acos(-1.0);
const double ee = exp(1.0);

int cap[maxn][maxn];    //容量
int a[maxn];            //残量
int flow[maxn][maxn];   //流量
int p[maxn];            //记录父节点
int n, m;

int EK(int s, int t)
{
    queue<int> q;
    memset(flow, 0, sizeof(flow));
    int res = 0;
    while (1)
    {
        memset(a, 0, sizeof(a));
        a[s] = inf;
        q.push(s);
        while (!q.empty())
        {
            int u = q.front();
            q.pop();
            for (int v = s; v <= t; v++)
            {
                if (!a[v] && flow[u][v] < cap[u][v])
                {
                    p[v] = u;
                    q.push(v);
                    a[v] = min(a[u], cap[u][v] - flow[u][v]);
                }
            }
        }
        if (a[t] == 0)
            break;
        for (int u = t; u != s; u = p[u])
        {
            flow[p[u]][u] += a[t];
            flow[u][p[u]] -= a[t];
        }
        res += a[t];
    }
    return res;
}

int main()
{
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
#endif // LOCAL
    while (scanf("%d%d", &n, &m) == 2)
    {
        memset(cap, 0, sizeof(cap));
        for (int i = 1; i <= n; i++)
        {
            int fr, to, num;
            scanf("%d%d%d", &fr, &to, &num);
            cap[fr][to] += num;
        }
        int ans = EK(1, m);
        printf("%d\n", ans);
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值