제출 #1153354

#제출 시각아이디문제언어결과실행 시간메모리
1153354TroySer사이버랜드 (APIO23_cyberland)C++20
15 / 100
55 ms13896 KiB
#include <bits/stdc++.h>
#include "cyberland.h"
#include <vector>

using namespace std;
using ll = long long;

const ll INF = 1e15;

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

class UnionFind {
    private:
        ll N;
        vector<ll> p, sz;
    public:
        UnionFind() {}
        UnionFind(ll _N) {
            N = _N;
            p.resize(N); sz.resize(N);
            for (ll i = 0; i < N; i++) {
                p[i] = i; sz[i] = 1;
            }
        }

        ll root(ll x) {
            if (p[x] != x) return p[x] = root(p[x]);
            return p[x];
        }

        void unify(ll x, ll y) {
            x = root(x), y = root(y);
            if (x == y) return;
            if (sz[x] > sz[y]) swap(x, y);
            sz[y] += sz[x];
            p[x] = p[y];
        }
};

UnionFind uf;

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);
    distances[startingNode] = 0;

    while (!pq.empty()) {

        auto [currentDistance, currentNode] = pq.top();
        pq.pop();

        if (currentDistance > distances[currentNode]) {
            continue;
        }

        for (ll v: adjList[currentNode]) {
            if (currentDistance + weightMat[{currentNode, v}] < distances[v]) {
                distances[v] = currentDistance + weightMat[{currentNode, v}];
                pq.push({distances[v], v});
            }
        }

    }

    ll minimumPossible = INF;
    for (ll i = 0; i < N; i++) {
        if ((A[i] == 0 || i == 0) && (uf.root(i) == uf.root(0) && uf.root(i) == uf.root(startingNode))) 
            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);

    uf = UnionFind(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];
        uf.unify(x[i], y[i]);
    }

    if (uf.root(0) != uf.root(H)) return -1.0;

    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...