【题目链接】
【思路要点】
- 补档博客,无题解。
【代码】
#include<bits/stdc++.h> using namespace std; #define MAXN 505 #define INF 1e18 #define MAXQ 1000005 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; } struct edge {int dest, flow; long long cost; unsigned home; }; int n, s, t, flow; long long cost, len; long long dist[MAXN], c[MAXN]; int path[MAXN], value[MAXN], cnt[MAXN]; unsigned home[MAXN]; vector <edge> a[MAXN]; vector <int> b[MAXN]; bool visited[MAXN], colour[MAXN], unsolved; void flow_path() { int p = t, ans = 1e9; while (p != s) { ans = min(ans, a[path[p]][home[p]].flow); p = path[p]; } if (cost + ans * len < 0) { unsolved = false; ans = cost / (-len); } flow += ans, cost += ans * len; p = t; while (p != s) { a[path[p]][home[p]].flow -= ans; a[p][a[path[p]][home[p]].home].flow += ans; p = path[p]; } } bool spfa() { static int q[MAXQ]; static bool inq[MAXN]; int l = 0, r = 0; q[0] = s, inq[s] = true, dist[s] = 0; while (l <= r) { int tmp = q[l++]; inq[tmp] = false; for (unsigned i = 0; i < a[tmp].size(); i++) if (a[tmp][i].flow != 0 && dist[a[tmp][i].dest] < dist[tmp] + a[tmp][i].cost) { dist[a[tmp][i].dest] = dist[tmp] + a[tmp][i].cost; path[a[tmp][i].dest] = tmp; home[a[tmp][i].dest] = i; if (!inq[a[tmp][i].dest]) { inq[a[tmp][i].dest] = true; q[++r] = a[tmp][i].dest; } } } len = dist[t]; for (int i = 0; i <= r; i++) dist[q[i]] = -INF; return len != -INF; } void addedge(int s, int t, int flow, long long cost) { a[s].push_back((edge) {t, flow, cost, a[t].size()}); a[t].push_back((edge) {s, 0, -cost, a[s].size() - 1}); } bool prime(int value) { if (value == 1) return false; int tmp = sqrt(value); for (int i = 2; i <= tmp; i++) if (value % i == 0) return false; return true; } void work(int pos) { visited[pos] = true; for (unsigned i = 0; i < b[pos].size(); i++) if (!visited[b[pos][i]]) { colour[b[pos][i]] = !colour[pos]; work(b[pos][i]); } } int main() { read(n); for (int i = 1; i <= n; i++) read(value[i]); for (int i = 1; i <= n; i++) read(cnt[i]); for (int i = 1; i <= n; i++) read(c[i]); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) if (value[i] % value[j] == 0 && prime(value[i] / value[j])) { b[i].push_back(j); b[j].push_back(i); } for (int i = 1; i <= n; i++) if (!visited[i]) work(i); s = 0, t = n + 1; for (int i = 1; i <= n; i++) if (colour[i]) { addedge(s, i, cnt[i], 0); for (unsigned j = 0; j < b[i].size(); j++) addedge(i, b[i][j], 1e9, c[i] * c[b[i][j]]); } else addedge(i, t, cnt[i], 0); for (int i = 0; i <= n + 1; i++) dist[i] = -INF; unsolved = true; while (unsolved && spfa()) flow_path(); cout << flow << endl; return 0; }