QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#763592 | #6996. Building Factories | hejinming983282# | WA | 1644ms | 174792kb | C++23 | 3.6kb | 2024-11-19 21:07:19 | 2024-11-19 21:07:20 |
Judging History
answer
// Author : hejinming2012
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
#define dbg(x) cout << #x " = " << (x) << endl
#define quickio ios::sync_with_stdio(false);
#define quickin cin.tie(0);
#define quickout cout.tie(0);
using namespace std;
inline int read() {
int n = 0, f = 1; char c = getchar();
while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); }
while(c >= '0' && c <= '9') { n = (n << 1) + (n << 3) + (c & 15); c = getchar(); }
return n * f;
}
inline void write(int x) {
if(x < 0) putchar('-'), x = -x;
if(x > 9) write(x / 10);
putchar(x % 10 + '0');
}
struct NP {
int u, p;
};
vector<NP> dfs(int r, const vector < vector < pair < int, int > > > &g) {
vector < NP > po;
stack < tuple < int, int, bool > > st;
st.emplace(r, -1, false);
while(!st.empty()) {
auto [u, p, v] = st.top(); st.pop();
if(v) po.emplace_back(NP{u, p});
else {
st.emplace(u, p, true);
for(auto &[v, w] : g[u])
if(v != p) st.emplace(v, u, false);
}
}
return po;
}
signed main() {
quickio
quickin
quickout
int T = read();
for(int _ = 1; _ <= T; _++) {
int n = read(), k = read();
vector < vector < pair < int, int > > > g(n + 1);
for(int i = 1; i < n; i++) {
int u = read(), v = read(), w = read();
g[u].emplace_back(v, w);
g[v].emplace_back(u, w);
}
vector<NP> po = dfs(1, g);
vector<vector<int>> dp(n + 1, vector<int>(k + 1, LLONG_MAX)), sum(n + 1, vector<int>(k + 1));
for(auto &np : po) {
int u = np.u, p = np.p;
if(g[u].size() == 1 && u != 1) {
dp[u][0] = dp[u][1] = sum[u][0] = sum[u][1] = 0;
for(int i = 2; i <= k; i++)
dp[u][i] = LLONG_MAX, sum[u][i] = 0;
} else {
vector<int> dp_c(k + 1, LLONG_MAX), sum_c(k + 1, 0);
dp_c[0] = sum_c[0] = 0;
for(auto &[v, w] : g[u]) {
if(v == p) continue;
vector<int> sum_ch(k + 1, 0);
for(int i = 0; i <= k; i++) {
if(dp[v][i] == LLONG_MAX && i != 0)
sum_ch[i] = LLONG_MAX;
else sum_ch[i] = sum[v][i] + i * w;
}
vector <int> dp_ch(k + 1, LLONG_MAX);
for(int i = 0; i <= k; i++) dp_ch[i] = dp[v][i];
vector <int> temp_dp(k + 1, LLONG_MAX), temp_sum(k + 1, 0);
for(int i1 = 0; i1 <= k; i1++) {
if(dp_c[i1] == LLONG_MAX) continue;
for(int i2 = 0; i2 <= k - i1; i2++) {
if(dp_ch[i2] == LLONG_MAX) continue;
int new_m = i1 + i2;
if(new_m > k) continue;
if(sum_ch[i2] == LLONG_MAX) continue;
int tmp_sum = sum_c[i1] + sum_ch[i2];
int tmp_dp = dp_c[i1] + dp_ch[i2] + i1 * sum_ch[i2] + i2 * sum_c[i1];
if(tmp_dp < temp_dp[new_m]) {
temp_dp[new_m] = tmp_dp;
temp_sum[new_m] = tmp_sum;
}
}
}
dp_c = move(temp_dp);
sum_c = move(temp_sum);
}
for(int i = 0; i <= k; i++) {
dp[u][i] = dp_c[i];
sum[u][i] = sum_c[i];
}
}
}
printf("Case #%lld: %lld\n", _, dp[1][k]);
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3764kb
input:
2 4 2 1 2 2 1 3 3 1 4 4 4 3 1 2 2 1 3 3 1 4 4
output:
Case #1: 5 Case #2: 18
result:
ok 6 tokens
Test #2:
score: -100
Wrong Answer
time: 1644ms
memory: 174792kb
input:
1000 9 4 7 9 5 5 7 10 4 5 8 6 7 7 2 9 7 1 6 2 3 6 6 8 3 3 10 4 3 2 11 6 3 2 5 3 3 10 2 9 9 5 5 8 9 4 4 2 4 1 3 10 7 5 12 5 2 3 5 4 1 3 10 2 5 13 4 5 11 9 2 3 8 16 9 3 4 2 9 12 6 2 5 5 6 2 4 5 14 7 4 4 1 7 12 10 2 5 6 11 8 5 4 2 8 6 4 6 13 1 8 15 9 5 17 7 4 3 10 2 6 3 2 9 9 2 3 6 15 5 3 12 1 3 4 8 1 ...
output:
Case #1: 9223372036854775807 Case #2: 134 Case #3: 24 Case #4: 9223372036854775807 Case #5: 15 Case #6: 29 Case #7: 9223372036854775807 Case #8: 198 Case #9: 168 Case #10: 101 Case #11: 419 Case #12: 357 Case #13: 61 Case #14: 262 Case #15: 1089 Case #16: 450 Case #17: 9223372036854775807 Case #18: ...
result:
wrong answer 3rd words differ - expected: '151', found: '9223372036854775807'