#include "bits/stdc++.h"
using namespace std;
#define vec vector
#define all(x) (x).begin(), (x).end()
double solve(int N, int M, int K, int H, vector<int> X, vector<int> Y, vector<int> C, vector<int> S) {
const double INF = numeric_limits<double>::max();
map<pair<int, int>, double> edges;
vector<vector<pair<int, double>>> adj(N);
for (int i = 0; i < M; i++) {
int u = X[i], v = Y[i];
double cost = static_cast<double>(C[i]);
adj[u].push_back({v, cost});
adj[v].push_back({u, cost});
edges[{u, v}] = cost;
edges[{v, u}] = cost;
}
vector<double> dist(N, INF);
dist[0] = 0;
priority_queue<pair<double, int>, vector<pair<double, int>>, greater<pair<double, int>>> pq;
pq.push({0, 0});
while (!pq.empty()) {
auto [d, u] = pq.top();
pq.pop();
if (d > dist[u]) continue;
for (auto [v, cost] : adj[u]) {
if (dist[v] > d + cost) {
dist[v] = d + cost;
pq.push({dist[v], v});
}
}
}
if (dist[H] == INF) return -1;
if (N == 2) {
return dist[H];
}
// Subtask O1
if (N == 3) {
int other = (H == 1 ? 2 : 1);
double result = INF;
if (edges.count({0, H})) {
result = edges[{0, H}];
}
if (edges.count({0, other}) && edges.count({other, H})) {
if (S[other] == 0)
result = min(result, edges[{other, H}]);
if (S[other] == 1)
result = min(result, dist[H]);
if (S[other] == 2)
result = min(result, edges[{0, other}] / 2.0 + edges[{other, H}]);
}
return result;
}
vector<double> backdist(N, INF);
backdist[H] = 0;
priority_queue<pair<double, int>, vector<pair<double, int>>, greater<pair<double, int>>> backpq;
backpq.push({0, H});
while (!backpq.empty()) {
auto [d, u] = backpq.top();
backpq.pop();
if (d > backdist[u]) continue;
for (auto [v, cost] : adj[u]) {
if (backdist[v] > d + cost) {
backdist[v] = d + cost;
backpq.push({backdist[v], v});
}
}
}
if (backdist[0] == INF) return -1;
double cost = backdist[0];
for(int i = 0; i < N; i++) {
if (S[i] == 0 && dist[i] < dist[H]) {
cost = min(cost, backdist[i]);
}
}
return cost;
}
// signed main() {
// cout << solve(3, 2, 30, 2, {1, 2}, {2, 0}, {12, 4}, {1, 2, 1});
// }
# | 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... |