QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#629336#7502. Painting the RoadsxhguaWA 2ms3832kbC++141.5kb2024-10-11 10:48:552024-10-11 10:48:55

Judging History

你现在查看的是最新测评结果

  • [2024-10-11 10:48:55]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:3832kb
  • [2024-10-11 10:48:55]
  • 提交

answer

#include <bits/stdc++.h>

using i64 = long long;

constexpr int N = 5e3 + 5, S = 1e4, INF = (1 << 29);

int T, n, m, p[N], siz[N], f[N][N * 4], g[N * 4];
std::vector<std::array<int, 3>> G[N];

void chmin(int &x, int y) { if (y < x) x = y; }

void dfs(int u, int fa) {
    for (int i = -2 * n; i <= 2 * n; i++) f[u][i + S] = INF;
    for (int i = -n; i <= 0; i++) f[u][i + S] = 0;
    siz[u] = p[u] + 1;
    for (auto [v, w, c] : G[u]) if (v != fa) {
        dfs(v, u);
        for (int i = -siz[u] - siz[v]; i <= siz[u] + siz[v]; i++) g[i + S] = INF;
        for (int i = -siz[u]; i <= siz[u]; i++)
            for (int j = -siz[v]; j <= siz[v]; j++)
                if (c == (std::abs(j) & 1)) chmin(g[i + j + S], f[u][i + S] + f[v][j + S] + std::abs(j) * w);
        for (int i = -siz[u] - siz[v]; i <= siz[u] + siz[v]; i++) f[u][i + S] = g[i + S];
        siz[u] += siz[v];
    }
    for (int i = siz[u]; i >= -siz[u]; i--) f[u][i + S] = f[u][i - p[u] + S];
}

void solve() {
    std::cin >> n >> m;
    for (int i = 1; i <= n; i++) p[i] = 0, G[i].clear();
    for (int i = 1; i < n; i++) {
        int u, v, w, c; std::cin >> u >> v >> w >> c;
        G[u].push_back({v, w, c});
        G[v].push_back({u, w, c});
    }
    for (int i = 1, x; i <= m; i++) std::cin >> x, p[x]++;
    dfs(1, 0); std::cout << (f[1][S] == INF ? -1 : f[1][S]) << "\n";
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    std::cin >> T;
    while (T--) solve();

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3832kb

input:

5
3 2
1 2 1 1
2 3 2 1
1 3
4 2
1 2 3 1
2 3 1 0
3 4 4 1
1 2
5 4
1 2 3 0
2 3 1 1
3 4 2 0
4 5 2 1
1 1 1 1
5 2
1 2 2 1
1 3 3 0
1 5 2 1
3 4 1 1
1 2
10 5
1 2 10 1
2 3 3 1
3 4 4 0
4 5 4 1
5 6 2 1
2 7 8 0
2 8 9 1
4 9 1 0
1 10 4 0
10 10 2 1 8

output:

3
9
21
-1
42

result:

ok 5 number(s): "3 9 21 -1 42"

Test #2:

score: -100
Wrong Answer
time: 2ms
memory: 3752kb

input:

1000
5 5
1 2 4 1
2 3 9 0
3 4 10 1
3 5 8 1
1 5 2 5 1
5 3
1 2 7 1
1 3 7 0
2 4 9 0
3 5 4 1
3 4 3
5 3
1 2 7 1
2 3 1 0
1 4 7 1
4 5 5 1
4 4 3
5 1
1 2 3 1
1 3 6 0
2 4 10 0
2 5 7 0
1
5 3
1 2 10 1
1 3 10 0
1 4 1 1
3 5 4 0
2 5 2
5 5
1 2 7 0
1 3 5 0
2 4 8 1
2 5 10 0
2 2 3 5 4
5 4
1 2 6 1
1 3 4 0
3 4 4 0
1 5 5 ...

output:

22
-1
19
3
11
8
11
7
8
0
10
1
1
7
5
28
12
-1
19
16
12
13
-1
32
9
18
16
14
10
12
16
0
11
-1
17
-1
9
14
27
8
11
-1
6
6
15
18
46
0
14
9
-1
5
8
22
-1
-1
17
-1
25
6
0
24
6
15
21
15
22
-1
6
0
65
20
5
28
20
0
20
19
18
-1
10
0
16
9
19
6
21
11
11
4
6
20
11
0
8
8
31
8
23
-1
8
-1
11
-1
9
13
-1
-1
19
9
20
19
6
...

result:

wrong answer 426th numbers differ - expected: '16', found: '15'