#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
#define OK puts("OK");
#define NO puts("NO");
#define YES puts("YES");
#define fr first
#define sc second
#define ret return
#define scanl(a) scanf("%lld",&a);
#define scanll(a,b) scanf("%lld %lld",&a, &b);
#define scanlll(a,b,c) scanf("%lld %lld %lld",&a,&b,&c);
#define scan1(a) scanf("%lld",&a);
#define scan2(a,b) scanf("%d %lld",&a, &b);
#define scan3(a,b,c) scanf("%d %d %d",&a,&b,&c);
#define all(s) s.begin(),s.end()
#define allr(s) s.rbegin(),s.rend()
#define pb push_back
#define sz(v) (int)v.size()
#define umax(a,b) a = max(a,b);
#define umin(a,b) a = min(a,b);
#define endi puts("");
#define eps 1e-12
const int N = 1e5+12,INF=1e18+7;
int q[N],res,dp[1001][1001];
vector <pii> g[N],ver[N];
void dfs(int x,int gl){
if (g[x].empty())ret ;
for (pii to:g[x])
dfs(to.fr,gl+1);
int i,j;
for (pii to:g[x]){
for (i=0;i<=600;++i){
dp[x][i]=0;
int mn = INF;
for (j=0;j<=i;++j){
mn = min(mn,dp[to.fr][i-j]+abs(to.sc-j));
}
dp[x][i] += mn;
}
}
}
main(){
int n,m,i,j,ans=INF,sum=0;
cin>>n>>m;
for (i=0;i<=1000;++i){
for (j=0;j<=1000;++j){
dp[i][j] = INF;
}
}
for (i=2;i<=m+n;++i){
int x,y;
cin>>x>>y;
g[x].pb({i,y});
if (i > n){
dp[i][0] = 0;
}
}
dfs(1,0);
for (i=1;i<=1000;++i){
ans = min(ans,dp[1][i]);
}
cout <<ans;
}
Compilation message
fireworks.cpp:54:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
54 | main(){
| ^~~~
fireworks.cpp: In function 'int main()':
fireworks.cpp:55:25: warning: unused variable 'sum' [-Wunused-variable]
55 | int n,m,i,j,ans=INF,sum=0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
9 ms |
12748 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
10 ms |
12812 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
9 ms |
12748 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
9 ms |
12748 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |