Submission #1358636

#TimeUsernameProblemLanguageResultExecution timeMemory
1358636silence25Cyberland (APIO23_cyberland)C++20
100 / 100
2695 ms92620 KiB
#include "cyberland.h"

#include "bits/stdc++.h"


#define ff first

#define ss second

#define pp pop_back

#define ll long long

#define pb push_back

#define ls(v) (int)v.size()

#define all(v) v.begin(),v.end()

#define rall(v) v.rbegin(),v.rend()

#define wr cout << "------------------------" << endl


using namespace std;

const double INF = 1e18;

const int N = 1e5 + 5;

vector<pair<int, int>> g[N];

struct Node{

    double dist;

    int v;

    int k;

    bool operator>(const Node& other) const {

        return dist > other.dist;

    }

};

double solve(int N, int M, int K, int H, vector<int> x, vector<int> y, vector<int> c, vector<int> arr) {
    
    for(int i = 0;i<M;++i){
    
        g[x[i]].pb({c[i], y[i]});
    
        g[y[i]].pb({c[i], x[i]});
    
    }
    
    K = min(K, 68);
    
    vector<int> vis(N, 0);
    
    queue<int> q;
    
    q.push(0);
    
    vector<int> start = {0};
    
    while(!q.empty()){
    
        int nd = q.front();
    
        q.pop();
    
        vis[nd] = 1;
    
        for(auto it:g[nd]){
    
            if(!vis[it.ss]){
    
                if(it.ss == H) {vis[H] = 1; continue;}
    
                vis[it.ss] = 1;
    
                if(arr[it.ss] == 0) start.pb(it.ss);
    
                q.push(it.ss);
    
            }
    
        }
    
    }
    
    vector<vector<double>> dp(N, vector<double>(K + 1, INF));    

    priority_queue<Node, vector<Node>, greater<Node>> pq;    

        
    for(auto nd:start){

        for(int i = 0;i<=K;++i) dp[nd][i] = 0;
        
        pq.push({0.0, nd, 0});
    }

	while(!pq.empty()){

        auto [cost, xx, k] = pq.top(); pq.pop();

        int x = xx;

        if(dp[x][k] != cost or x == H) continue;

        for(auto [val, y]:g[x]){

            if(arr[y] == 2){

                double req1 = cost + val;

                double req2 = (cost + val) / 2;

                if(dp[y][k] > req1){

                    dp[y][k] = req1;

                    pq.push({req1, y, k});

                }

                if(k + 1 <= K and dp[y][k + 1] > req2){

                    dp[y][k + 1] = req2;

                    pq.push({req2, y, k + 1});
                }

            }

            else if(arr[y]){

                double req = cost + val;

                if(dp[y][k] > req){

                    dp[y][k] = req;

                    pq.push({req, y, k});

                }

            }

        }

    }

    double ans = INF;

    for(int i = 0;i<=K;++i) ans = min(ans, dp[H][i]);

    for(int i = 0;i<N;++i){

        g[i].clear();

    }

    if(ans >= INF) ans = -1;

    return ans;

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