제출 #1313661

#제출 시각아이디문제언어결과실행 시간메모리
1313661WeIlIaNMuseum (CEOI17_museum)C++20
20 / 100
537 ms1114112 KiB
#include <bits/stdc++.h>
using namespace std;

#define MOD1 1000000007
#define MOD2 998244353
#define fir first
#define sec second

#define pushf push_front
#define pushb push_back
#define popf pop_front
#define popb pop_back
#define mp make_pair
#define all(a) a.begin(), a.end()
#define lbound(v, x) lower_bound(all(v), x) - v.begin()
#define ubound(v, x) upper_bound(all(v), x) - v.begin()
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)

#define FOR1(a) for (int _ = 0; _ < (a); ++_)
#define FOR2(i, a) for (int i = 0; i < (a); ++i)
#define FOR3(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR1(a) for (int _ = (a)-1; _ >= 0; --_)
#define RFOR2(i, a) for (int i = (a)-1; i >= 0; --i)
#define RFOR3(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define overload3(a, b, c, d, ...) d
#define REP(...) overload3(__VA_ARGS__, FOR3, FOR2, FOR1)(__VA_ARGS__)
#define RREP(...) overload3(__VA_ARGS__, RFOR3, RFOR2, RFOR1)(__VA_ARGS__)

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<vb> vvb;
typedef vector<vc> vvc;
typedef vector<vpii> vvpii;
typedef vector<vpll> vvpll;
typedef queue<int> qi;
typedef queue<ll> qll;
typedef queue<pii> qpii;
typedef queue<pll> qpll;
typedef deque<int> dqi;
typedef deque<ll> dqll;
typedef deque<pii> dqpii;
typedef deque<pll> dqpll;
typedef priority_queue<int> pqi;
typedef priority_queue<ll> pqll;
typedef priority_queue<pii> pqpii;
typedef priority_queue<pll> pqpll;
typedef priority_queue<int, vi, greater<int> > r_pqi;
typedef priority_queue<ll, vll, greater<ll> > r_pqll;
typedef priority_queue<pii, vpii, greater<pii> > r_pqpii;
typedef priority_queue<pll, vpll, greater<pll> > r_pqpll;

const ll inf = 1e16;

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int n;
	int k, r;
	cin>>n>>k>>r;
	vvpii adj(n);
	REP(i, n-1) {
		int a, b, c;
		cin>>a>>b>>c;
		adj[a-1].pushb(mp(b-1, c));
		adj[b-1].pushb(mp(a-1, c));
	}
	vvpll dp(n, vpll(n+1, mp(inf, inf)));
	function<int(int, int)> dfs = [&](int u, int p) {
		int sz = 1;
		dp[u][0] = mp(0, 0);
		dp[u][1] = mp(0, 0);
		for(auto [v, w] : adj[u]) {
			if(v == p) {
				continue;
			}
			// cerr<<u<<' '<<v<<endl;
			int t = dfs(v, u);
			RREP(i, 1, min(k, sz)+1) {
				RREP(j, 1, min(k-i, t)+1) {
					chmin(dp[u][i+j].fir, dp[u][i].sec + dp[v][j].fir + w);
					chmin(dp[u][i+j].fir, dp[u][i].fir + dp[v][j].sec + w*2);

					chmin(dp[u][i+j].sec, dp[u][i].sec + dp[v][j].sec + w*2);
				}
			}
			sz += t;
		}
		// cerr<<u<<' '<<sz<<endl;
		return sz;
	};
	dfs(r-1, -1);
	// REP(i, n) {
	// 	REP(j, n+1) {
	// 		cerr<<(dp[i][j].fir == inf ? -1 : dp[i][j].fir)<<' '<<(dp[i][j].sec == inf ? -1 : dp[i][j].sec)<<"; ";
	// 	}
	// 	cerr<<endl;
	// }
	cout<<dp[r-1][k].fir;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...