Submission #1092671

# Submission time Handle Problem Language Result Execution time Memory
1092671 2024-09-24T17:55:25 Z alexander707070 Fireworks (APIO16_fireworks) C++14
0 / 100
16 ms 30044 KB
#include<bits/stdc++.h>
#define MAXN 300007
using namespace std;

const long long inf=1e17;

int n,m;
int p[MAXN],c[MAXN];
vector< pair<int,int> > v[MAXN],g[MAXN];

long long ans;

vector<long long> dists[5007];
vector<long long> dp[5007];
vector<bool> li[5007];

void dfs(int x,long long d){
	if(v[x].empty()){
		dists[x]={0};
		return;
	}

	for(int i=0;i<v[x].size();i++){
		dfs(v[x][i].first,v[x][i].second+d);

		for(long long f:dists[v[x][i].first])dists[x].push_back(f+v[x][i].second);
	}

	dp[x].resize(dists[x].size());
	li[x].resize(dists[x].size());
}

long long solve(int x,int len){
	if(v[x].empty())return 0;

	if(li[x][len])return dp[x][len];
	li[x][len]=true;

	for(int i=0;i<v[x].size();i++){
		
		long long mins=inf;
		for(int f=0;f<dists[v[x][i].first].size();f++){
			if(dists[x][len]-dists[v[x][i].first][f]>=0){
				mins=min(mins,abs(v[x][i].second-(dists[x][len]-dists[v[x][i].first][f]) )+solve(v[x][i].first,f));
			}
		}

		dp[x][len]+=mins;
	}

	return dp[x][len];
}

int main(){

	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	
	cin>>n>>m;
	n+=m;
	for(int i=2;i<=n;i++){
		cin>>p[i]>>c[i];

		v[p[i]].push_back({i,c[i]});
	}

	dfs(1,0);

	for(int i=0;i<=300;i++)dists[1].push_back(i);

	ans=inf;
	for(int len=0;len<dists[1].size();len++){
		ans=min(ans,solve(1,len));
	}

	cout<<ans<<"\n";

    return 0;
}

Compilation message

fireworks.cpp: In function 'void dfs(int, long long int)':
fireworks.cpp:23:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   23 |  for(int i=0;i<v[x].size();i++){
      |              ~^~~~~~~~~~~~
fireworks.cpp: In function 'long long int solve(int, int)':
fireworks.cpp:39:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   39 |  for(int i=0;i<v[x].size();i++){
      |              ~^~~~~~~~~~~~
fireworks.cpp:42:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |   for(int f=0;f<dists[v[x][i].first].size();f++){
      |               ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
fireworks.cpp: In function 'int main()':
fireworks.cpp:73:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   73 |  for(int len=0;len<dists[1].size();len++){
      |                ~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Runtime error 15 ms 30044 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 16 ms 30044 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 15 ms 30044 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 15 ms 30044 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -