제출 #749235

#제출 시각아이디문제언어결과실행 시간메모리
749235dxz05사이버랜드 (APIO23_cyberland)C++17
97 / 100
3095 ms175216 KiB
#include "cyberland.h" #include <bits/stdc++.h> using namespace std; using i128 = long double; vector<vector<pair<int, i128>>> g; vector<bool> used; vector<int> zeros; void dfs(int v, const vector<int> &arr){ used[v] = true; if (arr[v] == 0 || v == 0) zeros.push_back(v); for (auto [u, w] : g[v]){ if (!used[u]){ dfs(u, arr); } } } double solve(int N, int M, int K, int H, std::vector<int> x, std::vector<int> y, std::vector<int> c, std::vector<int> arr) { K = min(K, 91); g.clear(); g.resize(N); for (int i = 0; i < M; i++) { if (y[i] != H) g[x[i]].emplace_back(y[i], c[i]); if (x[i] != H) g[y[i]].emplace_back(x[i], c[i]); } zeros.clear(); used.assign(N, false); dfs(0, arr); const i128 INF = 2e15; vector<vector<i128>> dp(N, vector<i128>(K + 1, INF)); for (int v : zeros){ dp[v].assign(K + 1, 0); } for (int k = 0; k <= K; k++) { if (k > 0) { for (int i = 0; i < N; i++) { if (dp[i][k - 1] == INF) continue; dp[i][k] = min(dp[i][k], dp[i][k - 1]); if (arr[i] == 0) { dp[i][k] = 0; } } } priority_queue<pair<i128, int>> pq; for (int i = 0; i < N; i++) { if (dp[i][k] != INF) pq.emplace(-dp[i][k], i); } vector<bool> processed(N, false); while (!pq.empty()) { int v = pq.top().second; pq.pop(); if (processed[v]) continue; processed[v] = true; for (auto [u, w]: g[v]) { dp[u][k] = min(dp[u][k], dp[v][k] + w); if (arr[u] == 0) { dp[u][k] = 0; } if (arr[u] == 2 && k < K) { dp[u][k + 1] = min(dp[u][k + 1], (dp[v][k] + w) / 2); } pq.emplace(-dp[u][k], u); } } } i128 ans = INF; for (auto [v, w]: g[H]) { for (int k = 0; k <= K; k++) { if (dp[v][k] == INF) continue; if (arr[H] == 0) return 0; ans = min(ans, dp[v][k] + w); if (k < K && arr[H] == 2) { ans = min(ans, (dp[v][k] + w) / 2); } } } if (ans == INF) return -1; return ans; }
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...