This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
#include <queue>
#include <tuple>
#include <limits>
using namespace std;
double solve(int N, int M, int K, int H, vector<int> x, vector<int> y, vector<int> c, vector<int> arr) {
const double INF = numeric_limits<double>::infinity();
//list representation of the graph
vector<vector<pair<int, int>>> graph(N);
for (int i = 0; i < M; ++i) {
graph[x[i]].emplace_back(y[i], c[i]);
graph[y[i]].emplace_back(x[i], c[i]);
}
//store as (current cost, current country, remaining K)
priority_queue<tuple<double, int, int>, vector<tuple<double, int, int>>, greater<>> pq;
pq.emplace(0.0, 0, K);
//store the minimum cost
vector<vector<double>> dist(N, vector<double>(K + 1, INF));
dist[0][K] = 0.0;
while (!pq.empty()) {
auto [current_cost, u, remaining_k] = pq.top();
pq.pop();
if (u == H) {
return current_cost;
}
// If a cheaper way to get here
if (current_cost > dist[u][remaining_k]) {
continue;
}
for (auto &[v, travel_time] : graph[u]) {
double new_cost = current_cost + travel_time;
//Normal travel
if (new_cost < dist[v][remaining_k]) {
dist[v][remaining_k] = new_cost;
pq.emplace(new_cost, v, remaining_k);
}
//Use zeroing ability
if (arr[v] == 0 && current_cost < dist[v][remaining_k]) {
dist[v][remaining_k] = current_cost;
pq.emplace(current_cost, v, remaining_k);
}
//Use halving ability
if (arr[v] == 2 && remaining_k > 0) {
double halved_cost = current_cost + travel_time / 2.0;
if (halved_cost < dist[v][remaining_k - 1]) {
dist[v][remaining_k - 1] = halved_cost;
pq.emplace(halved_cost, v, remaining_k - 1);
}
}
}
}
// If unreachable
return -1.0;
}
# | 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... |