# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
985151 | Kracken_180 | Cyberland (APIO23_cyberland) | C++17 | 0 ms | 0 KiB |
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();
// Adjacency 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]);
}
// Priority queue to store the states 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);
// Distance table to store the minimum cost to reach each state
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 we find a cheaper way to get here, continue
if (current_cost > dist[u][remaining_k]) {
continue;
}
for (auto &[v, travel_time] : graph[u]) {
double new_cost = current_cost + travel_time;
// Case 1: Normal travel without using special abilities
if (new_cost < dist[v][remaining_k]) {
dist[v][remaining_k] = new_cost;
pq.emplace(new_cost, v, remaining_k);
}
// Case 2: Using 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);
}
// Case 3: Using 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 Cyberland is unreachable
return -1.0;
}
int main() {
// Example input
int N = 4;
int M = 4;
int K = 30;
int H = 3;
vector<int> x = {0, 0, 1, 2};
vector<int> y = {1, 2, 3, 3};
vector<int> c = {5, 4, 2, 4};
vector<int> arr = {1, 0, 2, 1};
cout << solve(N, M, K, H, x, y, c, arr) << endl;
return 0;
}