P2762 太空飞行计划问题

题意

\(n\) 个工作,每个工作需要一些限制。

你可以花 \(s_i\) 的代价满足一个限制。

然后获得 \(h_i\) 的贡献。

问是的获得的贡献最大可以使多少?

Sol

最小割。

从源点往每个实验连 \(h_i\),每个实验往每个代价连 \(inf\).

代价往汇点连 \(s_i\) 就行了。

Code

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <array>
#include <queue>
#include <vector>
#include <bitset>
#include <cstring>
#define int long long
#define pii pair <int, int>
using namespace std;
// #ifdef ONLINE_JUDGE

// #define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
// char buf[1 << 23], *p1 = buf, *p2 = buf, ubuf[1 << 23], *u = ubuf;

// #endif
int read() {
    int p = 0, flg = 1;
    char c = getchar();
    while (c < '0' || c > '9') {
        if (c == '-') flg = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        p = p * 10 + c - '0';
        c = getchar();
    }
    return p * flg;
}
void write(int x) {
    if (x < 0) {
        x = -x;
        putchar('-');
    }
    if (x > 9) {
        write(x / 10);
    }
    putchar(x % 10 + '0');
}

#define fi first
#define se second

const int N = 1005, M = 10005, inf = 2e18;

namespace G {

array <int, N> fir;
array <int, M> nex, to, cap;
int cnt = 1;
void add(int x, int y, int z) {
    cnt++;
    nex[cnt] = fir[x];
    to[cnt] = y;
    cap[cnt] = z;
    fir[x] = cnt;
}
void _add(int x, int y, int z) {
    add(x, y, z);
    add(y, x, 0);
}

}

namespace Mfl {

array <int, N> cur, dis;
queue <int> q;

bool bfs(pii st) {
    dis.fill(-1), dis[st.fi] = 0;
    q.push(st.fi);
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        for (int i = G::fir[u]; i; i = G::nex[i]) {
            if (!G::cap[i] || ~dis[G::to[i]]) continue;
            dis[G::to[i]] = dis[u] + 1, q.push(G::to[i]);
        }
    }
    return ~dis[st.se];
}

int dfs(int x, int augcd, pii st) {
    if (x == st.se) return augcd;
    int augc = augcd;
    for (int &i = cur[x]; i; i = G::nex[i]) {
        if (!G::cap[i] || dis[G::to[i]] <= dis[x]) continue;
        int flow = dfs(G::to[i], min(augc, G::cap[i]), st);
        augc -= flow;
        G::cap[i] -= flow, G::cap[i ^ 1] += flow;
        if (!augc) break;
    }
    return augcd - augc;
}

int dinic(pii st) {
    int ans = 0;
    while (bfs(st)) {
        copy(G::fir.begin(), G::fir.end(), cur.begin());
        ans += dfs(st.fi, inf, st);
    }
    return ans;
}

}

bitset <N> vis;

void dfs(int x) {
    vis[x] = 1;
    for (int i = G::fir[x]; i; i = G::nex[i]) {
        if (!G::cap[i] || vis[G::to[i]]) continue;
        dfs(G::to[i]);
    }
}

char tools[10000]; 
signed main() {
    int n, m; scanf("%d%d", &m, &n);
    pii st = make_pair(n + m + 1, n + m + 2);
    int ans = 0;
    for (int i = 1; i <= m; i++) {
        int x; scanf("%d", &x);
        ans += x;
        G::_add(st.fi, i + n, x);
        memset(tools, 0, sizeof tools);
        cin.getline(tools, 10000); int ulen = 0,tool;
        while (sscanf(tools + ulen,"%d", &tool) == 1) {
            G::_add(i + n, tool, inf);
            if (tool == 0) ulen++;
            else while (tool) tool /= 10, ulen++;
            ulen++;
        }
    }
    for (int i = 1; i <= n; i++) {
        int x; scanf("%d", &x);
        G::_add(i, st.se, x);
    }
    ans = ans - Mfl::dinic(st); dfs(st.fi);
    for (int i = n + 1; i <= n + m; i++)
        if (vis[i]) write(i - n), putchar(32);
    puts("");
    for (int i = 1; i <= n; i++)
        if (vis[i]) write(i), putchar(32);
    puts("");
    write(ans), puts("");
    return 0;
}
posted @ 2023-12-11 19:05  cxqghzj  阅读(2)  评论(0编辑  收藏  举报