제출 #985152

#제출 시각아이디문제언어결과실행 시간메모리
985152Kracken_180사이버랜드 (APIO23_cyberland)C++17
20 / 100
2127 ms2097152 KiB
#include <iostream>
#include <vector>
#include <queue>
#include <tuple>
#include <limits>
using namespace std;

double solve(int N, int M, int K, int H, vector<int> x, vector<int> y, vector<int> c, vector<int> arr) {
    const double INF = numeric_limits<double>::infinity();
    
    // Adjacency list representation of the graph
    vector<vector<pair<int, int>>> graph(N);
    for (int i = 0; i < M; ++i) {
        graph[x[i]].emplace_back(y[i], c[i]);
        graph[y[i]].emplace_back(x[i], c[i]);
    }
    
    // Priority queue to store the states as (current cost, current country, remaining K)
    priority_queue<tuple<double, int, int>, vector<tuple<double, int, int>>, greater<>> pq;
    pq.emplace(0.0, 0, K);
    
    // Distance table to store the minimum cost to reach each state
    vector<vector<double>> dist(N, vector<double>(K + 1, INF));
    dist[0][K] = 0.0;
    
    while (!pq.empty()) {
        auto [current_cost, u, remaining_k] = pq.top();
        pq.pop();
        
        if (u == H) {
            return current_cost;
        }
        
        // If we find a cheaper way to get here, continue
        if (current_cost > dist[u][remaining_k]) {
            continue;
        }
        
        for (auto &[v, travel_time] : graph[u]) {
            double new_cost = current_cost + travel_time;
            
            // Case 1: Normal travel without using special abilities
            if (new_cost < dist[v][remaining_k]) {
                dist[v][remaining_k] = new_cost;
                pq.emplace(new_cost, v, remaining_k);
            }
            
            // Case 2: Using zeroing ability
            if (arr[v] == 0 && current_cost < dist[v][remaining_k]) {
                dist[v][remaining_k] = current_cost;
                pq.emplace(current_cost, v, remaining_k);
            }
            
            // Case 3: Using halving ability
            if (arr[v] == 2 && remaining_k > 0) {
                double halved_cost = current_cost + travel_time / 2.0;
                if (halved_cost < dist[v][remaining_k - 1]) {
                    dist[v][remaining_k - 1] = halved_cost;
                    pq.emplace(halved_cost, v, remaining_k - 1);
                }
            }
        }
    }
    
    // If Cyberland is unreachable
    return -1.0;
}
#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...