# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
983089 | vjudge1 | 사이버랜드 (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 <limits>
#include <algorithm>
using namespace std;
const int INF = numeric_limits<int>::max();
double solve(int N, int M, int K, int H, vector<int> x, vector<int> y, vector<int> c, vector<int> arr) {
// Define a graph using adjacency list
vector<vector<pair<int, int>>> graph(N);
// Fill the graph with edges and costs
for (int i = 0; i < M; ++i) {
graph[x[i]].push_back({y[i], c[i]});
graph[y[i]].push_back({x[i], c[i]});
}
// Initialize the distance array to infinity
vector<int> dist(N, INF);
// Priority queue to store vertices and their distances
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
// Push the starting node into the priority queue
pq.push({0, 0});
dist[0] = 0;
while (!pq.empty()) {
// Extract the vertex with the minimum distance
int u = pq.top().second;
int d = pq.top().first;
pq.pop();
// If the extracted vertex is Cyberland, return the distance
if (u == H) return dist[u];
// Iterate through all neighboring vertices of u
for (auto& neighbor : graph[u]) {
int v = neighbor.first;
int weight = neighbor.second;
int new_dist = dist[u] + weight;
// Apply special abilities if necessary
if (arr[v] == 2 && K > 0) {
new_dist /= 2;
K--;
} else if (arr[v] == 0) {
new_dist = 0;
}
// Update the distance if a shorter path is found
if (new_dist < dist[v]) {
dist[v] = new_dist;
pq.push({dist[v], v});
}
}
}
// If Cyberland is not reachable, return -1
return -1;
}
int main() {
// Example usage
cout << solve(3, 2, 30, 2, {1, 2}, {2, 0}, {12, 4}, {1, 2, 1}) << endl; // Output: 4
cout << solve(4, 4, 30, 3, {0, 0, 1, 2}, {1, 2, 3, 3}, {5, 4, 2, 4}, {1, 0, 2, 1}) << endl; // Output: 2
return 0;
}