QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#155644 | #2161. The Cost of Speed Limits | warwolf | ML | 0ms | 0kb | C++23 | 1.7kb | 2023-09-01 22:38:41 | 2023-09-01 22:38:42 |
answer
#include <bits/stdc++.h>
#define maxn 20005
using namespace std;
int n, c, m;
unsigned f[maxn][maxn];
vector<pair<int, int> > v[maxn];
vector<int> w;
int val[maxn];
void dfs(int i, int fa){
for(auto it : v[i]){
int to = it.first, co = it.second;
if(to == fa) continue;
val[to] = co;
dfs(to, i);
}
unsigned sum = 0;
for(auto it : v[i]){
int to = it.first, co = it.second;
if(to == fa) continue;
unsigned mn = -1;
for(int j = 0;j < m;j++) mn = min(mn, f[to][j]);
sum += mn;
}
for(int j = val[i];j < m;j++){
f[i][j] = 0;
for(auto it : v[i]){
int to = it.first;
if(to == fa) continue;
if(f[to][j] == -1){
f[i][j] = -1;
break;
}
f[i][j] += f[to][j];
}
f[i][j] = min(f[i][j], sum + c * (unsigned)v[i].size());
if(i != 1) f[i][j] += w[j] - w[val[i]];
// printf("%d %d %u--\n", i, j, f[i][j]);
}
}
int main(){
scanf("%d%d", &n, &c);
for(int i = 1;i < n;i++){
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
v[x].push_back({y, z}), v[y].push_back({x, z});
w.push_back(z);
}
if(n==1){
printf("0\n");
return 0;
}
sort(w.begin(), w.end());
w.erase(unique(w.begin(), w.end()), w.end());
m = w.size();
for(int i = 1;i <= n;i++){
for(auto &it : v[i]){
it.second = lower_bound(w.begin(), w.end(), it.second) - w.begin();
}
}
memset(f, -1, sizeof(f));
dfs(1, 0);
printf("%u", *min_element(f[1], f[1] + m));
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Memory Limit Exceeded
input:
13 20 1 8 101 1 9 30 1 2 100 1 3 100 2 4 75 2 5 70 2 6 82 2 7 77 3 10 73 3 11 69 3 12 83 3 13 79