# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
983089 | vjudge1 | 사이버랜드 (APIO23_cyberland) | C++17 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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;
}