제출 #554559

#제출 시각아이디문제언어결과실행 시간메모리
554559Zhora_004도로 폐쇄 (APIO21_roads)C++17
14 / 100
2076 ms37644 KiB
#include "roads.h" #include <iostream> #include <cmath> #include <algorithm> #include <vector> #include <set> #include <unordered_set> #include <queue> #include <deque> #include <string> #include <sstream> #include <iomanip> #include <map> #include <unordered_map> #include <stack> #include <cstdio> #include <climits> #include <tuple> #include <ctime> #include <cstring> #include <numeric> #include <functional> #include <chrono> #include <cassert> #include <bitset> #include <fstream> #define sz(a) ((int)((a).size())) using ll = long long; using namespace std; const int inf = 1e9; int n, k; vector<int> a, b, c; vector<vector<pair<int, int>>> tree; vector<vector<ll>> dp; void dfs(int u, int p, int w) { for (pair<int, int>& edge : tree[u]) if (edge.first != p) dfs(edge.first, u, edge.second); int can = k; dp[u][0] = 0; if (p != -1) can--; int cnt = sz(tree[u]); if (p != -1) cnt--; vector<vector<ll>> d(cnt + 1, vector<ll>(can + 1, 1ll * inf * inf)); d[0][0] = 0; int cur = 1; for (int i = 0; i < sz(tree[u]); i++) { int v = tree[u][i].first; if (v == p) continue; for (int j = 0; j <= min(cur, can); j++) { if (cur - 1 >= j) d[cur][j] = d[cur - 1][j] + dp[v][1]; if (j) d[cur][j] = min(d[cur][j], d[cur - 1][j - 1] + dp[v][0]); } cur++; } dp[u][0] = 1ll * inf * inf; for (int j = 0; j <= can; j++) dp[u][0] = min(dp[u][0], d[cnt][j]); if (p != -1) { can = k; d = vector<vector<ll>>(cnt + 1, vector<ll>(can + 1, 1ll * inf * inf)); d[0][0] = 0; cur = 1; for (int i = 0; i < sz(tree[u]); i++) { int v = tree[u][i].first; if (v == p) continue; for (int j = 0; j <= min(cur, can); j++) { if (cur - 1 >= j) d[cur][j] = d[cur - 1][j] + dp[v][1]; if (j) d[cur][j] = min(d[cur][j], d[cur - 1][j - 1] + dp[v][0]); } cur++; } dp[u][1] = 1ll * inf * inf; for (int j = 0; j <= can; j++) dp[u][1] = min(dp[u][1], d[cnt][j]); dp[u][1] += w; } } std::vector<long long> minimum_closure_costs(int N, std::vector<int> U, std::vector<int> V, std::vector<int> W) { n = N; a = U; b = V; c = W; ll tmp = 0; tree = vector<vector<pair<int, int>>>(n); for (int i = 0; i < n - 1; i++) { int u, v, w; u = a[i], v = b[i], w = c[i]; tree[u].push_back({ v, w }); tree[v].push_back({ u, w }); tmp += w; } vector<ll> res; res.push_back(tmp); for (k = 1; k < n; k++) { dp = vector<vector<ll>>(n, vector<ll>(2)); dfs(0, -1, -1); res.push_back(dp[0][0]); } return res; }
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...