Official

D - 最安経路 / Cheapest Route Editorial by admin

DeepSeek V3

Overview

This is a problem where you need to find the minimum toll from city 1 to any airport city, given that tolls are charged when traveling between cities.

Analysis

The toll for each movement between adjacent cities is calculated as “population of departure city × population of arrival city.” This problem can be interpreted as a graph problem of finding the shortest path (minimum toll) from city 1 to any airport city.

A brute-force approach could be considered, but since the constraints are large (\(N, K \leq 2 \times 10^5\)), we need to use an efficient shortest path algorithm such as Dijkstra’s algorithm. Since tolls are non-negative values (populations are positive integers), Dijkstra’s algorithm is applicable.

Algorithm

Using Dijkstra’s algorithm, we compute the minimum toll from city 1 to all cities. We use a priority queue to sequentially finalize the city with the smallest toll among unfinalized cities. As soon as we reach any airport city, we output the toll at that point and terminate (since we can leave the country once we reach an airport city).

Specific steps: 1. Initialize the minimum toll to each city as infinity, and set the toll for city 1 to 0 2. Add (0, 1) to the priority queue 3. Repeat the following until the queue is empty: - Extract the city with the minimum toll - If that city is an airport city, output the result and terminate - For each adjacent city, compute the current toll + edge toll, and update if the new value is smaller

Complexity

  • Time complexity: \(O((N + K) \log N)\)
  • Space complexity: \(O(N + K)\)

Implementation Notes

  • To handle large inputs, read all input at once using sys.stdin.read() and process with an iterator

  • Use a sufficiently large value (\(10^{18}\)) to represent infinity

  • Represent the graph as an adjacency list, storing the toll for each edge

  • Manage airport cities using a set for efficient membership checking

  • Use a priority queue (heap) to efficiently retrieve the city with the minimum toll

    Source Code

import heapq
import sys

def main():
    data = sys.stdin.read().split()
    if not data:
        return
    
    it = iter(data)
    N = int(next(it)); K = int(next(it)); P = int(next(it))
    M = [0] * (N+1)
    for i in range(1, N+1):
        M[i] = int(next(it))
    
    graph = [[] for _ in range(N+1)]
    for _ in range(K):
        u = int(next(it)); v = int(next(it))
        cost_uv = M[u] * M[v]
        cost_vu = cost_uv
        graph[u].append((v, cost_uv))
        graph[v].append((u, cost_vu))
        
    airports = set()
    for _ in range(P):
        e = int(next(it))
        airports.add(e)
        
    INF = 10**18
    dist = [INF] * (N+1)
    dist[1] = 0
    heap = [(0, 1)]
    
    while heap:
        d, node = heapq.heappop(heap)
        if d != dist[node]:
            continue
        if node in airports:
            print(d)
            return
            
        for neighbor, cost in graph[node]:
            new_d = d + cost
            if new_d < dist[neighbor]:
                dist[neighbor] = new_d
                heapq.heappush(heap, (new_d, neighbor))
                
    ans = INF
    for a in airports:
        if dist[a] < ans:
            ans = dist[a]
    print(ans)

if __name__ == "__main__":
    main()

This editorial was generated by deepseekv3.

posted:
last update: