Merge remote-tracking branch 'origin/master'
This commit is contained in:
46
zeta_python/completed/1753.py
Normal file
46
zeta_python/completed/1753.py
Normal file
@@ -0,0 +1,46 @@
|
||||
import sys
|
||||
import heapq as hq
|
||||
|
||||
input = sys.stdin.readline
|
||||
|
||||
|
||||
def solve(V, E, K) -> list[int]:
|
||||
W = [1 << 31 for _ in range(V + 1)]
|
||||
W[K] = 0
|
||||
D = []
|
||||
|
||||
hq.heappush(D, (W[K], K))
|
||||
|
||||
while D:
|
||||
d, k = hq.heappop(D)
|
||||
|
||||
if d > W[k]:
|
||||
continue
|
||||
|
||||
for v, w in E[k]:
|
||||
nd = d + w
|
||||
if W[v] > nd:
|
||||
W[v] = nd
|
||||
hq.heappush(D, (nd, v))
|
||||
|
||||
return W
|
||||
|
||||
|
||||
def pformat(x):
|
||||
if x >= 1 << 31:
|
||||
return 'INF'
|
||||
else:
|
||||
return x
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
V, n = map(int, input().split())
|
||||
K = int(input())
|
||||
E = {}
|
||||
for i in range(1, V + 1):
|
||||
E[i] = []
|
||||
for _ in range(n):
|
||||
u, v, w = map(int, input().split())
|
||||
E[u].append((v, w))
|
||||
|
||||
[print(pformat(i)) for i in solve(V, E, K)[1:]]
|
||||
83
zeta_python/completed/5427.py
Normal file
83
zeta_python/completed/5427.py
Normal file
@@ -0,0 +1,83 @@
|
||||
import sys
|
||||
from collections import deque
|
||||
|
||||
input = sys.stdin.readline
|
||||
|
||||
|
||||
def translate(char):
|
||||
if char == '.':
|
||||
return 0
|
||||
elif char == '#':
|
||||
return 1
|
||||
elif char == '*':
|
||||
return 2
|
||||
else:
|
||||
return 3
|
||||
|
||||
|
||||
def solve(w, h, linelist):
|
||||
Map = [[0] * h for _ in range(w)]
|
||||
firepoint = []
|
||||
startpoint = []
|
||||
|
||||
# preprocess
|
||||
for i, line in enumerate(linelist):
|
||||
for j, c in enumerate(line):
|
||||
t = translate(c)
|
||||
if t == 2:
|
||||
firepoint.append((j, i))
|
||||
elif t == 3:
|
||||
startpoint.append((j, i))
|
||||
Map[j][i] = t
|
||||
|
||||
# run
|
||||
step = 0
|
||||
D = deque()
|
||||
delta = ((-1, 0), (1, 0), (0, -1), (0, 1))
|
||||
for fp in firepoint:
|
||||
D.append((fp, 2))
|
||||
for sp in startpoint:
|
||||
D.append((sp, 3))
|
||||
while True:
|
||||
step += 1
|
||||
firepoint = []
|
||||
startpoint = []
|
||||
while D:
|
||||
point, selector = D.popleft()
|
||||
for dx, dy in delta:
|
||||
new = point[0] + dx, point[1] + dy
|
||||
if 0 <= new[0] < w and 0 <= new[1] < h:
|
||||
if Map[new[0]][new[1]] != 0:
|
||||
continue
|
||||
else:
|
||||
Map[new[0]][new[1]] = selector
|
||||
if selector == 2:
|
||||
firepoint.append(new)
|
||||
else:
|
||||
startpoint.append(new)
|
||||
|
||||
else:
|
||||
if selector == 3:
|
||||
return step
|
||||
else:
|
||||
continue
|
||||
|
||||
for fp in firepoint:
|
||||
D.append((fp, 2))
|
||||
|
||||
if not startpoint:
|
||||
return None
|
||||
for sp in startpoint:
|
||||
D.append((sp, 3))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
N: int = int(input())
|
||||
for _ in range(N):
|
||||
w, h = map(int, input().split())
|
||||
result = solve(w, h, [input().strip() for _ in range(h)])
|
||||
|
||||
if result is None:
|
||||
print("IMPOSSIBLE")
|
||||
else:
|
||||
print(result)
|
||||
Reference in New Issue
Block a user