From e958fbb4ad3d2fa772dbcf59501ca4de322dffcc Mon Sep 17 00:00:00 2001 From: yenru0 Date: Wed, 3 Apr 2024 16:56:59 +0900 Subject: [PATCH] complete 5427 and aloha dijkstra1(1753) --- zeta_python/5427.py | 0 zeta_python/completed/1753.py | 46 +++++++++++++++++++ zeta_python/completed/5427.py | 83 +++++++++++++++++++++++++++++++++++ 3 files changed, 129 insertions(+) delete mode 100644 zeta_python/5427.py create mode 100644 zeta_python/completed/1753.py create mode 100644 zeta_python/completed/5427.py diff --git a/zeta_python/5427.py b/zeta_python/5427.py deleted file mode 100644 index e69de29..0000000 diff --git a/zeta_python/completed/1753.py b/zeta_python/completed/1753.py new file mode 100644 index 0000000..e225b4c --- /dev/null +++ b/zeta_python/completed/1753.py @@ -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:]] diff --git a/zeta_python/completed/5427.py b/zeta_python/completed/5427.py new file mode 100644 index 0000000..c96c121 --- /dev/null +++ b/zeta_python/completed/5427.py @@ -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)