結果

問題 No.3013 ハチマキ買い星人
ユーザー Apollo@Kuro
提出日時 2025-01-03 18:42:32
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 737 ms / 2,000 ms
コード長 1,520 bytes
コンパイル時間 618 ms
コンパイル使用メモリ 81,792 KB
実行使用メモリ 168,920 KB
最終ジャッジ日時 2025-01-25 22:13:58
合計ジャッジ時間 18,524 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 45
権限があれば一括ダウンロードができます

ソースコード

diff #

#GPT_25/1/3
from heapq import heappush, heappop
from collections import defaultdict
import sys
input = sys.stdin.read

def main():
    data = input().split()
    idx = 0
    
    # 入力処理
    N, M, P, Y = map(int, data[idx:idx+4])
    idx += 4
    
    graph = defaultdict(list)
    for _ in range(M):
        A, B, C = map(int, data[idx:idx+3])
        idx += 3
        graph[A].append((B, C))
        graph[B].append((A, C))
    
    shops = []
    for _ in range(P):
        D, E = map(int, data[idx:idx+2])
        idx += 2
        shops.append((D, E))
    
    # Dijkstra法
    def dijkstra(start):
        dist = [float('inf')] * (N + 1)
        dist[start] = 0
        pq = [(0, start)]  # (コスト, ノード)
        
        while pq:
            current_cost, current_node = heappop(pq)
            if current_cost > dist[current_node]:
                continue
            
            for neighbor, cost in graph[current_node]:
                new_cost = current_cost + cost
                if new_cost < dist[neighbor]:
                    dist[neighbor] = new_cost
                    heappush(pq, (new_cost, neighbor))
        
        return dist
    
    # 最短距離計算
    min_cost = dijkstra(1)
    
    # 各店における最大の鉢巻数を計算
    max_hachimaki = 0
    for D, E in shops:
        if min_cost[D] <= Y:
            max_hachimaki = max((Y - min_cost[D]) // E, max_hachimaki)
    
    # 結果出力
    print(max_hachimaki)

if __name__ == "__main__":
    main()
0