1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| #include <bits/stdc++.h> #define MAX_N 1500 #define MAX_M 2000000 #define INF 0x3f3f3f3f using namespace std; template <class T> inline void read(T &x) { x = 0; int c = getchar(), f = 1; for (; !isdigit(c); c = getchar()) if (c == 45) f = -1; for (; isdigit(c); c = getchar()) (x *= 10) += f*(c-'0'); } int n, A[MAX_N+5], B[MAX_N+5], C[MAX_N+5]; int f[MAX_N+5], ord[MAX_N+5], ans[MAX_N+5]; int S, T, s, t, cnt, d[MAX_N+5], pr[MAX_N+5], cr[MAX_N+5]; bool cmp (const int &x, const int &y) {return C[x] < C[y];} struct node {int v, c, nxt;} E[MAX_M+5]; void init() {S = 0, T = 2*n+1, cnt = 0, memset(pr, -1, sizeof pr);} void insert(int u, int v, int c) {E[cnt] = (node){v, c, pr[u]}, pr[u] = cnt++;} void addedge(int u, int v, int c) {insert(u, v, c), insert(v, u, 0);} bool BFS() { queue <int> que; que.push(s); memset(d, -1, sizeof d), d[s] = 0; while (!que.empty()) { int u = que.front(); que.pop(); for (int i = pr[u]; ~i; i = E[i].nxt) { int v = E[i].v, c = E[i].c; if (~d[v] || !c) continue; d[v] = d[u]+1, que.push(v); } } return ~d[t]; } int DFS(int u, int flow) { if (u == t) return flow; int ret = 0; for (int &i = pr[u]; ~i; i = E[i].nxt) { int v = E[i].v, c = E[i].c; if (d[u]+1 != d[v] || !c) continue; int tmp = DFS(v, min(flow, c)); E[i].c -= tmp, E[i^1].c += tmp; flow -= tmp, ret += tmp; if (!flow) break; } if (!ret) d[u] = -1; return ret; } void cpy() {for (int i = S; i <= T; i++) cr[i] = pr[i];} void rec() {for (int i = S; i <= T; i++) pr[i] = cr[i];} int Dinic() {int ret = 0; cpy(); while (BFS()) ret += DFS(s, INF), rec(); return ret;} int main() { int CASE; read(CASE); for (; CASE; puts(""), CASE--) { read(n), init(); int mx = 0, tot = 0; for (int i = 1; i <= n; i++) read(A[i]); for (int i = 1; i <= n; i++) read(B[i]); for (int i = 1; i <= n; i++) read(C[i]); for (int i = 1; i <= n; i++) ord[i] = i; for (int i = 1; i <= n; i++) f[i] = 1; for (int i = 1; i <= n; i++) for (int j = 1; j < i; mx = max(mx, f[j++])) if (A[j] < A[i]) f[i] = max(f[i], f[j]+1); for (int i = 1; i <= n; i++) addedge(i, i+n, B[i]); for (int i = 1; i <= n; i++) if (f[i] == 1) addedge(S, i, INF); else if (f[i] == mx) addedge(i+n, T, INF); for (int i = 1; i <= n; i++) for (int j = i+1; j <= n; j++) if (A[i] < A[j] && f[j] == f[i]+1) addedge(i+n, j, INF); s = S, t = T, printf("%d ", Dinic()); sort(ord+1, ord+n+1, cmp), s = ord[1], t = s+n; for (int i = 1; i <= n; s = ord[++i], t = s+n) if (!BFS()) ans[++tot] = ord[i], s = ord[i], t = S, Dinic(), s = T, t = ord[i]+n, Dinic(), E[ord[i]*2-2].c = E[ord[i]*2-1].c = 0; sort(ans+1, ans+tot+1), printf("%d\n", tot); for (int i = 1; i <= tot; i++) printf("%d ", ans[i]); } return 0; }
|