UVALive - 2197 Paint the Roads(费用流)

题目大意:有n个点,m条边,你的任务是选择其中的一些边,使得每条被选择的边组成一些没有公共边的回路,且每个城市恰好在其中的k个回路上,被选择的边的总权值要求最小

解题思路:k个回路,每个城市都有,表示每个城市的入度和出度都是k,所以以此建边

源点连向每个城市,容量为k,费用0

每个城市连向汇点,容量为k,费用0

边连接两个城市,容量为1,费用为权值

跑最小费用最大流

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
#define N 1010
#define INF 0x3f3f3f3f

struct Edge{
    int from, to, cap, flow ,cost;
    Edge() {}
    Edge(int from, int to, int cap, int flow, int cost):from(from), to(to), cap(cap), flow(flow), cost(cost) {}
};

struct MCMF{
    int n, m, source, sink, flow, cost;
    vector<Edge> edges;
    vector<int> G[N];
    int d[N], f[N], p[N];
    bool vis[N];

    void init(int n) {
        this->n = n;
        for (int i = 0; i <= n; i++)
            G[i].clear();
        edges.clear();
    }

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

    bool BellmanFord(int s, int t, int &flow, int &cost) {
        for (int i = 0; i <= n; i++)
            d[i] = INF;
        memset(vis, 0, sizeof(vis));
        vis[s] = 1; d[s] = 0; f[s] = INF; p[s] = 0;
        queue<int> Q;
        Q.push(s);

        while (!Q.empty()) {
            int u = Q.front();
            Q.pop();
            vis[u] = 0;

            for (int i = 0; i < G[u].size(); i++) {
                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];
                    f[e.to] = min(f[u], e.cap - e.flow);
                    if (!vis[e.to]) {
                        vis[e.to] = true;
                        Q.push(e.to);
                    }
                }
            }
        }

        if (d[t] == INF)
            return false;

        flow += f[t];
        cost += d[t];

        int u = t;
        while (u != s) {
            edges[p[u]].flow += f[t];
            edges[p[u] ^ 1].flow -= f[t];
            u = edges[p[u]].from;
        }
        return true;
    }

    void Mincost(int s, int t) {
        flow = 0, cost = 0;
        while (BellmanFord(s, t, flow, cost));
    }
};
MCMF mcmf;

int n, m, k;
void solve() {
    scanf("%d%d%d", &n, &m, &k);
    int source = 2 * n, sink = 2 * n + 1;
    mcmf.init(sink);
    for (int i = 0; i < n; i++) {
        mcmf.AddEdge(source, i * 2, k, 0);
        mcmf.AddEdge(i * 2 + 1, sink, k, 0);
    }
    int u, v, c;
    for (int i = 1; i <= m; i++) {
        scanf("%d%d%d", &u, &v, &c);
        mcmf.AddEdge(u * 2, v * 2 + 1, 1, c);
    }
    mcmf.Mincost(source, sink);
    if (mcmf.flow == k * n) printf("%d\n", mcmf.cost);
    else printf("-1\n");

}

int main() {
    int test;
    scanf("%d", &test);
    while (test--) solve();
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-13 15:19:16

UVALive - 2197 Paint the Roads(费用流)的相关文章

【 UVALive - 5095】Transportation(费用流)

Description There are N cities, and M directed roads connecting them. Now you want to transport K units ofgoods from city 1 to city N. There are many robbers on the road, so you must be very careful. Themore goods you carry, the more dangerous it is.

【 UVALive - 2197】Paint the Roads(上下界费用流)

Description In a country there are n cities connected by m one way roads. You can paint any of these roads. To paint a road it costs d unit of money where d is the length of that road. Your task is to paint some of the roads so that the painted roads

UVa 2197 &amp; 拆点分环费用流

题意: 给你一个带权有向图,选择一些边组成许多没有公共边的环,使每个点都在k个环上,要求代价最小. SOL: 现在已经养成了这种习惯,偏题怪题都往网络流上想... 怎么做这题呢... 对我们看到每个点都在k个环上,而且没有公共边,那么很显然每个点的入度出度都为k.   然后我们拆点,建源汇ST,S与每个入点连边容量为k,出点与汇点相连容量为k,费用为0,如果城市i,j之间有边那么将i的入点和j的出点连一条费用为权,容量为1的边.然后跑一遍费用流.如果每条边都满流那么就有解. 好神奇...从环变成

【UVALive - 5131】Chips Challenge(上下界循环费用流)

Description A prominent microprocessor company has enlisted your help to lay out some interchangeable components(widgets) on some of their computer chips. Each chip’s design is an N × N square of slots. Oneslot can hold a single component, and you ar

Cyclic Tour HDUOJ 费用流

Cyclic Tour Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others)Total Submission(s): 1399    Accepted Submission(s): 712 Problem Description There are N cities in our country, and M one-way roads connecting them. Now Li

【网络流24题】No.19 负载平衡问题 (费用流)

[题意] G 公司有 n 个沿铁路运输线环形排列的仓库, 每个仓库存储的货物数量不等. 如何用最少搬运量可以使 n 个仓库的库存数量相同.搬运货物时,只能在相邻的仓库之间搬运. 输入文件示例input.txt517 9 14 16 4 输出文件示例output.txt11 [分析] 其实我觉得这题可以贪心啊..n^2贪心??.没细想.. 打的是费用流.. 大概这样建图: 懒得写了..凌乱之美.. 求满流费用.. 1 #include<cstdio> 2 #include<cstdlib&

POJ 3422 kaka&#39;s matrix trvals(费用流)

#include <iostream> #include <cstring> #include <cstdio> #include <cstdlib> #include <algorithm> #include <vector> #include <queue> #include <stack> #include <set> #include <map> #include <cma

hdu 2448 Mining Station on the Sea【网络费用流】

Mining Station on the Sea Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2371    Accepted Submission(s): 732 Problem Description The ocean is a treasure house of resources and the development

POJ训练计划3422_Kaka&#39;s Matrix Travels(网络流/费用流)

解题报告 题目传送门 题意: 从n×n的矩阵的左上角走到右下角,每次只能向右和向下走,走到一个格子上加上格子的数,可以走k次.问最大的和是多少. 思路: 建图:每个格子掰成两个点,分别叫"出点","入点", 入点到出点间连一个容量1,费用为格子数的边,以及一个容量∞,费用0的边. 同时,一个格子的"出点"向它右.下的格子的"入点"连边,容量∞,费用0. 源点向(0,0)的入点连一个容量K的边,(N-1,N-1)的出点向汇点连一