Submission #964431

# Submission time Handle Problem Language Result Execution time Memory
964431 2024-04-16T20:04:03 Z dio_2 Museum (CEOI17_museum) C++17
25 / 100
1832 ms 787748 KB
#include<bits/stdc++.h>
using namespace std;

using pii = pair<int, int>;
template <typename T> void min_self(T &a, T b) { if(b < a) a = b; }

const int N = 10010;
const int inf = (int)1e9;
int dp[N][N];
vector<pii> g[N];
int n, k, x;

// 25 points
int f[N][N][2];
#define come_back 0
#define leave 1
void dfs(int u, int p = -1){
	f[u][1][come_back] = f[u][1][leave] = 0;
	vector<pii> ch;
	for(auto [v, c] : g[u]) if(v != p){
		dfs(v, u);
		ch.emplace_back(v, c);
	}
	if(ch.empty()) return ;
	if((int)ch.size() == 1){
		for(auto [v, c] : ch){
			for(int vis = 1;vis < k;vis++){ // k <= 100.
				f[u][vis + 1][leave] = c + min(f[v][vis][leave], f[v][vis][come_back]);
				f[u][vis + 1][come_back] = 2 * c + f[v][vis][come_back];
			}
		}
	} else if((int)ch.size() == 2){
		for(int vis = 1;vis < k;vis++){
			for(int vis1 = 0;vis1 <= vis;vis1++){
				int vis2 = vis - vis1;
				auto [v1, c1] = ch[0];
				auto [v2, c2] = ch[1];
				c1 = (vis1 > 0 ? c1 : 0);
				c2 = (vis2 > 0 ? c2 : 0);
				min_self(f[u][vis + 1][come_back], 2 * c1 + 2 * c2 + f[v1][vis1][come_back] + f[v2][vis2][come_back]);
				
				min_self(f[u][vis + 1][leave], 2 * c1 + c2 + f[v1][vis1][come_back] + min(f[v2][vis2][leave], f[v2][vis2][come_back]));
				min_self(f[u][vis + 1][leave], 2 * c2 + c1 + f[v2][vis2][come_back] + min(f[v1][vis1][leave], f[v1][vis1][come_back]));
			}
		}
	} else {
		for(int vis = 1;vis < k;vis++){
			for(int vis1 = 0;vis1 <= vis;vis1++){
				for(int vis2 = 0;vis2 + vis1 <= vis;vis2++){
					int vis3 = vis - vis1 - vis2;
					assert(vis1 + vis2 + vis3 == vis);
					auto [v1, c1] = ch[0];
					auto [v2, c2] = ch[1];
					auto [v3, c3] = ch[2];
					c1 = (vis1 > 0 ? c1 : 0);
					c2 = (vis2 > 0 ? c2 : 0);
					c3 = (vis3 > 0 ? c3 : 0);
					min_self(f[u][vis + 1][come_back], 2 * (c1 + c2 + c3) + f[v1][vis1][come_back] + f[v2][vis2][come_back] + f[v3][vis3][come_back]);
				
					min_self(f[u][vis + 1][leave], 2 * c1 + 2 * c2 + c3 + f[v1][vis1][come_back] + f[v2][vis2][come_back] + min(f[v3][vis3][leave], f[v3][vis3][come_back]));
					min_self(f[u][vis + 1][leave], 2 * c2 + 2 * c3 + c1 + f[v2][vis2][come_back] + f[v3][vis3][come_back] + min(f[v1][vis1][leave], f[v1][vis1][come_back]));
					min_self(f[u][vis + 1][leave], 2 * c1 + 2 * c3 + c2 + f[v1][vis1][come_back] + f[v3][vis3][come_back] + min(f[v2][vis2][leave], f[v2][vis2][come_back]));
				}
			}
		}
	}
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	//~ for(int i = 0;i < N;i++) for(int j = 1;j < N;j++) dp[i][j] = inf;
	for(int a = 0;a < N;a++) for(int b = 1;b < N;b++) for(int c = 0;c < 2;c++) f[a][b][c] = inf;
	cin >> n >> k >> x;
	
	for(int i = 1;i <= n - 1;i++){
		int a, b, c;
		cin >> a >> b >> c;
		g[a].emplace_back(b, c);
		g[b].emplace_back(a, c);
	}
	
	dfs(x);
	cout << min(f[x][k][come_back], f[x][k][leave]) << '\n';
	
	return 0;
}

/*

*/
# Verdict Execution time Memory Grader output
1 Incorrect 439 ms 786668 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 310 ms 786944 KB Output is correct
2 Correct 325 ms 786980 KB Output is correct
3 Correct 287 ms 787748 KB Output is correct
4 Correct 292 ms 787188 KB Output is correct
5 Correct 287 ms 787056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 310 ms 786944 KB Output is correct
2 Correct 325 ms 786980 KB Output is correct
3 Correct 287 ms 787748 KB Output is correct
4 Correct 292 ms 787188 KB Output is correct
5 Correct 287 ms 787056 KB Output is correct
6 Incorrect 1832 ms 787056 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 439 ms 786668 KB Output isn't correct
2 Halted 0 ms 0 KB -