hdu 4780 Candy Factory

题意:M个工厂生产N个糖果,现场过的人很少,但这道费用流本身并不是很难。

建图:我的建图是,每个糖果要么从初始状态转移过来,要么从其他糖果生产完的状态转移过来,同时拆点保证每个糖果只能生产一次。

另外看了杨神的建图,复杂度一样,但是他的抽象的更好,直接就是选择来源,这也是一般费用流的想法。

贴一下他的代码


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;

const int N = 510;
const int M = 1000010;
const int INF = 0x7f7f7f7f;

struct Edge {
    int v, cap, cost, next;
    Edge() {}
    Edge(int a, int b, int c, int d)
    :v(a), cap(b), cost(c), next(d) {}
}e[M];
int head[N], sz;
int dis[N], pre[N], cur[N];
bool inq[N];
queue<int> q;

void graphinit() {
    memset(head, -1, sizeof(head));
    sz = 0;
}
void addedge(int u, int v, int cp, int ct) {
//printf("%d %d %d %d\n", u, v, cp, ct);
    e[sz] = Edge(v, cp, ct, head[u]);
    head[u] = sz++;
    e[sz] = Edge(u, 0, -ct, head[v]);
    head[v] = sz++;
}
pair<int, int> mcmf(int s, int t) {
    int mc = 0, mf = 0;
    while (true) {
        memset(pre, -1, sizeof(pre));
        memset(inq, 0, sizeof(inq));
        memset(dis, 0x7f, sizeof(dis));
        dis[s] = 0;
        q.push(s);
        while (!q.empty()) {
            int u = q.front(); q.pop();
            inq[u] = false;
            for (int i = head[u]; i != -1; i = e[i].next) {
                int v = e[i].v;
                if (e[i].cap > 0 && dis[v] > dis[u] + e[i].cost) {
                    dis[v] = dis[u] + e[i].cost;
                    pre[v] = u; cur[v] = i;
                    if (!inq[v]) { inq[v] = true; q.push(v); }
                }
            }
        }
        if (pre[t] == -1) break;
        int aug = INF;
        for (int i = t; i != s; i = pre[i])
            aug = min(aug, e[cur[i]].cap);
        mf += aug;
        mc += dis[t] * aug;
        for (int i = t; i != s; i = pre[i]) {
            e[cur[i]].cap -= aug;
            e[cur[i] ^ 1].cap += aug;
        }
    }
    return make_pair(mf, mc);
}

const int MAXN = 110;
int n, m, k;
int candy_s[MAXN], candy_t[MAXN];
int start_time[MAXN][MAXN], start_cost[MAXN][MAXN];
int change_time[MAXN][MAXN], change_cost[MAXN][MAXN];

void read_matrix(int a[MAXN][MAXN], int n, int m) {
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            scanf("%d", &a[i][j]);
}
void work() {
    for (int i = 0; i < n; i++)
        scanf("%d%d", candy_s + i, candy_t + i);
    read_matrix(start_time, n, m);
    read_matrix(start_cost, n, m);
    read_matrix(change_time, n, n);
    read_matrix(change_cost, n, n);

    graphinit();
    int ss = 2 * n + m, tt = ss + 1;
    for (int i = 0; i < n; i++)
        addedge(ss, i, 1, 0);
    for (int i = 0; i < m; i++)
        addedge(i + n, tt, 1, 0);
    for (int i = 0; i < n; i++)
        addedge(i + n + m, tt, 1, 0);

    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++) {
            if (start_time[i][j] >= candy_t[i]) continue;
            int cost = start_cost[i][j];
            if (start_time[i][j] > candy_s[i])
                cost += k * (start_time[i][j] - candy_s[i]);
            addedge(i, j + n, 1, cost);
        }

    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++) if (i != j) {
            int dt = candy_t[i] + change_time[i][j];
            if (dt >= candy_t[j]) continue;
            int cost = change_cost[i][j];
            dt -= candy_s[j];
            if (dt > 0)
                cost += k * dt;
            addedge(j, i + n + m, 1, cost);
        }

    pair<int, int> ans = mcmf(ss, tt);
    if (ans.first < n) puts("-1");
    else printf("%d\n", ans.second);
}
int main() {
//freopen("../data.in", "r", stdin);
    while (scanf("%d%d%d", &n, &m, &k), n || m || k) {
        work();
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值