QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#59613 | #2161. The Cost of Speed Limits | losPatrons# | ML | 3ms | 4628kb | C++20 | 4.2kb | 2022-10-31 03:33:55 | 2022-10-31 03:33:56 |
Judging History
answer
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <stack>
#include <cassert>
#include <map>
#include <numeric>
#include <cstring>
#include <set>
#include <ctime>
#include <array>
#include <queue>
#include <cmath>
#include <iomanip>
#include <iterator>
#include <bitset>
#include <unordered_map>
#include <complex>
#include <unordered_set>
#include <chrono>
#include <random>
#include <functional>
#include <random>
#include <complex>
using namespace std;
using namespace std::chrono;
class InputReader {
public:
InputReader() {
input_file = stdin;
cursor = 0;
fread(buffer, SIZE, 1, input_file);
}
InputReader(const char *file_name) {
input_file = fopen(file_name, "r");
cursor = 0;
fread(buffer, SIZE, 1, input_file);
}
template<typename T>
inline InputReader &operator>>(T &n) {
while ((buffer[cursor] < '0' || buffer[cursor] > '9') && buffer[cursor] != '-') {
advance();
}
int sign = 1;
if (buffer[cursor] == '-') {
sign = -1;
advance();
}
n = 0;
while ('0' <= buffer[cursor] && buffer[cursor] <= '9') {
n = n * 10 + buffer[cursor] - '0';
advance();
}
n *= sign;
return *this;
}
inline InputReader &operator>>(char *s) {
while (!isalpha(buffer[cursor])) {
advance();
}
int len = 0;
while (isalpha(buffer[cursor])) {
s[len] = buffer[cursor];
len++;
advance();
}
s[len] = 0;
return *this;
}
private:
FILE *input_file;
static const int SIZE = 1 << 17;
int cursor;
char buffer[SIZE];
inline void advance() {
++cursor;
if (cursor == SIZE) {
cursor = 0;
fread(buffer, SIZE, 1, input_file);
}
}
};
const int MAXN = 20000;
const long long INF = 1000000000000000000LL;
vector<pair<int, int>> g[1 + MAXN];
vector<int> v;
int c;
int weight[1 + MAXN];
long long dp0[1 + MAXN];
vector<long long> dp1[1 + MAXN];
void getWeights(int node, int father) {
weight[node] = 1;
int biggest = 0, where = -1;
for (int i = 0; i < g[node].size(); i++) {
int son = g[node][i].first;
if (son != father) {
getWeights(son, node);
weight[node] += weight[son];
if (weight[son] > biggest) {
biggest = weight[son];
where = i;
}
}
}
if (where != -1 && where != 0) {
swap(g[node][0], g[node][where]);
}
}
void DFS(int node, int father) {
int k = v.size();
for (auto [son, s] : g[node]) {
if (son != father) {
DFS(son, node);
dp1[node].resize(k);
long long best = INF;
for (int i = 0; i < k; i++) {
if (s > v[i]) {
dp1[node][i] = INF;
} else {
dp1[node][i] += v[i] - s + min(dp0[son] + c, dp1[son][i]);
best = min(best, v[i] - s + dp1[son][i]);
}
}
dp0[node] += c + min(dp0[son] + c, best);
dp1[son] = {};
}
}
dp1[node].resize(k);
}
int main() {
// ifstream cin("input.txt");
// ofstream cout("output.txt");
ios_base::sync_with_stdio(false);
cin.tie(0);
srand(time(0));
int n;
cin >> n >> c;
for (int i = 1; i < n; i++) {
int a, b, s;
cin >> a >> b >> s;
g[a].push_back({b, s});
g[b].push_back({a, s});
v.push_back(s);
}
sort(v.begin(), v.end());
v.resize(unique(v.begin(), v.end()) - v.begin());
getWeights(1, 0);
DFS(1, 0);
long long ans = dp0[1];
for (int i = 0; i < v.size(); i++) {
ans = min(ans, dp1[1][i]);
}
cout << ans << "\n";
// auto stopTime = high_resolution_clock::now();
// auto duration = duration_cast<milliseconds>(stopTime - startTime);
// cout << "Ellapsed time: " << duration.count() << "\n";
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 3ms
memory: 4628kb
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
output:
272
result:
ok single line: '272'
Test #2:
score: 0
Accepted
time: 2ms
memory: 4352kb
input:
9 10 1 6 26 2 6 27 3 6 28 4 6 29 5 6 30 7 9 14 8 9 1 9 6 10
output:
60
result:
ok single line: '60'
Test #3:
score: 0
Accepted
time: 3ms
memory: 4464kb
input:
7 64 2 1 194 3 1 187 4 2 158 5 1 42 6 5 101 7 5 80
output:
308
result:
ok single line: '308'
Test #4:
score: 0
Accepted
time: 3ms
memory: 4468kb
input:
6 32 2 1 110 3 2 36 4 1 54 5 3 101 6 5 71
output:
178
result:
ok single line: '178'
Test #5:
score: -100
Memory Limit Exceeded
input:
20000 100 2273 4097 98627 14155 14055 33506 16060 6363 28081 14840 12695 23379 11520 7892 5831 6633 13834 73944 19218 19341 62064 14392 160 58289 18147 209 46443 16941 5453 55103 11895 12849 31201 10275 1622 71781 19595 6349 14232 19485 10800 9778 10745 13541 44786 18727 15264 25726 5847 12815 43894...