結果
問題 | No.1545 [Cherry 2nd Tune N] Anthem |
ユーザー |
![]() |
提出日時 | 2021-06-15 23:15:44 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,400 bytes |
コンパイル時間 | 730 ms |
コンパイル使用メモリ | 82,304 KB |
実行使用メモリ | 107,648 KB |
最終ジャッジ日時 | 2024-12-28 02:29:30 |
合計ジャッジ時間 | 27,482 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 31 WA * 35 TLE * 1 |
ソースコード
from heapq import heapify, heappop, heappushfrom collections import dequedef main():N,S,T,K = map(int,input().split()); INF = float("inf")S-=1;T-=1 #0-indexX = list(map(int,input().split()))G = [[] for _ in range(N)]M = int(input())for _ in range(M):A,B,Y = map(int,input().split())A-=1;B-=1G[A].append((Y,B))Q = deque([])Q.append(S)d_bfs = [INF]*Nd_bfs[S] = 1while Q:v = Q.popleft()for cost, u in G[v]:if d_bfs[u] != INF: continued_bfs[u] = d_bfs[v] + 1Q.append(v)if d_bfs[T] == INF:print("Impossible");exit()def dijkstra_heap2(s,G,t,K):INF = float("inf")#S:start, V: node, E: Edge, G: GraphV = len(G)#dp[i][j]: i番目の歌を歌って今j曲目(i番目の歌を含む)if K <= 10:MAX = 20elif K <= 130:MAX = 1000else:MAX = pow(10,5)dp = [[INF]*MAX for _ in range(V)]#d = [INF for _ in range(V)]dp[s][1] = X[s]prev = [[-1]*MAX for _ in range(V)]PQ = []heappush(PQ,(X[s],s,1)) #時間, 位置, 何曲目while PQ:c,v,n = heappop(PQ)if v == t and n >= K:breakif dp[v][n] < c:continuedp[v][n] = cif n+1 >= MAX: #これ以上は配列外参照continuefor cost,u in G[v]:if dp[u][n+1] <= cost + X[u] + dp[v][n]:continuedp[u][n+1] = cost + X[u] + dp[v][n]prev[u][n+1] = vheappush(PQ,(dp[u][n+1], u, n+1))return dp, prevd, keiro = dijkstra_heap2(S,G,T,K)#print(d)#print(keiro)ans = INFnum = INF #何曲かfor i in range(K,len(d[T])):if d[T][i] < ans:ans = d[T][i]num = iif ans == INF:print("Impossible")else:print("Possible")print(ans)print(num)ret = [T+1] #1-index#num -= 1now = Twhile num > 1:pre = keiro[now][num]ret.append(pre+1) #1-indexnow = prenum -= 1ret.reverse()print(*ret)if __name__ == '__main__':main()