This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define Y8o "Museum"
#define maxn (int) 1e4 + 5
#define ll long long
#define pii pair<int, int>
#define gb(i, j) ((i >> j) & 1)
#define all(x) x.begin(), x.end()
#define _left id * 2, l, mid
#define _right id * 2 + 1, mid + 1, r
#define fi(i, a, b) for(int i = a; i <= b; i ++)
#define fid(i, a, b) for(int i = a; i >= b; i --)
//#define f first
//#define s second
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll GetRandom(ll l, ll r) {
return uniform_int_distribution<ll> (l, r) (rng);
}
void iof() {
ios_base::sync_with_stdio(0);
cin.tie(NULL), cout.tie(NULL);
if(fopen(Y8o".inp", "r"))
{
freopen(Y8o".inp", "r", stdin);
// freopen(Y8o".out", "w", stdout);
}
}
void ctime() {
cerr << "\n" << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
}
const ll INF = 1e18;
int n, k, st;
vector<pii> o[maxn];
int h[maxn];
vector<ll> dp[maxn][2];
vector<ll> cb(vector<ll> A, vector<ll> B, ll w) {
vector<ll> ans (A.size() + B.size() - 1, INF);
fi(i, 0, A.size() - 1) {
fi(j, 0, B.size() - 1) {
ans[i + j] = min(ans[i + j], A[i] + B[j] + (j > 0) * w);
}
}
return ans;
}
vector<ll> minimum(vector<ll> A, vector<ll> B) {
if(A.size() > B.size()) swap(A, B);
while(A.size() < B.size()) A.push_back(INF);
fi(i, 0, A.size() - 1) A[i] = min(A[i], B[i]);
return A;
}
void dfs(int u, int p = 0) {
dp[u][0] = { 0, 0 };
dp[u][1] = { 0, -h[u] };
for(auto [v, w] : o[u]) if(v != p) {
h[v] = h[u] + w;
dfs(v, u);
vector<ll> tmp1 = cb(dp[u][0], dp[v][1], 2*w);
vector<ll> tmp2 = cb(dp[u][1], dp[v][0], 2*w);
dp[u][0] = cb(dp[u][0], dp[v][0], 2*w);
dp[u][1] = minimum(tmp1, tmp2);
}
}
void solve() {
cin >> n >> k >> st;
fi(i, 1, n - 1) {
int u, v, w; cin >> u >> v >> w;
o[u].push_back({ v, w }), o[v].push_back({ u, w });
}
dfs(st);
cout << dp[st][1][k];
}
int main() {
iof();
int nTest = 1;
// cin >> nTest;
while(nTest --) {
solve();
}
ctime();
return 0;
}
Compilation message (stderr)
museum.cpp: In function 'std::vector<long long int> cb(std::vector<long long int>, std::vector<long long int>, long long int)':
museum.cpp:10:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
10 | #define fi(i, a, b) for(int i = a; i <= b; i ++)
......
44 | fi(i, 0, A.size() - 1) {
| ~~~~~~~~~~~~~~~~~~
museum.cpp:44:5: note: in expansion of macro 'fi'
44 | fi(i, 0, A.size() - 1) {
| ^~
museum.cpp:10:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
10 | #define fi(i, a, b) for(int i = a; i <= b; i ++)
......
45 | fi(j, 0, B.size() - 1) {
| ~~~~~~~~~~~~~~~~~~
museum.cpp:45:9: note: in expansion of macro 'fi'
45 | fi(j, 0, B.size() - 1) {
| ^~
museum.cpp: In function 'std::vector<long long int> minimum(std::vector<long long int>, std::vector<long long int>)':
museum.cpp:10:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
10 | #define fi(i, a, b) for(int i = a; i <= b; i ++)
......
55 | fi(i, 0, A.size() - 1) A[i] = min(A[i], B[i]);
| ~~~~~~~~~~~~~~~~~~
museum.cpp:55:5: note: in expansion of macro 'fi'
55 | fi(i, 0, A.size() - 1) A[i] = min(A[i], B[i]);
| ^~
museum.cpp: In function 'void iof()':
museum.cpp:27:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
27 | freopen(Y8o".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |