1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| def get_smallest_index(D, V): min = INF idx = 0
for i in range(1, len(D)): if not V[i] and D[i] < min: min = D[i] idx = i
return idx
def dijkstra(A, n): V = [False] * (n + 1) D = [INF] * (n + 1)
for i in range(1, n + 1): D[i] = A[c][i]
V[c] = True
for _ in range(n - 1): current = get_smallest_index(D, V) V[current] = True
for j in range(1, n + 1): if not V[j] and D[current] + A[current][j] < D[j]: D[j] = D[current] + A[current][j]
return D
n, m, c = map(int, input().split()) INF = 1e9 A = [[INF] * (n + 1) for _ in range(n + 1)]
for i in range(m): x, y, z = map(int, input().split()) A[x][y] = z
for i in range(1, n + 1): for j in range(1, n + 1): if i == j: A[i][j] = 0
D = dijkstra(A, n) cnt = 0 time = 0
for x in D[1:]: if x != 0 and x < INF: cnt += 1 time = max(time, x)
print(cnt, time)
|