Codeforces 362E Petya and Pipes (费用流)

n个点的网络,可以增加某些弧的容量,最大增加量为K,求增加后的最大流。

要求增大K容量后的最大流,那如果把增加的流量加上费用呢?那题目也就是说,当费用为K的时候,最大流是多少。。。。

把每条边<u, v, c>拆成两条边<u, v, c, 0>跟<u, v, K, 1>,然后求s-t费用流,在费用等于K的时候终止费用流就行了。

#include<algorithm>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#define REP(i, n) for(int i=0; i<n; i++)
#define CLR(a, b) memset(a, b, sizeof(a))
#define PB push_back
using namespace std;

const int INF = 1e9;
const int maxn = 100;
int n, K, x;
struct Edge
{
    int from, to, cap, flow, cost;
    Edge(){}
    Edge(int a, int b, int c, int d, int e)
        : from(a), to(b), cap(c), flow(d), cost(e){}
};
struct MCMF
{
    int n, m, s, t;
    vector<int> G[maxn];
    vector<Edge> edges;
    bool inq[maxn];
    int p[maxn], a[maxn], d[maxn];

    void AddEdge(int from, int to, int cap, int cost)
    {
        Edge e1 = Edge(from, to, cap, 0, cost);
        Edge e2 = Edge(to, from, 0, 0, -cost);
        edges.PB(e1); edges.PB(e2);
        m = edges.size();
        G[from].PB(m-2); G[to].PB(m-1);
    }

    bool spfa(int s, int t, int& flow, int& cost)
    {
        CLR(inq, 0);
        REP(i, n) d[i] = INF;
        d[s] = p[s] = 0; a[s] = INF;
        queue<int> q; q.push(s);
        while(!q.empty())
        {
            int u = q.front(); q.pop();
            inq[u] = false;
            REP(i, G[u].size())
            {
                Edge& e = edges[G[u][i]];
                if(e.cap > e.flow && d[e.to] > d[u] + e.cost)
                {
                    d[e.to] = d[u] + e.cost;
                    p[e.to] = G[u][i];
                    a[e.to] = min(a[u], e.cap - e.flow);
                    if(!inq[e.to])
                    {
                        q.push(e.to);
                        inq[e.to] = true;
                    }
                }
            }
        }
        if(d[t] == INF) return false;
        if(cost + d[t] * a[t] > K)
        {
            flow += (K - cost) / d[t];
            return false;
        }
        cost += d[t] * a[t];
        flow += a[t];
        int u = t;
        while(u != s)
        {
            edges[p[u]].flow += a[t];
            edges[p[u]^1].flow -= a[t];
            u = edges[p[u]].from;
        }
        return true;
    }

    int Mincost(int s, int t, int& flow, int& cost)
    {
        this->n = t + 1;
        flow = cost = 0;
        while(spfa(s, t, flow, cost));
        return flow;
    }
}solver;

int main()
{
    scanf("%d%d", &n, &K);
    REP(i, n) REP(j, n)
    {
        scanf("%d", &x);
        if(x)
        {
            solver.AddEdge(i, j, x, 0);
            solver.AddEdge(i, j, K, 1);
        }
    }
    int flow, cost;
    printf("%d\n", solver.Mincost(0, n-1, flow, cost));
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值