Merge remote-tracking branch 'origin/master'
This commit is contained in:
@@ -44,17 +44,14 @@ void compress(int N, Honeycomb *arr) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
qsort(arr, N, sizeof(Honeycomb), compare_loc);
|
qsort(arr, N, sizeof(Honeycomb), compare_loc);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// C11(Clang)에서 런타임에러: 백준 clang에서 qsort가 정렬을 제대로 하지 못하는 문제
|
||||||
int main() {
|
int main() {
|
||||||
int N;
|
int N;
|
||||||
scanf("%d", &N);
|
scanf("%d", &N);
|
||||||
Honeycomb *arr;;
|
Honeycomb *arr;;
|
||||||
if ((arr = (Honeycomb *) calloc(N, sizeof(Honeycomb))) == NULL) {
|
arr = (Honeycomb *) calloc(N, sizeof(Honeycomb));
|
||||||
printf("NULL");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
for (int i = 0; i < N; i++) {
|
for (int i = 0; i < N; i++) {
|
||||||
scanf("%d", &arr[i].x);
|
scanf("%d", &arr[i].x);
|
||||||
arr[i].loc = i;
|
arr[i].loc = i;
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
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를 전개하면 됨.
|
|
||||||
"""
|
|
||||||
62
zeta_python/completed/28707.py
Normal file
62
zeta_python/completed/28707.py
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
import sys
|
||||||
|
import heapq
|
||||||
|
|
||||||
|
input = sys.stdin.readline
|
||||||
|
INF = 10 ** 9 + 1
|
||||||
|
|
||||||
|
sorted_one = None
|
||||||
|
|
||||||
|
|
||||||
|
def is_sorted(arr):
|
||||||
|
return arr == sorted(arr)
|
||||||
|
|
||||||
|
|
||||||
|
def swap(a, l, r):
|
||||||
|
arr = a.copy()
|
||||||
|
arr[l - 1], arr[r - 1] = arr[r - 1], arr[l - 1]
|
||||||
|
return arr
|
||||||
|
|
||||||
|
|
||||||
|
def dijkstra(operators, start: list) -> dict:
|
||||||
|
heap = []
|
||||||
|
distance = {tuple(start): 0}
|
||||||
|
heapq.heappush(heap, (0, start,))
|
||||||
|
while heap:
|
||||||
|
cost, now, = heapq.heappop(heap)
|
||||||
|
if is_sorted(now):
|
||||||
|
return distance
|
||||||
|
|
||||||
|
if distance[tuple(now)] < cost:
|
||||||
|
continue
|
||||||
|
|
||||||
|
for l, r, weight in operators:
|
||||||
|
new = swap(now, l, r)
|
||||||
|
new_cost = cost + weight
|
||||||
|
if tuple(new) not in distance:
|
||||||
|
distance[tuple(new)] = INF
|
||||||
|
if new_cost < distance[tuple(new)]:
|
||||||
|
distance[tuple(new)] = new_cost
|
||||||
|
heapq.heappush(heap, (new_cost, new))
|
||||||
|
distance[tuple(sorted_one)] = -1
|
||||||
|
return distance
|
||||||
|
|
||||||
|
|
||||||
|
def solve(N, A, Ops):
|
||||||
|
distance = dijkstra(Ops, A)
|
||||||
|
return distance[tuple(sorted_one)]
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
N = int(input())
|
||||||
|
A = list(map(int, input().split()))
|
||||||
|
sorted_one = sorted(A)
|
||||||
|
M = int(input())
|
||||||
|
Ops: list = []
|
||||||
|
for _ in range(M):
|
||||||
|
l, r, c = map(int, input().split())
|
||||||
|
Ops.append((l, r, c))
|
||||||
|
print(solve(N, A, Ops))
|
||||||
|
"""
|
||||||
|
문자가 배열된 상태 S_k를 정의할 수 있음.
|
||||||
|
이에 따라 S_0에서 S_k로 가는 distance를 구할 수 있고, 이로 dijkstra를 전개하면 됨.
|
||||||
|
"""
|
||||||
Reference in New Issue
Block a user