Contest Duration: - (local time) (100 minutes) Back to Home

Submission #6839848

Source Code Expand

Copy
```#!/usr/bin/env python3
import sys
from collections import deque
INF = float("inf")

# 有効グラフを仮定する。
class Graph(object):
def __init__(self, N):
self.N = N
self.V = list(range(N))
self.E = [[] for _ in range(N)]

"""辺を加える。edgeは(始点, 終点、重み)からなるリスト
重みがなければ、重み1とする。
"""
if len(edge) == 2:
edge.append(1)
elif len(edge) != 3:
pass

s, t, w = edge
self.E[s].append([t, w])

pass

def BellmanFord(g: Graph, s):
"""ベルマンフォード法による最短経路
"""
N = g.N
dist = [INF]*N
dist[s] = 0
prev = [None]*N

# 辺の緩和
for _ in range(N):
for from_ in range(N):      # ここの２重ループはO(M)
for to_, weight in g.E[from_]:
if dist[to_] > dist[from_] + weight:
dist[to_] = dist[from_] + weight
prev[to_] = from_
# 負の重みの閉路がないかチェック
for from_ in range(N):
for to_, weight in g.E[from_]:
if dist[from_]+weight < dist[to_]:
return None, None

return dist, prev

def solve(N: int, M: int, P: int, A: "List[int]", B: "List[int]", C: "List[int]"):
for i in range(M):
C[i] = P - C[i]

rev_g = Graph(N)
for a, b, c in zip(A, B, C):

# Nに到達できないノードは除く
visitable = [False]*N
visitable[N-1] = True
q = deque()
q.append(N-1)
while len(q) > 0:
curr = q.popleft()
for t_, w in rev_g.E[curr]:
if not visitable[t_]:
visitable[t_] = True
q.append(t_)

g = Graph(N)
for a, b, c in zip(A, B, C):
if visitable[a-1] and visitable[b-1]:

dist, prev = BellmanFord(g, 0)
if dist is None:
print(-1)
else:
print(max(-dist[-1], 0))

# print(longest)
return

def main():

def iterate_tokens():
for line in sys.stdin:
for word in line.split():
yield word
tokens = iterate_tokens()
N = int(next(tokens))  # type: int
M = int(next(tokens))  # type: int
P = int(next(tokens))  # type: int
A = [int()] * (M)  # type: "List[int]"
B = [int()] * (M)  # type: "List[int]"
C = [int()] * (M)  # type: "List[int]"
for i in range(M):
A[i] = int(next(tokens))
B[i] = int(next(tokens))
C[i] = int(next(tokens))
solve(N, M, P, A, B, C)

if __name__ == '__main__':
main()
```

#### Submission Info

Submission Time 2019-08-11 07:56:48+0900 E - Coins Respawn toot_tatsuhiro Python (3.4.3) 0 2766 Byte TLE 2104 ms 6132 KB

#### Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 500
Status
 AC × 3
 AC × 35 TLE × 21
Set Name Test Cases
Sample a01, a02, a03
All a01, a02, a03, b04, b05, b06, b07, b08, b09, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19, b20, b21, b22, b23, b24, b25, b26, b27, b28, b29, b30, b31, b32, b33, b34, b35, b36, b37, b38, b39, b40, b41, b42, b43, b44, b45, b46, b47, b48, b49, b50, b51, b52, b53, b54, b55, b56
Case Name Status Exec Time Memory
a01 AC 21 ms 3316 KB
a02 AC 21 ms 3316 KB
a03 AC 21 ms 3316 KB
b04 AC 21 ms 3316 KB
b05 AC 21 ms 3316 KB
b06 AC 21 ms 3316 KB
b07 AC 21 ms 3316 KB
b08 AC 21 ms 3316 KB
b09 AC 1656 ms 5236 KB
b10 AC 1678 ms 5228 KB
b11 AC 1684 ms 5108 KB
b12 TLE 2104 ms 5108 KB
b13 AC 1989 ms 5108 KB
b14 AC 1628 ms 5108 KB
b15 AC 36 ms 4596 KB
b16 AC 36 ms 4596 KB
b17 AC 36 ms 4596 KB
b18 AC 1686 ms 5224 KB
b19 AC 1766 ms 5232 KB
b20 AC 1584 ms 5108 KB
b21 AC 1619 ms 5108 KB
b22 AC 617 ms 4596 KB
b23 AC 613 ms 4596 KB
b24 AC 1756 ms 4972 KB
b25 AC 708 ms 3956 KB
b26 TLE 2104 ms 6132 KB
b27 TLE 2104 ms 6132 KB
b28 TLE 2104 ms 6004 KB
b29 TLE 2104 ms 6004 KB
b30 TLE 2104 ms 6004 KB
b31 TLE 2104 ms 6004 KB
b32 TLE 2104 ms 6000 KB
b33 TLE 2104 ms 5872 KB
b34 TLE 2104 ms 5860 KB
b35 TLE 2104 ms 5872 KB
b36 TLE 2024 ms 5748 KB
b37 AC 1964 ms 5744 KB
b38 AC 1847 ms 5748 KB
b39 AC 1673 ms 5620 KB
b40 AC 1547 ms 5620 KB
b41 AC 1409 ms 5492 KB
b42 AC 648 ms 5236 KB
b43 TLE 2104 ms 5868 KB
b44 TLE 2104 ms 5876 KB
b45 TLE 2104 ms 5876 KB
b46 TLE 2104 ms 5876 KB
b47 TLE 2104 ms 5748 KB
b48 AC 606 ms 4340 KB
b49 AC 1659 ms 5108 KB
b50 TLE 2104 ms 5232 KB
b51 AC 1672 ms 5216 KB
b52 TLE 2104 ms 5228 KB
b53 AC 1521 ms 4980 KB
b54 AC 1769 ms 5492 KB
b55 TLE 2104 ms 5948 KB
b56 TLE 2104 ms 5988 KB