이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "cyberland.h"
#include <bits/stdc++.h>
using namespace std;
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++) {
adj[x[i]].emplace_back(y[i], c[i]);
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<double> &dist) {
using state = pair<double, int>; // dist, node
vector<int> vis(N, 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> sources{0};
for(int i = 1; i < N; i++) if(arr[i] == 0)
sources.push_back(i);
vector<double> dist(N, 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++) {
// cerr << "it = " << it << '\n';
if(it == 1) {
sources.clear();
for(int i = 0; i < N; i++) if(arr[i] != 1)
sources.push_back(i);
}
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])
new_dist[i] = min(new_dist[i], (dist[v] + c) / 2.0);
}
}
swap(dist, new_dist);
}
dijkstra(sources, dist);
ans = min(ans, dist[H]);
// for(int i = 0; i < N; i++) {
// cerr << "dist[" << i << "] = " << dist[i] << '\n';
// }
}
return ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |