Submission #1158490

#TimeUsernameProblemLanguageResultExecution timeMemory
1158490countless사이버랜드 (APIO23_cyberland)C++20
44 / 100
951 ms2162688 KiB
#include "cyberland.h"

#include <vector>
#include <queue>
#include <iostream>
#include <math.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

const ld INF = 9e18;

struct T {
    ld cdist;
    int k;
    int node;

    bool operator>(const T &other) const {
        if (cdist != other.cdist) return cdist > other.cdist;
        if (k != other.k) return k > other.k;
        return node > other.node;
    }
};

ld divByPow2(int x, int k) {
    return x / (ull)pow(2, k);
}

void checkReal(int start, int H, vector<vector<pair<int, int>>> &adj, vector<bool> &real) {
    real[start] = true;

    if (start == H) return;

    for (const pair<int, int> &i : adj[start]) {
        if (!real[i.first]) {
            checkReal(i.first, H, adj, real);
        }
    }
}

double solve(int N, int M, int K, int H, std::vector<int> x, std::vector<int> y, std::vector<int> c, std::vector<int> arr) {
    vector<vector<pair<int, int>>> adj(N);

    for (int i = 0; i < M; i++) {
        adj[x[i]].push_back({y[i], c[i]});
        adj[y[i]].push_back({x[i], c[i]});
    }

    // find "real" nodes
    vector<bool> real(N, false);
    checkReal(0, H, adj, real);

    if (!real[H]) return -1;
    // real[H] = false;

    // start djikstra's from H
    ld mn = INF;
    vector<vector<ld>> dist(N, vector<ld>(K + 1, INF));
    dist[H][0] = 0;
    priority_queue<T, vector<T>, greater<T>> pq;
    pq.push({0, 0, H});

    while (!pq.empty()) {
        const auto [cdist, k, node] = pq.top();
        pq.pop();

        if (cdist > dist[node][k]) continue;

        if ((arr[node] == 0 || node == 0) && real[node]) {
            mn = min(mn, cdist);
            continue;
        }

        if (cdist != dist[node][k]) continue;
        for (const pair<int, int> &i : adj[node]) {
            // if (i.first == H) continue;
            if (!real[i.first]) continue;

            if (cdist + divByPow2(i.second, k) < dist[i.first][k]) {
                dist[i.first][k] = cdist + divByPow2(i.second, k);
                pq.push({dist[i.first][k], k, i.first});
            }

            // power-up 2
            if (arr[i.first] == 2 && cdist + divByPow2(i.second, k + 1) < dist[i.first][k+1] && k < K && node != H) {
                dist[i.first][k+1] = cdist + divByPow2(i.second, k + 1);
                pq.push({dist[i.first][k+1], k + 1, i.first});
            }
        }
    }

    return mn == INF ? -1 : mn;
}
#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...