#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector<vector<ll> > adjList;
map<pair<ll, ll>, double> weightMat;
vector<bool> isReachable;
struct NodeState {
double distance;
ll currentNode;
ll numSpecialsLeft;
};
bool myCompFunc(NodeState ns1, NodeState ns2) {
if (ns1.distance == ns2.distance) {
if (ns1.numSpecialsLeft == ns2.numSpecialsLeft) {
return (ns1.currentNode < ns2.currentNode);
}
return ns1.numSpecialsLeft > ns2.numSpecialsLeft;
}
return ns1.distance < ns2.distance;
}
void dfs(ll node, ll H, const vector<vector<ll>>& adjList, vector<bool>& isReachable) {
if (node == H) return;
for (auto v : adjList[node]) {
if (!isReachable[v]) {
isReachable[v] = true;
dfs(v, H, adjList, isReachable);
}
}
}
double dijkstrasToOrigin(ll H, ll numSpecials, vector<int> &A) {
ll N = adjList.size();
priority_queue<NodeState, vector<NodeState>, function<bool(NodeState, NodeState)> > pq(myCompFunc);
pq.push({0.0, H, numSpecials});
vector<double> distances(N, 1e16);
distances[H] = 0;
while (!pq.empty()) {
// store state of how many Ks were used here
auto [currentDistance, currentNode, currentSpecials] = pq.top();
pq.pop();
for (ll v: adjList[currentNode]) {
double newDist = currentDistance + weightMat[{currentNode, v}];
if (newDist < distances[v]) {
distances[v] = newDist;
pq.push({newDist, v, currentSpecials});
}
if (A[currentNode] == 2 && currentSpecials > 0) {
newDist /= 2;
if (newDist < distances[v]) {
distances[v] = newDist;
pq.push({newDist, v, currentSpecials - 1});
}
}
}
}
return distances[0];
}
double dijkstrasToZeros(ll H, ll numSpecials, vector<int> &A) {
numSpecials--;
ll N = adjList.size();
priority_queue<NodeState, vector<NodeState>, function<bool(NodeState, NodeState)> > pq(myCompFunc);
pq.push({0.0, H, numSpecials});
vector<double> distances(N, 1e16);
distances[H] = 0;
while (!pq.empty()) {
// store state of how many Ks were used here
auto [currentDistance, currentNode, currentSpecials] = pq.top();
pq.pop();
for (ll v: adjList[currentNode]) {
double newDist = currentDistance + weightMat[{currentNode, v}];
if (newDist < distances[v]) {
distances[v] = newDist;
pq.push({newDist, v, currentSpecials});
}
if (A[currentNode] == 2 && currentSpecials > 0) {
newDist /= 2;
if (newDist < distances[v]) {
distances[v] = newDist;
pq.push({newDist, v, currentSpecials - 1});
}
}
}
}
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);
dfs(0, H, adjList, isReachable);
isReachable[0] = true;
if (!isReachable[H]) {
return -1;
}
double response = min(dijkstrasToOrigin(H, K, arr), dijkstrasToZeros(H, K, arr));
return response;
}
# | 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... |