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 79 80 81 82 83 84 85 86 87
| #include <iostream> #include <cstdio> #include <queue> #define maxn 210 #define ll long long #define INF 1000000000 using namespace std;
int n, m;
int a[maxn], b[maxn], c[maxn], d[maxn];
struct Edge { int to, next, w; ll fi; } e[1000000]; int c1, head[maxn]; inline void add_edge(int u, int v, int w, ll fi) { e[c1].to = v; e[c1].w = w; e[c1].fi = fi; e[c1].next = head[u]; head[u] = c1++; }
inline void Add_edge(int u, int v, int w, ll fi) { add_edge(u, v, w, fi); add_edge(v, u, 0, -fi); }
ll dis[maxn]; int la[maxn], pre[maxn], fl[maxn]; bool vis[maxn]; int s, t; bool spfa() { fill(dis, dis + maxn, 1e18); dis[s] = 0; fill(vis, vis + maxn, 0); vis[s] = 1; queue<int> Q; Q.push(s); pre[t] = -1; fl[s] = INF; while (!Q.empty()) { int u = Q.front(); Q.pop(); vis[u] = 0; for (int i = head[u]; ~i; i = e[i].next) { int v = e[i].to, w = e[i].w; ll fi = e[i].fi; if (w > 0 && dis[v] > dis[u] + fi) { dis[v] = dis[u] + fi; pre[v] = u; fl[v] = min(fl[u], w); la[v] = i; if (vis[v]) continue; vis[v] = 1; Q.push(v); } } } return ~pre[t]; }
int mf; ll mc; int mcmf() { while (spfa()) { if (mc + dis[t] * fl[t] > 0) { mf += -mc / dis[t]; break; } mc += dis[t] * fl[t]; mf += fl[t]; int now = t; while (now != s) { e[la[now]].w -= fl[t]; e[la[now] ^ 1].w += fl[t]; now = pre[now]; } } return mf; }
int main() { fill(head, head + maxn, -1); ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
cin >> n; s = 0; t = n + 1; for (int i = 1; i <= n; ++i) cin >> a[i]; for (int i = 1; i <= n; ++i) cin >> b[i]; for (int i = 1; i <= n; ++i) cin >> c[i]; for (int i = 1; i <= n; ++i) { int x = a[i]; for (int j = 2; j * j <= x; ++j) while (x % j == 0) ++d[i], x /= j; if (x > 1) ++d[i]; } for (int i = 1; i <= n; ++i) { if (d[i] & 1) { Add_edge(i, t, b[i], 0); continue; } Add_edge(s, i, b[i], 0); for (int j = 1; j <= n; ++j) if (a[j] % a[i] == 0 && d[j] == d[i] + 1) Add_edge(i, j, INF, (ll) -c[i] * c[j]); else if (a[i] % a[j] == 0 && d[i] == d[j] + 1) Add_edge(i, j, INF, (ll) -c[i] * c[j]); } cout << mcmf() << "\n"; return 0; }
|