【题目链接】
【思路要点】
- DP+拆点最小割可以解决第一问。
- 按\(C_i\)从小到大考虑每一个点是否可以在最优解中被删除。
- 首先,若该点内部的边没有满流,那么这条边一定不在最小割上,故该点不可能在最优解中被删除。
- 否则我们考虑退掉该点内部边的在残量网络上的流量,并将其删去。具体退流的过程可以通过从汇点到该点出点,以及该点入点到源点各做一次流量限制为该点内部边的容量的最大流。
- 若该点在最优解中可以被删除,那么源点到汇点在残量网络上应当保持不连通,此时将该点计入答案。
- 否则加回删除的边,并重新求源点到汇点的最大流,保证源点到汇点在残量网络上不连通。
- 时间复杂度\(O(N*Dinic(N,M))\),其中\(M\)为DP后所建出的图的边数。
【代码】
#include<bits/stdc++.h> using namespace std; const int MAXN = 705; const int MAXP = 1505; const int INF = 1e9; template <typename T> void chkmax(T &x, T y) {x = max(x, y); } template <typename T> void chkmin(T &x, T y) {x = min(x, y); } template <typename T> void read(T &x) { x = 0; int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; x *= f; } template <typename T> void write(T x) { if (x < 0) x = -x, putchar('-'); if (x > 9) write(x / 10); putchar(x % 10 + '0'); } template <typename T> void writeln(T x) { write(x); puts(""); } struct edge {int dest; int flow; unsigned home; }; int ss, st, ex, s, t, tot, dist[MAXP]; vector <edge> a[MAXP]; unsigned curr[MAXP]; int inp[MAXN], outp[MAXN], home[MAXN]; int n, x[MAXN], y[MAXN], z[MAXN], dp[MAXN]; bool selected[MAXN]; bool cmp(int x, int y) {return z[x] < z[y]; } int dinic(int pos, int limit) { if (pos == t) return limit; int used = 0, tmp; for (unsigned &i = curr[pos]; i < a[pos].size(); i++) if (a[pos][i].flow != 0 && dist[pos] + 1 == dist[a[pos][i].dest] && (tmp = dinic(a[pos][i].dest, min(limit - used, a[pos][i].flow))) != 0) { used += tmp; a[pos][i].flow -= tmp; a[a[pos][i].dest][a[pos][i].home].flow += tmp; if (used == limit) return used; } return used; } bool bfs() { static int q[MAXP], l = 0, r = -1; for (int i = 0; i <= r; i++) dist[q[i]] = 0; q[l = r = 0] = s, dist[s] = 1; while (l <= r) { int tmp = q[l++]; for (unsigned i = 0; i < a[tmp].size(); i++) if (a[tmp][i].flow != 0 && dist[a[tmp][i].dest] == 0) { q[++r] = a[tmp][i].dest; dist[a[tmp][i].dest] = dist[tmp] + 1; if (a[tmp][i].dest == t) return true; } } return false; } void addedge(int s, int t, int flow) { a[s].push_back((edge) {t, flow, a[t].size()}); a[t].push_back((edge) {s, 0, a[s].size() - 1}); } bool cut(int x, int y) { for (unsigned i = 0; i < a[x].size(); i++) if (a[x][i].dest == y) if (a[x][i].flow) return false; return true; } void cutedge(int x, int y) { for (unsigned i = 0; i < a[x].size(); i++) if (a[x][i].dest == y) a[x][i].flow = 0; for (unsigned i = 0; i < a[y].size(); i++) if (a[y][i].dest == x) a[y][i].flow = 0; } int main() { int T; read(T); while (T--) { read(n); for (int i = 1; i <= n; i++) read(x[i]); for (int i = 1; i <= n; i++) read(y[i]); for (int i = 1; i <= n; i++) read(z[i]); int Max = 0; for (int i = 1; i <= n; i++) { dp[i] = 1; for (int j = 1; j <= i - 1; j++) if (x[i] > x[j]) chkmax(dp[i], dp[j] + 1); chkmax(Max, dp[i]); home[i] = i; } ss = s = tot = 0, st = t = 2 * n + 1, ex = 2 * n + 2; for (int i = 0; i <= 2 * n + 2; i++) a[i].clear(); for (int i = 1; i <= n; i++) { inp[i] = ++tot; outp[i] = ++tot; addedge(inp[i], outp[i], y[i]); if (dp[i] == 1) addedge(s, inp[i], INF); if (dp[i] == Max) addedge(outp[i], t, INF); for (int j = 1; j <= i - 1; j++) if (dp[j] + 1 == dp[i] && x[j] < x[i]) addedge(outp[j], inp[i], INF); } int ans = 0; while (bfs()) { memset(curr, 0, sizeof(curr)); ans += dinic(s, INF); } printf("%d ", ans); sort(home + 1, home + n + 1, cmp); memset(selected, false, sizeof(selected)); int ttot = 0; for (int i = 1; i <= n; i++) { int tmp = home[i]; if (!cut(inp[tmp], outp[tmp])) continue; selected[tmp] = true; ttot += y[tmp]; cutedge(inp[tmp], outp[tmp]); addedge(ex, st, y[tmp]); s = ex, t = outp[tmp]; while (bfs()) { memset(curr, 0, sizeof(curr)); dinic(s, INF); } cutedge(ex, st); addedge(ex, inp[tmp], y[tmp]); s = ex, t = ss; while (bfs()) { memset(curr, 0, sizeof(curr)); dinic(s, INF); } cutedge(ex, inp[tmp]); s = ss, t = st; if (bfs()) { selected[tmp] = false, ttot -= y[tmp]; addedge(inp[tmp], outp[tmp], y[tmp]); while (bfs()) { memset(curr, 0, sizeof(curr)); dinic(s, INF); } } } int sum = 0; for (int i = 1; i <= n; i++) sum += selected[i]; writeln(sum); for (int i = 1; i <= n; i++) if (selected[i]) printf("%d ", i); printf("\n"); } return 0; }