-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from AlgoLeadMe/15-wkdghdwns199
15-wkdghdwns199
- Loading branch information
Showing
6 changed files
with
174 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
''' | ||
λ¬Έμ | ||
Nκ°μ λμκ° μλ€. κ·Έλ¦¬κ³ ν λμμμ μΆλ°νμ¬ λ€λ₯Έ λμμ λμ°©νλ λ²μ€κ° Mκ° μλ€. | ||
κ° λ²μ€λ A, B, Cλ‘ λνλΌ μ μλλ°, Aλ μμλμ, Bλ λμ°©λμ, Cλ λ²μ€λ₯Ό νκ³ μ΄λνλλ° κ±Έλ¦¬λ μκ°μ΄λ€. | ||
μκ° Cκ° μμκ° μλ κ²½μ°κ° μλ€. C = 0μΈ κ²½μ°λ μκ° μ΄λμ νλ κ²½μ°, C < 0μΈ κ²½μ°λ νμλ¨Έμ μΌλ‘ μκ°μ λλμκ°λ κ²½μ°μ΄λ€. | ||
1λ² λμμμ μΆλ°ν΄μ λλ¨Έμ§ λμλ‘ κ°λ κ°μ₯ λΉ λ₯Έ μκ°μ ꡬνλ νλ‘κ·Έλ¨μ μμ±νμμ€. | ||
μ λ ₯ | ||
첫째 μ€μ λμμ κ°μ N (1 β€ N β€ 500), λ²μ€ λ Έμ μ κ°μ M (1 β€ M β€ 6,000)μ΄ μ£Όμ΄μ§λ€. | ||
λμ§Έ μ€λΆν° Mκ°μ μ€μλ λ²μ€ λ Έμ μ μ 보 A, B, C (1 β€ A, B β€ N, -10,000 β€ C β€ 10,000)κ° μ£Όμ΄μ§λ€. | ||
μΆλ ₯ | ||
λ§μ½ 1λ² λμμμ μΆλ°ν΄ μ΄λ€ λμλ‘ κ°λ κ³Όμ μμ μκ°μ 무νν μ€λ μ μΌλ‘ λλ릴 μ μλ€λ©΄ 첫째 μ€μ -1μ μΆλ ₯νλ€. | ||
κ·Έλ μ§ μλ€λ©΄ N-1κ° μ€μ κ±Έμ³ κ° μ€μ 1λ² λμμμ μΆλ°ν΄ 2λ² λμ, 3λ² λμ, ..., Nλ² λμλ‘ κ°λ κ°μ₯ λΉ λ₯Έ μκ°μ μμλλ‘ μΆλ ₯νλ€. | ||
λ§μ½ ν΄λΉ λμλ‘ κ°λ κ²½λ‘κ° μλ€λ©΄ λμ -1μ μΆλ ₯νλ€. | ||
μμ μ λ ₯ 1 | ||
3 4 | ||
1 2 4 | ||
1 3 3 | ||
2 3 -1 | ||
3 1 -2 | ||
μμ μΆλ ₯ 1 | ||
4 | ||
3 | ||
μμ μ λ ₯ 2 | ||
3 4 | ||
1 2 4 | ||
1 3 3 | ||
2 3 -4 | ||
3 1 -2 | ||
μμ μΆλ ₯ 2 | ||
-1 | ||
μμ μ λ ₯ 3 | ||
3 2 | ||
1 2 4 | ||
1 2 3 | ||
μμ μΆλ ₯ 3 | ||
3 | ||
-1 | ||
''' | ||
|
||
import sys | ||
input = sys.stdin.readline | ||
INF = int(1e9) | ||
|
||
def bellman_ford(start): | ||
dist[start] = 0 | ||
|
||
for i in range(1,n+1): | ||
for j in range(m): | ||
now, next, cost = edges[j][0], edges[j][1], edges[j][2] | ||
if dist[now] != INF and dist[next] > dist[now] + cost: | ||
dist[next] = dist[now] + cost | ||
if i == n : | ||
return True | ||
return False | ||
|
||
n,m = map(int, input().split()) | ||
edges = [] | ||
dist = [INF] * (n+1) | ||
|
||
for _ in range(m): | ||
a,b,c = map(int, input().split()) | ||
edges.append((a,b,c)) | ||
|
||
negative_cycle = bellman_ford(1) | ||
if negative_cycle : | ||
print(-1) | ||
else : | ||
for i in range(2,n+1): | ||
if dist[i] == INF: | ||
print(-1) | ||
else : | ||
print(dist[i]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import sys | ||
input = sys.stdin.readline | ||
N,M = map(int, input().split()) | ||
num_list = list(map(int, input().split())) | ||
accumulate_list = [0] * (N+1) | ||
for i in range(N): | ||
accumulate_list[i+1] = accumulate_list[i] + num_list[i] | ||
|
||
for _ in range(M): | ||
start, end = map(int, input().split()) | ||
print(accumulate_list[end] - accumulate_list[start-1]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import sys | ||
from heapq import * | ||
input = sys.stdin.readline | ||
|
||
def dijkstra(graph, start): | ||
length = [float('inf') for _ in graph] | ||
length[start] = 0 | ||
queue = [] | ||
heappush(queue, [length[start], start]) | ||
|
||
while queue: | ||
current_length, current_target = heappop(queue) | ||
|
||
if length[current_target] < current_length : | ||
continue | ||
|
||
for new_target, new_length in graph[current_target] : | ||
len = current_length + new_length | ||
if len < length[new_target]: | ||
length[new_target] = len | ||
heappush(queue, [len, new_target]) | ||
return length | ||
|
||
items = [] | ||
N,M,R = map(int, input().split()) | ||
T = list(map(int, input().split())) | ||
graph = [[] for _ in range(N+1)] | ||
|
||
for _ in range(R): | ||
start, end, weight = map(int, input().split()) | ||
graph[start].append((end, weight)) | ||
graph[end].append((start, weight)) | ||
|
||
for item in range(1, len(graph)) : | ||
result = dijkstra(graph, item) | ||
get = 0 | ||
for i in range(len(result)): | ||
if result[i] <= M : | ||
get += T[i-1] | ||
items.append(get) | ||
|
||
print(max(items)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import sys | ||
input = sys.stdin.readline | ||
N = int(input()) | ||
lines = [list(map(int, input().rstrip().split())) for _ in range(N)] | ||
lines.sort() | ||
|
||
length =0 | ||
current_start = lines[0][0] | ||
current_end = lines[0][1] | ||
for start, end in lines[1:]: | ||
if start > current_end : | ||
print(start, current_end) | ||
length += (current_end - current_start) | ||
current_start = start | ||
current_end = max(current_end, end) | ||
length += (current_end - current_start) | ||
print(length) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import sys, heapq | ||
input =sys.stdin.readline | ||
min_heap = [] | ||
max_heap = [] | ||
N = int(input()) | ||
for _ in range(N): | ||
number = int(input()) | ||
if (number > 0) : | ||
heapq.heappush(min_heap, number) | ||
elif (number < 0) : | ||
heapq.heappush(max_heap, -number) | ||
else : | ||
if min_heap : | ||
if max_heap : | ||
if min_heap[0] < max_heap[0] : | ||
print(heapq.heappop(min_heap)) | ||
else : | ||
print(-heapq.heappop(max_heap)) | ||
else : | ||
print(heapq.heappop(min_heap)) | ||
elif max_heap : | ||
print(-heapq.heappop(max_heap)) | ||
else : | ||
print(0) |