-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
140 additions
and
3 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import sys | ||
from collections import deque, defaultdict | ||
|
||
def input() : return sys.stdin.readline().rstrip() | ||
|
||
N, M, k = map(int, input().split()) | ||
friend_money = list(map(int, input().split())) | ||
relation = defaultdict(set) | ||
visited = [0] * N | ||
total = 0 | ||
|
||
for _ in range(M): | ||
a, b = map(int, input().split()) | ||
relation[a-1].add(b-1) | ||
relation[b-1].add(a-1) | ||
|
||
for i in range(N): | ||
if not visited[i]: | ||
visited[i] = True | ||
friends = [] | ||
q = deque([i]) | ||
|
||
while q: | ||
f = q.popleft() | ||
friends.append(f) | ||
for p in relation[f]: | ||
if not visited[p]: | ||
visited[p] = True | ||
q.append(p) | ||
|
||
if friends: | ||
cost = min(friend_money[friend] for friend in friends) | ||
total += cost | ||
|
||
if total <= k: | ||
print(total) | ||
else: | ||
print("Oh no") |
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,19 @@ | ||
import sys | ||
|
||
def input(): return sys.stdin.readline().rstrip() | ||
|
||
T = int(input()) | ||
|
||
for _ in range(T): | ||
N = int(input()) | ||
apply = [list(map(int, input().split())) for _ in range(N)] | ||
apply = sorted(apply) | ||
top = 0 | ||
cnt = 1 | ||
|
||
for i in range(1, N): | ||
if apply[i][1] < apply[top][1]: | ||
top = i | ||
cnt += 1 | ||
|
||
print(cnt) |
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,36 @@ | ||
from collections import deque | ||
import sys | ||
|
||
input = sys.stdin.readline | ||
|
||
T = int(input()) | ||
|
||
def command(register, cmd): | ||
if cmd == 0: | ||
return ((register * 2) % 10000, "D") | ||
elif cmd == 1: | ||
return (9999 if register == 0 else register - 1, "S") | ||
elif cmd == 2: | ||
return (register % 1000 * 10 + register // 1000, "L") | ||
elif cmd == 3: | ||
return (register // 10 + (register % 10) * 1000, "R") | ||
|
||
for _ in range(T): | ||
A, B = map(int, input().split()) | ||
|
||
visited = [False] * 10000 | ||
visited[A] = True | ||
|
||
queue = deque([(A, "")]) | ||
while queue: | ||
cur, result = queue.popleft() | ||
|
||
if cur == B: | ||
print(result) | ||
break | ||
|
||
for i in range(4): | ||
register, cmd = command(cur, i) | ||
if not visited[register]: | ||
visited[register] = True | ||
queue.append((register, result + cmd)) |
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
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,40 @@ | ||
from collections import defaultdict | ||
from heapq import * | ||
import sys | ||
|
||
def input(): return sys.stdin.readline().rstrip() | ||
MAX = 100_000 * 700_000 + 1 | ||
N, M = map(int, input().split()) | ||
|
||
graph = defaultdict(lambda : defaultdict(list)) | ||
|
||
for idx in range(1,M+1): | ||
start, destination = map(int, input().split()) | ||
graph[start][destination].append(idx) | ||
graph[destination][start].append(idx) | ||
|
||
costs = [MAX for _ in range(N+1)] | ||
visited = set() | ||
heap = [(0, 1)] | ||
while heap: | ||
now_cost, now_idx = heappop(heap) | ||
|
||
if now_idx in visited: continue | ||
visited.add(now_idx) | ||
|
||
if costs[now_idx] <= now_cost: continue | ||
costs[now_idx] = now_cost | ||
|
||
if now_idx == N: break | ||
|
||
for neighbor in graph[now_idx]: | ||
if neighbor in visited: continue | ||
|
||
for neighbor_idx in graph[now_idx][neighbor]: | ||
|
||
real_idx = now_cost % M | ||
if real_idx < neighbor_idx: | ||
heappush(heap, (neighbor_idx - real_idx + now_cost, neighbor)) | ||
else: | ||
heappush(heap, (now_cost + M - real_idx + neighbor_idx, neighbor)) | ||
print(costs[-1]) |