結果
問題 | No.2915 辺更新価値最大化 |
ユーザー | vwxyz |
提出日時 | 2024-10-04 22:20:22 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 644 ms / 2,000 ms |
コード長 | 4,246 bytes |
コンパイル時間 | 141 ms |
コンパイル使用メモリ | 82,300 KB |
実行使用メモリ | 87,172 KB |
最終ジャッジ日時 | 2024-10-04 22:20:31 |
合計ジャッジ時間 | 8,272 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 36 ms
53,260 KB |
testcase_01 | AC | 37 ms
53,816 KB |
testcase_02 | AC | 38 ms
53,796 KB |
testcase_03 | AC | 40 ms
53,636 KB |
testcase_04 | AC | 41 ms
59,068 KB |
testcase_05 | AC | 45 ms
61,552 KB |
testcase_06 | AC | 44 ms
61,488 KB |
testcase_07 | AC | 196 ms
78,044 KB |
testcase_08 | AC | 216 ms
78,012 KB |
testcase_09 | AC | 218 ms
78,424 KB |
testcase_10 | AC | 229 ms
78,132 KB |
testcase_11 | AC | 230 ms
78,488 KB |
testcase_12 | AC | 226 ms
78,168 KB |
testcase_13 | AC | 398 ms
79,692 KB |
testcase_14 | AC | 385 ms
80,424 KB |
testcase_15 | AC | 476 ms
81,684 KB |
testcase_16 | AC | 644 ms
84,548 KB |
testcase_17 | AC | 379 ms
81,664 KB |
testcase_18 | AC | 410 ms
81,720 KB |
testcase_19 | AC | 401 ms
81,332 KB |
testcase_20 | AC | 392 ms
82,784 KB |
testcase_21 | AC | 397 ms
81,884 KB |
testcase_22 | AC | 181 ms
77,548 KB |
testcase_23 | AC | 230 ms
82,312 KB |
testcase_24 | AC | 154 ms
79,512 KB |
testcase_25 | AC | 525 ms
87,172 KB |
testcase_26 | AC | 170 ms
79,148 KB |
testcase_27 | AC | 409 ms
83,984 KB |
ソースコード
import heapq class Graph: def __init__(self,V,edges=None,graph=None,directed=False,weighted=False,inf=float("inf")): self.V=V self.directed=directed self.weighted=weighted self.inf=inf if graph!=None: self.graph=graph """ self.edges=[] for i in range(self.V): if self.weighted: for j,d in self.graph[i]: if self.directed or not self.directed and i<=j: self.edges.append((i,j,d)) else: for j in self.graph[i]: if self.directed or not self.directed and i<=j: self.edges.append((i,j)) """ else: self.edges=edges self.graph=[[] for i in range(self.V)] if weighted: for i,j,d in self.edges: self.graph[i].append((j,d)) if not self.directed: self.graph[j].append((i,d)) else: for i,j in self.edges: self.graph[i].append(j) if not self.directed: self.graph[j].append(i) def Dijkstra(self,s,route_restoration=False): dist=[self.inf]*self.V dist[s]=0 queue=[(0,s)] if route_restoration: parents=[None]*self.V while queue: dx,x=heapq.heappop(queue) if dist[x]<dx: continue for y,dy in self.graph[x]: if dist[y]>dx+dy: dist[y]=dx+dy if route_restoration: parents[y]=x heapq.heappush(queue,(dist[y],y)) if route_restoration: return dist,parents else: return dist def Bellman_Ford(self,s,route_restoration=False): dist=[self.inf]*self.V dist[s]=0 if route_restoration: parents=[None]*self.V for _ in range(self.V-1): for i,j,d in self.edges: if dist[j]>dist[i]+d: dist[j]=dist[i]+d if route_restoration: parents[j]=i if not self.directed and dist[i]>dist[j]+d: dist[i]=dist[j]+d if route_restoration: parents[i]=j negative_cycle=[] for i,j,d in self.edges: if dist[j]>dist[i]+d: negative_cycle.append(j) if not self.directed and dist[i]>dist[j]+d: negative_cycle.append(i) if negative_cycle: is_negative_cycle=[False]*self.V for i in negative_cycle: if is_negative_cycle[i]: continue else: queue=deque([i]) is_negative_cycle[i]=True while queue: x=queue.popleft() for y,d in self.graph[x]: if not is_negative_cycle[y]: queue.append(y) is_negative_cycle[y]=True if route_restoration: parents[y]=x for i in range(self.V): if is_negative_cycle[i]: dist[i]=-self.inf if route_restoration: return dist,parents else: return dist N,M,Q=map(int,input().split()) edges=[] for m in range(M): u,v,w=map(int,input().split()) u-=1;v-=1 w=-w edges.append((u,v,w)) inf=1<<60 G=Graph(N,edges=edges,directed=True,weighted=True,inf=inf) dist=G.Bellman_Ford(0) E=[] for u,v,w in edges: w+=dist[u]-dist[v] E.append((u,v,w)) use=[1]*M for j in map(int,input().split()): j-=1 use[j]^=1 edges=[] for m in range(M): if use[m]: edges.append(E[m]) G=Graph(N,edges=edges,directed=True,weighted=True,inf=inf) ans=G.Dijkstra(0)[N-1] if ans==inf: ans="NaN" else: ans+=dist[N-1]-dist[0] ans=-ans print(ans)