QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#516803 | #7762. 数据库 | JWRuixi | 40 | 41ms | 5976kb | C++20 | 3.2kb | 2024-08-12 21:53:30 | 2024-08-12 21:53:30 |
Judging History
answer
#ifdef LOCAL
#include "stdafx.h"
#else
#include <bits/stdc++.h>
#define IL inline
#define LL long long
#define eb emplace_back
#define L(i, j, k) for (int i = (j); i <= (k); ++i)
#define R(i, j, k) for (int i = (j); i >= (k); --i)
#define FIO(FILE) freopen(FILE".in", "r", stdin), freopen(FILE".out", "w", stdout)
using namespace std;
using vi = vector<int>;
#endif
template<int N, int M>
struct MCMF {
const LL inf = 1e18;
int n, hd[N], cur[N], tot = 1, S, T;
LL h[N], d[N];
struct edge {
int v, nx, w, c;
} e[M * 2];
void add_edge (int u, int v, int w, int c) {
e[++tot] = {v, hd[u], w, c};
hd[u] = tot;
e[++tot] = {u, hd[v], 0, -c};
hd[v] = tot;
}
bool vis[N];
bool spfa () {
L (i, 1, n) {
h[i] = inf;
}
h[S] = 0;
L (u, S, T - 1) {
for (int i = hd[u], v; i; i = e[i].nx) {
if (e[i].w) {
v = e[i].v;
h[v] = min(h[v], h[u] + e[i].c);
}
}
}
return h[T] < inf;
}
bool dij () {
L (i, 1, n) {
cur[i] = hd[i];
d[i] = inf;
}
d[S] = 0;
priority_queue<pair<LL, int>> q;
q.emplace(0, S);
while (!q.empty()) {
int u = q.top().second;
q.pop();
if (vis[u]) {
continue;
}
vis[u] = true;
for (int i = hd[u], v; i; i = e[i].nx) {
if (e[i].w) {
v = e[i].v;
LL x = d[u] + e[i].c + h[u] - h[v];
if (d[v] > x) {
d[v] = x;
q.emplace(-x, v);
}
}
}
}
L (i, 1, n) {
vis[i] = false;
h[i] += d[i];
}
return d[T] < inf;
}
LL cost;
LL dfs (int u, LL in) {
if (u == T) {
return in;
}
vis[u] = true;
LL out = 0;
for (int &i = cur[u], v; i; i = e[i].nx) {
if (e[i].w && !vis[v = e[i].v]) {
LL c = e[i].c;
if (h[v] == h[u] + c) {
LL w = dfs(v, min(in, (LL)e[i].w));
in -= w;
out += w;
e[i].w -= w;
e[i ^ 1].w += w;
cost += w * c;
if (!in) {
break;
}
}
}
}
vis[u] = false;
return out;
}
pair<LL, LL> dinic () {
LL flow = 0;
cost = 0;
if (spfa()) {
while (dij()) {
flow += dfs(S, inf);
}
}
return make_pair(flow, cost);
}
};
constexpr int N = 5e3 + 9;
constexpr int inf = 1e9;
int n, m, q, w[N], k[N], id[N][3];
MCMF<3 * N, 5 * N> G;
int lst[N];
int main () {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> m >> q;
L (i, 1, n) {
cin >> w[i];
}
L (i, 1, q) {
cin >> k[i];
}
id[0][0] = ++G.n;
L (i, 1, q) {
R (j, 2, 0) {
id[i][j] = ++G.n;
}
}
L (i, 1, q) {
G.add_edge(id[i - 1][0], id[i][0], m - (i == 1), 0);
G.add_edge(id[i - 1][0], id[i][1], 1, w[k[i]]);
G.add_edge(id[i][1], id[i][2], 1, -inf);
G.add_edge(id[i][2], id[i][0], 1, 0);
if (lst[k[i]]) {
G.add_edge(id[lst[k[i]]][2], id[i][1], 1, 0);
}
lst[k[i]] = i;
}
G.S = 1;
G.T = id[q][0];
cout << G.dinic().second + (LL)inf * q << '\n';
}
// I love WHQ!
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 20
Accepted
Test #1:
score: 20
Accepted
time: 28ms
memory: 5976kb
input:
10 3 5000 23077 34848 88221 8067 83132 62621 41320 69146 32971 27594 2 7 5 3 9 6 1 9 4 8 1 8 8 3 6 9 1 7 5 5 6 8 1 3 10 6 8 7 10 2 1 2 6 7 5 5 9 5 7 10 10 6 6 7 2 4 3 1 10 10 5 1 1 6 1 2 8 9 2 5 10 1 10 7 5 5 10 5 2 5 6 10 9 5 6 3 5 3 6 5 7 4 1 5 8 1 1 9 7 1 1 2 1 8 6 2 9 8 4 2 5 4 5 2 10 4 3 6 9 7 ...
output:
100598924
result:
ok single line: '100598924'
Test #2:
score: 20
Accepted
time: 38ms
memory: 5712kb
input:
10 4 5000 54224 55364 32836 46635 99179 26033 49587 15072 63093 94302 4 7 8 4 1 2 6 3 1 1 6 10 5 7 2 9 3 9 1 5 7 3 8 4 7 2 5 3 5 4 1 6 4 10 4 10 8 1 5 7 9 7 9 1 6 1 10 10 10 5 1 1 3 5 10 1 8 8 6 8 8 10 6 9 7 6 1 1 5 3 10 8 6 5 8 7 5 8 4 8 8 1 8 6 4 5 8 10 7 6 7 2 10 7 10 10 3 1 3 5 5 7 3 2 5 3 1 7 6...
output:
85562058
result:
ok single line: '85562058'
Test #3:
score: 20
Accepted
time: 41ms
memory: 5780kb
input:
10 5 5000 8091 5917 35634 83114 46741 61842 48134 41606 63881 20560 8 7 9 5 1 4 10 9 10 9 8 5 5 7 5 1 5 9 4 1 4 7 9 4 10 4 4 2 9 1 5 1 2 6 10 1 5 4 5 3 3 1 6 7 5 7 10 2 5 8 2 9 3 5 3 1 2 4 5 10 4 4 10 4 8 2 8 6 4 1 3 6 8 2 6 2 2 5 9 4 2 8 3 10 8 7 2 1 10 6 5 8 2 8 9 3 9 5 6 6 7 2 7 3 10 9 10 2 4 7 2...
output:
43892491
result:
ok single line: '43892491'
Subtask #2:
score: 0
Time Limit Exceeded
Test #4:
score: 20
Accepted
time: 31ms
memory: 5720kb
input:
10 2 5000 65644 5214 85000 40719 98071 56616 35404 16019 96748 89032 5 9 6 1 3 6 8 10 2 9 1 10 5 9 9 9 2 7 7 7 7 10 5 1 3 10 4 2 5 5 2 8 3 2 1 3 3 6 2 4 5 5 2 5 2 3 4 2 10 1 4 6 10 9 6 4 9 10 5 3 9 7 7 2 1 9 5 8 8 4 8 8 4 5 6 1 3 4 8 10 4 3 6 6 9 2 5 2 8 5 10 4 7 10 3 9 3 2 9 3 10 7 1 3 9 9 3 5 1 3 ...
output:
173524192
result:
ok single line: '173524192'
Test #5:
score: 0
Time Limit Exceeded
input:
100 2 5000 49570 6371 37107 2261 33457 98048 84700 76277 32602 53831 20995 86351 57905 93492 65198 80688 44394 48442 57924 88655 16250 11904 21033 99426 59241 71456 7697 85276 81310 49884 64543 72091 63969 23936 88032 62420 42269 76663 37639 16930 61480 97674 38809 77434 25043 46618 93378 74399 1031...
output:
result:
Subtask #3:
score: 20
Accepted
Test #7:
score: 20
Accepted
time: 2ms
memory: 3896kb
input:
5000 15 400 34145 93322 29976 7963 53362 50640 10859 94528 13329 49980 18826 50286 60155 79748 73253 18329 5216 83079 48220 98825 46592 76855 14037 19859 80960 4461 377 70496 28092 99806 5355 27013 92051 95231 65553 32365 3862 89764 86063 93033 12754 68996 38965 52942 69948 34370 3023 52079 16066 57...
output:
19341503
result:
ok single line: '19341503'
Test #8:
score: 20
Accepted
time: 1ms
memory: 3788kb
input:
5000 15 400 68511 70579 96844 20943 72871 28340 68790 60294 76760 12623 80406 65418 37942 2849 76307 28818 18555 42151 94506 78241 15350 11345 76323 21860 22703 31009 24763 71007 60858 53746 28720 5027 72512 39122 55299 41675 88475 94331 78711 3464 80345 69031 98746 14792 97862 97255 5853 92742 9096...
output:
19515326
result:
ok single line: '19515326'
Test #9:
score: 20
Accepted
time: 2ms
memory: 4048kb
input:
5000 15 400 18305 15347 86331 37503 78391 3378 82447 51150 49032 2422 47659 20516 18666 16520 27948 17865 45842 66799 35584 75288 46358 21654 30233 25291 44810 27579 93986 15174 56012 48286 66826 93615 77979 40669 17262 77857 82134 50752 19642 77961 55856 68600 60598 33035 94214 17392 16736 21460 58...
output:
18253485
result:
ok single line: '18253485'
Subtask #4:
score: 0
Time Limit Exceeded
Test #10:
score: 20
Accepted
time: 6ms
memory: 4124kb
input:
10 5 1000 86764 81108 88408 93029 87155 18790 28170 29349 81866 77109 8 7 10 7 2 7 1 8 4 10 9 10 4 1 7 1 9 9 1 6 6 1 9 6 7 1 8 10 1 7 9 1 1 9 7 10 8 5 5 1 2 3 10 6 2 6 2 6 1 2 7 8 5 6 10 2 9 8 2 6 8 5 10 8 1 10 4 6 5 6 3 8 1 3 5 2 2 7 2 4 5 5 8 2 4 1 3 6 8 2 2 3 1 8 1 5 8 6 9 7 7 3 7 9 8 9 9 7 5 3 6...
output:
15248002
result:
ok single line: '15248002'
Test #11:
score: 0
Time Limit Exceeded
input:
10 10 1000 57793 91210 18011 676 38391 78527 56352 69343 95947 5616 5 8 4 5 10 6 1 7 3 10 2 7 1 9 5 3 5 8 3 8 7 10 2 10 6 1 4 3 6 10 6 3 8 4 3 9 1 6 10 3 10 1 6 2 9 9 3 5 8 8 3 9 1 5 9 9 3 3 7 9 2 4 1 9 8 3 1 2 8 9 10 9 4 3 10 5 4 4 8 6 6 9 3 3 2 3 7 2 3 3 10 4 5 5 6 4 2 5 10 1 2 6 9 8 7 9 4 8 2 1 2...
output:
result:
Subtask #5:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
0%