From b67478dfa406a58695d53979f25426385ffd7939 Mon Sep 17 00:00:00 2001 From: yenru0 Date: Mon, 8 Apr 2024 01:02:21 +0900 Subject: [PATCH] incomplete 18870.c(unknown boj c11-clang runtime error), 28707.py & complete aloha dijkstra3(1238.py, 1504.py, 17940.py) --- zeta_C/18870.c | 72 ++++++++++++++++++++++++++++++++++ zeta_C/run.sh | 2 +- zeta_python/28707.py | 24 ++++++++++++ zeta_python/completed/1238.py | 40 +++++++++++++++++++ zeta_python/completed/1504.py | 47 ++++++++++++++++++++++ zeta_python/completed/17940.py | 44 +++++++++++++++++++++ 6 files changed, 228 insertions(+), 1 deletion(-) create mode 100644 zeta_C/18870.c create mode 100644 zeta_python/28707.py create mode 100644 zeta_python/completed/1238.py create mode 100644 zeta_python/completed/1504.py create mode 100644 zeta_python/completed/17940.py diff --git a/zeta_C/18870.c b/zeta_C/18870.c new file mode 100644 index 0000000..169a7e7 --- /dev/null +++ b/zeta_C/18870.c @@ -0,0 +1,72 @@ +#include +#include + +typedef struct Honeycomb { + int x; + int loc; + int compressed; +} Honeycomb; + +int compare_x(const void *a, const void *b) { + Honeycomb A = *(Honeycomb *) a; + Honeycomb B = *(Honeycomb *) b; + if (A.x > B.x) { + return 1; + } else if (A.x < B.x) { + return -1; + } else { + return 0; + } +} + +int compare_loc(const void *a, const void *b) { + Honeycomb A = *(Honeycomb *) a; + Honeycomb B = *(Honeycomb *) b; + if (A.loc > B.loc) { + return 1; + } else if (A.loc < B.loc) { + return -1; + } else { + return 0; + } +} + +// 좌표 압축 알고리즘 +void compress(int N, Honeycomb *arr) { + qsort(arr, N, sizeof(Honeycomb), compare_x); + + for (int i = 1; i < N; i++) { + if (arr[i].x == arr[i - 1].x) { + arr[i].compressed = arr[i - 1].compressed; + } else { + arr[i].compressed = arr[i - 1].compressed + 1; + } + } + + qsort(arr, N, sizeof(Honeycomb), compare_loc); + return; +} + +int main() { + int N; + scanf("%d", &N); + Honeycomb *arr;; + if ((arr = (Honeycomb *) calloc(N, sizeof(Honeycomb))) == NULL) { + printf("NULL"); + return 1; + } + for (int i = 0; i < N; i++) { + scanf("%d", &arr[i].x); + arr[i].loc = i; + arr[i].compressed = 0; + } + + compress(N, arr); + + for (int i = 0; i < N - 1; i++) { + printf("%d ", arr[i].compressed); + } + printf("%d\n", arr[N - 1].compressed); + free(arr); + return 0; +} \ No newline at end of file diff --git a/zeta_C/run.sh b/zeta_C/run.sh index ef3a298..b0e2566 100644 --- a/zeta_C/run.sh +++ b/zeta_C/run.sh @@ -1,4 +1,4 @@ #!/bin/bash if [ -z "$*" ]; then echo "No args"; exit 0; fi -clang-18 -std=c17 $1 +clang-18 -std=c11 $1 ./a.out < stdin.txt \ No newline at end of file diff --git a/zeta_python/28707.py b/zeta_python/28707.py new file mode 100644 index 0000000..2bba7ca --- /dev/null +++ b/zeta_python/28707.py @@ -0,0 +1,24 @@ +import heapq + +INF = 10 ** 9 + 1 + + +def solve(N, A, Ops): + pass + + +if __name__ == '__main__': + N = int(input()) + A = list(map(int, input().split())) + M = int(input()) + Ops: list[list[tuple]] = [[] for _ in range(N)] + for _ in range(M): + l, r, c = map(int, input().split()) + Ops[l].append((r, c)) + Ops[r].append((l, c)) + + +""" +문자가 배열된 상태 S_k를 정의할 수 있음. +이에 따라 S_0에서 S_k로 가는 distance를 구할 수 있고, 이로 dijkstra를 전개하면 됨. +""" \ No newline at end of file diff --git a/zeta_python/completed/1238.py b/zeta_python/completed/1238.py new file mode 100644 index 0000000..99a159c --- /dev/null +++ b/zeta_python/completed/1238.py @@ -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)) diff --git a/zeta_python/completed/1504.py b/zeta_python/completed/1504.py new file mode 100644 index 0000000..971a677 --- /dev/null +++ b/zeta_python/completed/1504.py @@ -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)) diff --git a/zeta_python/completed/17940.py b/zeta_python/completed/17940.py new file mode 100644 index 0000000..fef001d --- /dev/null +++ b/zeta_python/completed/17940.py @@ -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))