제출 #954931

#제출 시각아이디문제언어결과실행 시간메모리
954931LukapNestabilnost (COI23_nestabilnost)C++14
41 / 100
201 ms216840 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int MAXN = 5e3 + 500; const ll INF = 1e18; int n; int val[MAXN], fk[MAXN]; vector<int> susjedi[MAXN], v; int d[MAXN], parent[MAXN]; ll dp[MAXN][MAXN], najm[MAXN]; void dfs (int x) { for (auto nx: susjedi[x]) { if (parent[x] == nx) continue; d[nx] = d[x] + 1; parent[nx] = x; dfs (nx); } } bool cmp (int x, int y) { return d[x] > d[y]; } int main () { ios_base::sync_with_stdio (false); cin.tie (0); cin >> n; for (int i = 0; i < n; i++) cin >> val[i]; for (int i = 0; i < n; i++) cin >> fk[i]; for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; susjedi[a - 1].push_back (b - 1); susjedi[b - 1].push_back (a - 1); } dfs (0); for (int i = 0; i < n; i++) v.push_back (i); sort (v.begin (), v.end (), cmp); for (auto x: v) { for (int i = 0; i < n; i++) { if (val[x] > i) { dp[x][i] = INF; continue; } dp[x][i] = fk[i]; for (auto nx: susjedi[x]) { if (parent[x] == nx) continue; if ((val[x] + 1 == val[nx] && val[nx] <= i) || (val[x] == i && val[nx] == 0)) dp[x][i] += min (dp[nx][i] - fk[i], najm[nx]); else dp[x][i] += najm[nx]; } } najm[x] = INF; for (int i = 0; i < n; i++) najm[x] = min (najm[x], dp[x][i]); } cout << najm[0]; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...