Submission #111463

# Submission time Handle Problem Language Result Execution time Memory
111463 2019-05-15T12:19:52 Z ecasdqina Fireworks (APIO16_fireworks) C++14
19 / 100
40 ms 1144 KB
#include <bits/stdc++.h>
using namespace std::literals::string_literals;
using i64 = long long;
using std::cout;
using std::endl;
using std::cin;

template<typename T>
std::vector<T> make_v(size_t a){return std::vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
  return std::vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

int main() {
	int n, m; scanf("%d%d", &n, &m);
	std::vector<int> leaf(m);
	std::vector<std::vector<std::pair<int, i64>>> g(n + m); n += m;
	for(int i = 1; i < n; i++) {
		int a; i64 b; scanf("%d%lld", &a, &b);
		
	//	g[i].push_back({a - 1, b});
		g[a - 1].push_back({i, b});
	}
	
	std::vector<i64> dist(n, 0);
	std::function<void (int, int, i64)> dfs = [&](int v, int par, i64 ari) {
		dist[v] = ari;
		for(auto e: g[v]) {
			if(e.first == par) continue;
			dfs(e.first, v, ari + e.second);
		}
	};
	dfs(0, -1, 0);
	for(auto v: dist) assert(v <= 300);
	
	assert(n <= 300);
	
	i64 ans = 1LL << 60;
	auto dp = make_v<i64>(n, 301);
	for(int i = 0; i < dp.size(); i++) for(int j = 0; j < dp[i].size(); j++) dp[i][j] = 1LL << 60;
	std::function<void (int)> solve = [&](int v) {
		if(v > n - m - 1) {
			dp[v][0] = 0;
			return;
		}
		
		for(auto e: g[v]) solve(e.first);
		for(int i = 0; i < dp[v].size(); i++) {
			i64 tmp = 0;
			for(auto e: g[v]) {
				i64 mi = 1LL << 60;
				for(int j = 0; j < dp[e.first].size(); j++) {
					i64 diff = i - (j + e.second);
					if(e.second + diff < 0) continue;
					mi = std::min(mi, dp[e.first][j] + std::abs(diff));
				}
				tmp += mi;
			}
			dp[v][i] = tmp;
		}
	};
	solve(0);
	
	for(auto v: dp[0]) ans = std::min(ans, v);
	printf("%lld\n", ans);
	return 0;
}

Compilation message

fireworks.cpp: In function 'int main()':
fireworks.cpp:42:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < dp.size(); i++) for(int j = 0; j < dp[i].size(); j++) dp[i][j] = 1LL << 60;
                 ~~^~~~~~~~~~~
fireworks.cpp:42:54: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < dp.size(); i++) for(int j = 0; j < dp[i].size(); j++) dp[i][j] = 1LL << 60;
                                                    ~~^~~~~~~~~~~~~~
fireworks.cpp: In lambda function:
fireworks.cpp:50:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0; i < dp[v].size(); i++) {
                  ~~^~~~~~~~~~~~~~
fireworks.cpp:54:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int j = 0; j < dp[e.first].size(); j++) {
                    ~~^~~~~~~~~~~~~~~~~~~~
fireworks.cpp: In function 'int main()':
fireworks.cpp:17:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  int n, m; scanf("%d%d", &n, &m);
            ~~~~~^~~~~~~~~~~~~~~~
fireworks.cpp:21:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int a; i64 b; scanf("%d%lld", &a, &b);
                 ~~~~~^~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Runtime error 2 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 512 KB Output is correct
2 Correct 8 ms 384 KB Output is correct
3 Correct 9 ms 512 KB Output is correct
4 Correct 13 ms 640 KB Output is correct
5 Correct 13 ms 640 KB Output is correct
6 Correct 15 ms 640 KB Output is correct
7 Correct 19 ms 768 KB Output is correct
8 Correct 23 ms 768 KB Output is correct
9 Correct 23 ms 768 KB Output is correct
10 Correct 30 ms 896 KB Output is correct
11 Correct 27 ms 896 KB Output is correct
12 Correct 35 ms 1024 KB Output is correct
13 Correct 32 ms 1024 KB Output is correct
14 Correct 40 ms 1024 KB Output is correct
15 Correct 34 ms 1144 KB Output is correct
16 Correct 36 ms 1024 KB Output is correct
17 Correct 35 ms 1024 KB Output is correct
18 Correct 35 ms 1024 KB Output is correct
19 Correct 34 ms 1024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 2 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 2 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -