0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ABC481のPython解答(A~D)

0
Posted at

SMBCプログラミングコンテスト #2(AtCoder Beginner Contest 481)の解答等の速報的まとめ

A問題

足して10以上か判定

A
a, b = map(int, input().split())

print(2 if a + b >= 10 else 1)

B問題

間に$K$があるか判定
$a_i$自体が$K$のときのことに気を付けて等号を付ける

B
n, k = map(int, input().split())
a = list(map(int, input().split()))

ans = 1 if a[0] == k else 0
for a_i, a_j in zip(a, a[1:]):
    if a_i < k <= a_j or a_i > k >= a_j:
        ans += 1

print(ans)

C問題

辞書に入れる

C
bank = dict()
for _ in range(int(input())):
    s = input()
    if s not in bank:
        bank[s] = len(bank) + 1

    print(bank[s])

D問題

ダイクストラ

D
from heapq import heappop, heappush

n, m, s, t = map(int, input().split())
s -= 1
t -= 1
edge = [list() for _ in range(n)]
for _ in range(m):
    u, v = map(int, input().split())
    edge[u - 1].append([v - 1, 1])

for e_i in edge:
    if e_i:
        e_i.sort()
        e_i[0][1] = 0

q = list()
q.append([0, s])
INF = 10 ** 18
cost = [INF] * n
cost[s] = 0
while q:
    cost_i, now = heappop(q)
    if cost[now] < cost_i:
        continue
    for to, c_i in edge[now]:
        if cost[to] > cost_i + c_i:
            cost[to] = cost_i + c_i
            heappush(q, [cost[to], to])

print(-1 if cost[t] == INF else cost[t])
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?