uva 10806 uva 10746 最小费用最大流

10806:

题意:

解析:

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <climits>
#include <cassert>
#define LL long long

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

int n, m;
int flow[maxn][maxn];
int cap[maxn][maxn];
//int a[maxn][maxn];//残量
int p[maxn];
int cost[maxn][maxn];

void Init()
{
    memset(flow, 0, sizeof(flow));
    memset(cap, 0, sizeof(cap));
    memset(p, 0, sizeof(p));
    memset(cost, inf, sizeof(cost));
}

int EK(int s, int t)
{
    queue<int> q;
    int d[maxn];
    int c, f;
    c = f = 0;
    while (1)
    {
        ///bellman-ford start
        bool inq[maxn];
        for (int i = s; i <= t; i++)
            d[i] = (i == s) ? 0 : inf;
        memset(inq, false, sizeof(inq));
        q.push(s);
        while (!q.empty())
        {
            int u = q.front();
            q.pop();
            inq[u] = false;
            for (int v = s; v <= t; v++)
            {
                if (flow[u][v] < cap[u][v] && d[u] + cost[u][v] < d[v])
                {
                    d[v] = d[u] + cost[u][v];
                    p[v] = u;
                    if (!inq[v])
                    {
                        inq[v] = true;
                        q.push(v);
                    }
                }
            }
        }
        ///bellman-ford end

        if (d[t] == inf)
            break;
        int a = inf;
        for (int u = t; u != s; u = p[u])
        {
            a = min(a, cap[p[u]][u] - flow[p[u]][u]);
        }
        for (int u = t; u != s; u = p[u])
        {
            flow[p[u]][u] += a;
            flow[u][p[u]] -= a;
            ///
            cost[u][p[u]] = -cost[p[u]][u];
        }
        c += d[t] * a;
        f += a;
    }
    if (f == 2)
        return c;
    return -1;
}

int main()
{
    #ifdef LOCAL
    freopen("in.txt", "r", stdin);
    #endif // LOCAL
    while (scanf("%d", &n) && n)
    {
        scanf("%d", &m);
        Init();
        for (int i = 0; i < m; i++)
        {
            int u, v, w;
            scanf("%d%d%d", &u, &v, &w);
            cost[u][v] = w;
            cost[v][u] = w;
            cap[u][v] += 1;
            cap[v][u] += 1;
        }
        cap[n][n + 1] = 2;
        cost[n][n + 1] = cost[n + 1][n] = 0;
        int ans = EK(1, n + 1);
        if (ans == -1)
            printf("Back to jail\n");
        else
            printf("%d\n", ans);
    }
    return 0;
}

10746:

题意:

解析:

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <climits>
#include <cassert>
#define LL long long

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

int n, m;
int flow[maxn][maxn];
int cap[maxn][maxn];
int p[maxn];
double cost[maxn][maxn];

void Init()
{
    memset(flow, 0, sizeof(flow));
    memset(cap, 0, sizeof(cap));
    memset(p, 0, sizeof(p));
    memset(cost, 0, sizeof(cost));
}

double EK(int s, int t)
{
    queue<int> q;
    double d[maxn];
    double c, f;
    c = f = 0;
    while (1)
    {
        ///bellman-ford start
        bool inq[maxn];
        for (int i = s; i <= t; i++)
            d[i] = (i == s) ? 0 : inf;
        memset(inq, false, sizeof(inq));
        q.push(s);
        while (!q.empty())
        {
            int u = q.front();
            q.pop();
            inq[u] = false;
            for (int v = s; v <= t; v++)
            {
                if (flow[u][v] < cap[u][v] && d[u] + cost[u][v] + eps < d[v])///精度
                {
                    d[v] = d[u] + cost[u][v];
                    p[v] = u;
                    if (!inq[v])
                    {
                        inq[v] = true;
                        q.push(v);
                    }
                }
            }
        }
        ///bellman-ford end

        if (d[t] == inf)
            break;
        int a = inf;
        for (int u = t; u != s; u = p[u])
        {
            a = min(a, cap[p[u]][u] - flow[p[u]][u]);
        }
        for (int u = t; u != s; u = p[u])
        {
            flow[p[u]][u] += a;
            flow[u][p[u]] -= a;
        }
        c += d[t] * a;
        f += a;
    }
    return c;
}

int main()
{
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
#endif // LOCAL
    while (~scanf("%d%d", &n, &m))
    {
        if (!n && !m)
            break;
        Init();
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
            {
                int u, v;
                double w;
                scanf("%lf", &w);
                u = j, v = i + m;
                cap[u][v] = 1;
                cost[u][v] = w;
                cost[v][u] = -w;
            }
        }
        int s = 0;
        int t = m + n + 1;
        for (int i = 1; i <= m; i++)
        {
            cap[s][i] = 1;
        }
        for (int i = m + 1; i <= m + n; i++)
        {
            cap[i][t] = 1;
        }
        double ans = EK(s, t);
        printf("%.2lf\n", ans / n + eps);///精度
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值