UVALive 2197 Paint the Roads(费用流)

n个点m条边的带权有向图,现在要将某些边涂上颜色,使得每个点恰好在k个有颜色的环上。

每个点都在k各环中,说明每个点的 入度=出度=k。 用费用流搞,增加源汇点s ,t,将每个点拆成两个点,u跟u+n,一个代表出度,一个代表入度。从s向每个u连<k, 0>的边,从每个u+n向t连<k, 0>的边,分别代表入度跟出度的上限。 然后对于原图中的有向边<u, v, d>, 从u向v+n连一条<1, d>的边。这样求s->t的最小费用最大流。如果能漫流,答案就是最小费用。否则无解。

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

const int maxn = 100;
const int INF = 1e9;
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<Edge> edges;
    vector<int> G[maxn];
    bool inq[maxn];
    int d[maxn], p[maxn], a[maxn];

    void init(int n)
    {
        this->n = n;
        REP(i, n) G[i].clear();
        edges.clear();
    }

    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)
    {
        REP(i, t+1) d[i] = INF;
        CLR(inq, 0);
        d[s] = 0; 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;
        flow += a[t];
        cost += d[t] * 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)
    {
        flow = cost = 0;
        while(spfa(s, t, flow, cost));
        return cost;
    }
}solver;

int T, n, m, k;

int main()
{
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d%d%d", &n, &m, &k);
        int s = 0, t = n * 2 + 1;
        solver.init(t+1);
        FF(i, 1, n+1)
        {
            solver.AddEdge(s, i, k, 0);
            solver.AddEdge(i+n, t, k, 0);
        }
        REP(i, m)
        {
            int f, t, d;
            scanf("%d%d%d", &f, &t, &d);
            solver.AddEdge(f+1, t+1+n, 1, d);
        }
        int flow, cost;
        cost = solver.Mincost(s, t, flow, cost);
        if(flow == k*n) printf("%d\n", cost);
        else puts("-1");
    }
    return 0;
}


你可能感兴趣的:(UVALive 2197 Paint the Roads(费用流))