Submission #1157221

#TimeUsernameProblemLanguageResultExecution timeMemory
1157221TroySerCyberland (APIO23_cyberland)C++20
44 / 100
84 ms16964 KiB
#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 numSpecialsUsed;
};

double binExp(double base, ll numTimes) {
    if (numTimes == 0) return 1.0;

    if (numTimes % 2 == 0) {
        double res = binExp(base, numTimes / 2);
        return res * res;
    } else {
        double res = binExp(base, numTimes - 1);
        return res * base;
    }
}

bool myCompFunc(NodeState ns1, NodeState ns2) {
    if (ns1.distance == ns2.distance) {
        if (ns1.numSpecialsUsed == ns2.numSpecialsUsed) {
            return (ns1.currentNode < ns2.currentNode);
        }
        return ns1.numSpecialsUsed < ns2.numSpecialsUsed;
    }
    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 dijkstras(ll H, ll maxSpecials, vector<int> &A) {

    ll N = adjList.size();
    
    // {distance, node, number of times special is used}
    priority_queue<NodeState, vector<NodeState>, function<bool(NodeState, NodeState)> > pq(myCompFunc);
    pq.push({0.0, H, 0});

    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;
            if (currentSpecials <= 69) {
                newDist += (weightMat[{currentNode, v}]) / binExp(2.0, currentSpecials);
            }

            if (newDist < distances[v]) {

                distances[v] = newDist;
                pq.push({newDist, v, currentSpecials});

                if (A[currentNode] == 2 && currentSpecials < maxSpecials) {
                    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 dijkstrasToOrigin(ll H, ll maxSpecials, vector<int> &A) {

    ll N = adjList.size();
    
    // {distance, node, number of times special is used}
    priority_queue<NodeState, vector<NodeState>, function<bool(NodeState, NodeState)> > pq(myCompFunc);
    pq.push({0.0, H, 0});

    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;
            if (currentSpecials <= 69) {
                newDist += (weightMat[{currentNode, v}]) / binExp(2.0, currentSpecials);
            }

            if (newDist < distances[v]) {

                distances[v] = newDist;
                pq.push({newDist, v, currentSpecials});

                if (A[currentNode] == 2 && currentSpecials < maxSpecials) {
                    pq.push({newDist, v, currentSpecials + 1});
                }

            }

        }

    }

    return distances[0];

}

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(dijkstras(H, K - 1, arr), dijkstrasToOrigin(H, K, arr));

    return response;

}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...