Submission #1088854

# Submission time Handle Problem Language Result Execution time Memory
1088854 2024-09-15T10:31:44 Z SamNguyen Museum (CEOI17_museum) C++14
20 / 100
29 ms 45916 KB
#include <bits/stdc++.h>
using namespace std;

#define INPFILE ".inp"
#define OUTFILE ".out"

const int INF = 3e8 + 7;

template <class T1, class T2>
inline bool minimise(T1 &x, T2 y) {
	if (x > y) { x = y; return true; }
	return false;
}

template <class T>
inline void minimise_pair(pair<T, T> &x, T y) {
	if (x.second > y)
		swap(x.second, y);
	if (x.first > x.second)
		swap(x.second, x.first);
}

const int MAX = 1e4 + 3;
int N, K, ROOT;
vector<pair<int, int>> adj[MAX];

void input() {
	cin >> N >> K >> ROOT;
	for (int t = N - 1; t--; ) {
		int u, v, w; cin >> u >> v >> w;
		adj[u].emplace_back(v, w);
		adj[v].emplace_back(u, w);
	}

	--K;
}

int dp[MAX][MAX], sz[MAX];

void dfs(int u, int p) {
	sz[u] = 1;
	for (const auto &e : adj[u]) {
		int v, w;
		tie(v, w) = e;
		if (v == p) continue;
		dfs(v, u);
		sz[u] += sz[v];
	}

	static int g[MAX][2];
	for (int x = 0; x <= min(K, sz[u]); x++) 
		g[x][false] = g[x][true] = INF;
	g[0][false] = 0;

	dp[u][0] = 0;
	for (const auto &e : adj[u]) {
		int v, w;
		tie(v, w) = e;
		if (v == p) continue;

		for (int s = min(K, sz[u]); s >= 0; s--) 
		for (int x = 0, y = s; y >= 1; x++, y--) {
			minimise(g[s][false], g[x][false] + 2 * (w + dp[v][y - 1]));
			minimise(g[s][true],  g[x][true]  + 2 * (w + dp[v][y - 1]));
			minimise(g[s][true],  g[x][false] +     (w + dp[v][y - 1]));
		}

		for (int k = 1; k <= min(K, sz[u]); k++) 
			minimise(dp[u][k], g[k][true]);
	}
}

void solve() {
	if (K == 1) {
		cout << 0;
		return;
	}

	for (int u = 1; u <= N; u++)
	for (int k = 0; k <= K; k++)
		dp[u][k] = INF;
	
	adj[0].emplace_back(ROOT, 0);
	dfs(0, -1);

	int ans = dp[ROOT][K];
	cout << ans;
}
	
int main(void) {
	if (fopen(INPFILE, "r")) {
		freopen(INPFILE, "r", stdin);
		freopen(OUTFILE, "w", stdout);
	}
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

	input();
	solve();

	return 0;
}

Compilation message

museum.cpp: In function 'int main()':
museum.cpp:92:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   92 |   freopen(INPFILE, "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
museum.cpp:93:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   93 |   freopen(OUTFILE, "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
3 Correct 0 ms 604 KB Output is correct
4 Correct 0 ms 604 KB Output is correct
5 Correct 0 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 29 ms 45916 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 29 ms 45916 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
3 Correct 0 ms 604 KB Output is correct
4 Correct 0 ms 604 KB Output is correct
5 Correct 0 ms 604 KB Output is correct
6 Incorrect 29 ms 45916 KB Output isn't correct
7 Halted 0 ms 0 KB -