Submission #1228051

#TimeUsernameProblemLanguageResultExecution timeMemory
1228051DzadzoCyberland (APIO23_cyberland)C++20
Compilation error
0 ms0 KiB
#include <bits/stdc++.h>
using namespace std;

double solve(int N, int M, int K, int H,
             const vector<int>& x,
             const vector<int>& y,
             const vector<int>& c,
             const vector<int>& arr) {
    // build graph
    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]});
    }

    // dp[v][d] = min time to reach v having used d divides
    const double INF = 1e18;
    vector<vector<double>> dp(N, vector<double>(K+1, INF));
    dp[0][0] = 0.0;

    // min‑heap by (time, v, used_divs)
    using State = tuple<double,int,int>;
    priority_queue<State, vector<State>, greater<State>> pq;
    pq.emplace(0.0, 0, 0);

    while (!pq.empty()) {
        auto [t, v, d] = pq.top(); 
        pq.pop();
        if (t > dp[v][d]) continue;
        if (v == H) break;           // once we pop H with current best t, that's optimal
        for (auto [u, w] : adj[v]) {
            // 1) move without using ability
            double t1 = t + w;
            if (t1 < dp[u][d]) {
                dp[u][d] = t1;
                pq.emplace(t1, u, d);
            }
            // 2) if u has reset ability
            if (arr[u] == 0) {
                double t0 = 0.0;
                if (t0 < dp[u][d]) {
                    dp[u][d] = t0;
                    pq.emplace(t0, u, d);
                }
            }
            // 3) if u has divide‑by‑2 and we have quota
            if (arr[u] == 2 && d < K) {
                double t2 = (t + w) / 2.0;
                if (t2 < dp[u][d+1]) {
                    dp[u][d+1] = t2;
                    pq.emplace(t2, u, d+1);
                }
            }
        }
    }

    // answer is the best over all ways to arrive at H
    double ans = INF;
    for (int d = 0; d <= K; d++) 
        ans = min(ans, dp[H][d]);
    return ans == INF ? -1.0 : ans;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, M, K, H;
    cin >> N >> M >> K >> H;
    vector<int> arr(N);
    for (int i = 0; i < N; i++) 
        cin >> arr[i];
    vector<int> X(M), Y(M), C(M);
    for (int i = 0; i < M; i++) {
        cin >> X[i] >> Y[i] >> C[i];
    }

    double res = solve(N, M, K, H, X, Y, C, arr);
    if (res < 0) 
        cout << "-1\n";
    else 
        // print with enough precision
        cout << fixed << setprecision(10) << res << "\n";
    return 0;
}

Compilation message (stderr)

/usr/bin/ld: /tmp/ccUWHwad.o: in function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccawmCnw.o:cyberland.cpp:(.text.startup+0x0): first defined here
/usr/bin/ld: /tmp/ccUWHwad.o: in function `main':
grader.cpp:(.text.startup+0x71e): undefined reference to `solve(int, int, int, int, std::vector<int, std::allocator<int> >, std::vector<int, std::allocator<int> >, std::vector<int, std::allocator<int> >, std::vector<int, std::allocator<int> >)'
collect2: error: ld returned 1 exit status