Submission #1153363

#TimeUsernameProblemLanguageResultExecution timeMemory
1153363TroySerCyberland (APIO23_cyberland)C++20
0 / 100
48 ms13108 KiB
#include <bits/stdc++.h>
#include "cyberland.h"
#include <vector>

using namespace std;
using ll = long long;

const ll INF = 1e16;

vector<vector<ll> > adjList;
map<pair<ll, ll>, ll> weightMat;
vector<bool> isReachable;

double dijkstras(ll startingNode, vector<int> &A) {

    ll N = adjList.size();
    
    priority_queue<pair<ll, ll>, vector<pair<ll, ll> >, greater<pair<ll, ll> > > pq;
    pq.push({0, startingNode});

    // check if H is actually reachable to 0

    vector<ll> distances(N, INF);
    vector<bool> visited(N, false);
    distances[startingNode] = 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({distances[v], v});
            }
        }

    }

    ll minimumPossible = distances[0];
    for (ll i = 0; i < N; i++) {
        if ((A[i] == 0) && isReachable[i]) 
            minimumPossible = min(minimumPossible, distances[i]);
    }

    return (double)(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.resize(N, false);
    queue<ll> q; q.push(0);
    while (!q.empty()) {

        ll currentTop = q.front();
        q.pop();

        if (isReachable[currentTop]) continue;
        isReachable[currentTop] = true;
        if (currentTop == H) break;

        for (auto v: adjList[currentTop]) {
            q.push(v);
        }

    }

    if (!isReachable[H]) {
        return -1;
    }

    double response = dijkstras(H, arr);

    if (response >= INF) {
        return -1.0;
    } else {
        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...