incomplete 18870.c(unknown boj c11-clang runtime error), 28707.py & complete aloha dijkstra3(1238.py, 1504.py, 17940.py)
This commit is contained in:
40
zeta_python/completed/1238.py
Normal file
40
zeta_python/completed/1238.py
Normal file
@@ -0,0 +1,40 @@
|
||||
import heapq
|
||||
import sys
|
||||
|
||||
input = sys.stdin.readline
|
||||
|
||||
|
||||
def dijkstra(N, E, start) -> list[int]:
|
||||
heap = []
|
||||
distance = [10 ** 9 + 1 for _ in range(N + 1)]
|
||||
distance[start] = 0
|
||||
heapq.heappush(heap, (0, start,))
|
||||
while heap:
|
||||
cost, now, = heapq.heappop(heap)
|
||||
if distance[now] < cost:
|
||||
continue
|
||||
|
||||
for new, weight in E[now]:
|
||||
new_cost = cost + weight
|
||||
if new_cost < distance[new]:
|
||||
distance[new] = new_cost
|
||||
heapq.heappush(heap, (new_cost, new))
|
||||
|
||||
return distance
|
||||
|
||||
|
||||
def solve(N, M, E, iE, X):
|
||||
come = dijkstra(N, E, X)
|
||||
gone = dijkstra(N, iE, X)
|
||||
return max([come[i] + gone[i] for i in range(1, N + 1)])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
N, M, X = map(int, input().split())
|
||||
E = {i: [] for i in range(1, N + 1)}
|
||||
iE = {i: [] for i in range(1, N + 1)}
|
||||
for _ in range(M):
|
||||
A, B, T = map(int, input().split())
|
||||
E[A].append((B, T))
|
||||
iE[B].append((A, T))
|
||||
print(solve(N, M, E, iE, X))
|
||||
47
zeta_python/completed/1504.py
Normal file
47
zeta_python/completed/1504.py
Normal file
@@ -0,0 +1,47 @@
|
||||
import heapq
|
||||
import sys
|
||||
|
||||
input = sys.stdin.readline
|
||||
|
||||
|
||||
def dijkstra(N, E, start) -> list[int]:
|
||||
heap = []
|
||||
distance = [10 ** 9 + 1 for _ in range(N + 1)]
|
||||
distance[start] = 0
|
||||
heapq.heappush(heap, (0, start,))
|
||||
while heap:
|
||||
cost, now, = heapq.heappop(heap)
|
||||
if distance[now] < cost:
|
||||
continue
|
||||
|
||||
for new, weight in E[now]:
|
||||
new_cost = cost + weight
|
||||
if new_cost < distance[new]:
|
||||
distance[new] = new_cost
|
||||
heapq.heappush(heap, (new_cost, new))
|
||||
|
||||
return distance
|
||||
|
||||
|
||||
def solve(N, E, v1, v2) -> int:
|
||||
heap = []
|
||||
distance_from_v1 = dijkstra(N, E, v1, )
|
||||
distance_from_v2 = dijkstra(N, E, v2, )
|
||||
ret = min(
|
||||
(sum((distance_from_v1[1], distance_from_v1[v2], distance_from_v2[N])),
|
||||
sum((distance_from_v2[1], distance_from_v2[v1], distance_from_v1[N])))
|
||||
)
|
||||
return ret if ret < 10 ** 9 + 1 else -1
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
N, e = map(int, input().split())
|
||||
E = {}
|
||||
for i in range(1, N + 1):
|
||||
E[i] = []
|
||||
for _ in range(e):
|
||||
a, b, c = map(int, input().split())
|
||||
E[a].append((b, c))
|
||||
E[b].append((a, c))
|
||||
v1, v2 = map(int, input().split())
|
||||
print(solve(N, E, v1, v2))
|
||||
44
zeta_python/completed/17940.py
Normal file
44
zeta_python/completed/17940.py
Normal file
@@ -0,0 +1,44 @@
|
||||
import heapq
|
||||
import sys
|
||||
|
||||
input = sys.stdin.readline
|
||||
|
||||
INF = 10 ** 9 + 1
|
||||
|
||||
|
||||
def dijkstra(N, V, E, start) -> list[int]:
|
||||
heap = []
|
||||
distance = [INF*INF for _ in range(N)]
|
||||
distance[start] = 0
|
||||
heapq.heappush(heap, (0, start,))
|
||||
while heap:
|
||||
cost, now, = heapq.heappop(heap)
|
||||
if distance[now] < cost:
|
||||
continue
|
||||
|
||||
for new, weight in E[now]:
|
||||
new_cost = cost + weight
|
||||
if V[new] != V[now]:
|
||||
new_cost += INF
|
||||
if new_cost < distance[new]:
|
||||
distance[new] = new_cost
|
||||
heapq.heappush(heap, (new_cost, new))
|
||||
|
||||
return distance
|
||||
|
||||
|
||||
def solve(N, M, V, E):
|
||||
distance = dijkstra(N, V, E, 0)
|
||||
return distance[M] // INF, distance[M] % INF
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
N, M = map(int, input().split())
|
||||
V = [int(input()) for _ in range(N)]
|
||||
E = [[] for i in range(N)]
|
||||
for i in range(N):
|
||||
for j, w in enumerate(map(int, input().split())):
|
||||
if w != 0:
|
||||
E[i].append((j, w))
|
||||
|
||||
print(*solve(N, M, V, E))
|
||||
Reference in New Issue
Block a user