제출 #756697

#제출 시각아이디문제언어결과실행 시간메모리
756697vioalbert사이버랜드 (APIO23_cyberland)C++17
68 / 100
548 ms13360 KiB
#include "cyberland.h" #include <bits/stdc++.h> using namespace std; #ifdef LOCAL #define debug(...) __VA_ARGS__; #else #define debug(...) #endif const double INF = 1e15; double solve(int N, int M, int K, int H, vector<int> x, vector<int> y, vector<int> c, vector<int> arr) { vector adj(N, vector<pair<int, int>>()); for(int i = 0; i < M; i++) { if(x[i] != H) adj[x[i]].emplace_back(y[i], c[i]); if(y[i] != H) adj[y[i]].emplace_back(x[i], c[i]); } vector<int> mark(N, 0); const auto connected = [&](const auto &self, int u, int target) -> bool { if(u == target) return true; mark[u] = 1; for(auto [v, c] : adj[u]) if(!mark[v]) { if(self(self, v, target)) return true; } return false; }; if(!connected(connected, 0, H)) { return -1.0; } const auto dijkstra = [&](const vector<int> &sources, vector<int> &vis, vector<double> &dist) { using state = pair<double, int>; // dist, node fill(vis.begin(), vis.end(), 0); priority_queue<state, vector<state>, greater<state>> pq; for(int i : sources) pq.push({dist[i], i}); while(!pq.empty()) { int u = pq.top().second; pq.pop(); if(vis[u]) continue; vis[u] = 1; for(auto [v, c] : adj[u]) { if(dist[v] > dist[u] + c) { pq.push({dist[v] = dist[u] + c, v}); } } } }; vector<int> vis(N, 0); vector<int> sources{0}; vector<double> dist(N, INF); for(int i : sources) dist[i] = 0.0; dijkstra(sources, vis, dist); for(int i = 1; i < N; i++) if(vis[i] && arr[i] == 0) sources.push_back(i); fill(dist.begin(), dist.end(), INF); for(int i : sources) dist[i] = 0.0; vector<double> new_dist(N); double ans = INF; for(int it = 0; it < min(K, 95); it++) { // debug(cerr << "it = " << it << '\n'); if(it == 1) { sources.clear(); for(int i = 0; i < N; i++) if(arr[i] != 1 && vis[i]) sources.push_back(i); fill(mark.begin(), mark.end(), 0); bool ok = 0; for(int i : sources) ok |= connected(connected, i, H); if(!ok) break; } if(it > 0) { for(int i = 0; i < N; i++) { if(arr[i] == 0) new_dist[i] = 0.0; else if(arr[i] == 1) new_dist[i] = INF; else { new_dist[i] = INF; for(auto [v, c] : adj[i]) if(v != H) new_dist[i] = min(new_dist[i], (dist[v] + c) / 2.0); } } swap(dist, new_dist); } dijkstra(sources, vis, dist); ans = min(ans, dist[H]); // debug({ // for(int i = 0; i < N; i++) { // cerr << "dist[" << i << "] = " << dist[i] << '\n'; // } // }); } 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...