# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1153379 | TroySer | 사이버랜드 (APIO23_cyberland) | C++20 | 0 ms | 0 KiB |
#include <bits/stdc++.h>
#include "cyberland.h"
#include <vector>
using namespace std;
using ll = long long;
vector<vector<ll> > adjList;
map<pair<ll, ll>, double> weightMat;
vector<bool> isReachable;
double dijkstras(ll H, vector<int> &A) {
ll N = adjList.size();
priority_queue<pair<double, ll>, vector<pair<double, ll> >, greater<pair<double, ll> > > pq;
pq.push({0.0, H});
// check if H is actually reachable to 0
vector<double> distances(N, 1e16);
vector<bool> visited(N, false);
distances[H] = 0;
while (!pq.empty()) {
auto [currentDistance, currentNode] = pq.top();
pq.pop();
if (visited[currentNode]) continue;
visited[currentNode] = true;
for (ll v: adjList[currentNode]) {
double newDist = currentDistance + weightMat[{currentNode, v}];
if (newDist < distances[v]) {
distances[v] = newDist;
pq.push({newDist, v});
}
}
}
double minimumPossible = distances[0];
for (ll i = 0; i < A.size(); i++) {
if ((A[i] == 0) && isReachable[i])
minimumPossible = min(minimumPossible, distances[i]);
}
return minimumPossible;
}
double solve(int N, int M, int K, int H, vector<int> x, vector<int> y, vector<int> c, vector<int> arr) {
adjList.clear();
weightMat.clear();
adjList.resize(N);
for (ll i = 0; i < M; i++) {
adjList[x[i]].push_back(y[i]);
adjList[y[i]].push_back(x[i]);
weightMat[{x[i], y[i]}] = (double)c[i];
weightMat[{y[i], x[i]}] = (double)c[i];
}
isReachable.clear();
isReachable.resize(N, false);
queue<ll> q; q.push(0);
while (!q.empty()) {
ll currentTop = q.front();
q.pop();
if (currentTop == H) break;
isReachable[currentTop] = true;
for (ll v: adjList[currentTop]) {
if (!isReachable[v]) {
isReachable[v] = true;
q.push(v);
}
}
}
if (!isReachable[H]) {
return -1;
}
double response = dijkstras(H, arr);
if (response >= INF) {
return -1.0;
} else {
return response;
}
}