[๋ฐฑ์ค€ ๐Ÿฅ‡3] 13168๋ฒˆ ๋‚ด์ผ๋กœ ์—ฌํ–‰ (Python/ํŒŒ์ด์ฌ)

mingssoยท2023๋…„ 3์›” 20์ผ
0

Algorithm

๋ชฉ๋ก ๋ณด๊ธฐ
29/35

1๏ธโƒฃ ๋ฌธ์ œ

https://www.acmicpc.net/problem/13168



2๏ธโƒฃ ์ฝ”๋“œ

ํ”Œ๋กœ์ด๋“œ ๋ฌธ์ œ๋Š” ํ‹ฐ์–ด์— ๋น„ํ•ด ์‰ฌ์šด ๋Š๋‚Œใ…Žใ…Ž
์ด ๋ฌธ์ œ๋„ ์ฒจ์— ์ž…๋ ฅ ๋ณด๊ณ  ๊ฒฝ์•…ํ–ˆ์ง€๋งŒ ํ’€๊ณ ๋ณด๋‹ˆ ์‰ฌ์šด ๋ฌธ์ œ์˜€๋‹ค

import sys

INF = sys.maxsize

n, r = map(int, sys.stdin.readline().split())
city = {}
temp = list(sys.stdin.readline().split())
for i in range(n):
    city[temp[i]] = i   # ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ด์šฉํ•ด 'city[๋„์‹œ์ด๋ฆ„] = ์ธ๋ฑ์Šค'๋กœ ์ •๋ฆฌํ•จ
    
m = int(sys.stdin.readline())
temp = list(sys.stdin.readline().split())
travel = []
for i in range(m):
    travel.append(city[temp[i]])   # ์—ฌํ–‰ํ•  ๋„์‹œ ์ธ๋ฑ์Šค ๋ฆฌ์ŠคํŠธ๋กœ ์ €์žฅํ•จ

k = int(input())
dist = [[INF] * n for _ in range(n)]    # ๋‚ด์ผ๋กœ๋ฅผ ์‚ฌ์ง€ ์•Š์•˜์„ ๋•Œ ์ตœ์†Œ ๋น„์šฉ
tomo = [[INF] * n for _ in range(n)]    # ๋‚ด์ผ๋กœ๋ฅผ ์ƒ€์„ ๋•Œ ์ตœ์†Œ ๋น„์šฉ

for _ in range(k):
    t, ss, es, c = input().split()

    s, e = city[ss], city[es]
    # ๋‚ด์ผ๋กœ๋ฅผ ์‚ฌ์ง€ ์•Š์•˜์„ ๋•Œ ๋น„์šฉ ์ €์žฅ 
    if dist[s][e] > int(c):
        dist[s][e] = int(c)
        dist[e][s] = int(c)

    # ๋‚ด์ผ๋กœ๋ฅผ ์ƒ€์„ ๋•Œ ๋น„์šฉ ์ €์žฅ
    if t == 'Mugunghwa' or t == 'ITX-Saemaeul' or t == 'ITX-Cheongchun':
        tomo[s][e] = 0
        tomo[e][s] = 0
    elif t == 'S-Train' or t == 'V-Train':
        if tomo[s][e] > int(c) / 2:
            tomo[s][e] = int(c) / 2
            tomo[e][s] = int(c) / 2
    else:
        if tomo[s][e] > int(c):
            tomo[s][e] = int(c)
            tomo[e][s] = int(c)

# ํ”Œ๋กœ์ด๋“œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ์ตœ์†Œ ๋น„์šฉ ๊ตฌํ•˜๊ธฐ 
for l in range(n):
    for i in range(n):
        for j in range(n):
            if dist[i][j] > dist[i][l] + dist[l][j]:
                dist[i][j] = dist[i][l] + dist[l][j]
                dist[j][i] = dist[j][l] + dist[l][i]

            if tomo[i][j] > tomo[i][l] + tomo[l][j]:
                tomo[i][j] = tomo[i][l] + tomo[l][j]
                tomo[j][i] = tomo[j][l] + tomo[l][i]

# m๊ฐœ์˜ ๋„์‹œ๋ฅผ ์ฐจ๋ก€๋Œ€๋กœ ์ด๋™ํ•˜๋Š”๋ฐ ๋“œ๋Š” ๋น„์šฉ ๊ณ„์‚ฐ
yes, no = r, 0
for i in range(m-1):
    s, e = travel[i], travel[i+1]
    yes += tomo[s][e]
    no += dist[s][e]

if yes >= no:
    print('No')
else:
    print('Yes')



3๏ธโƒฃ ํ’€์ด

ํ—ท๊ฐˆ๋ ธ๋˜ ๋ถ€๋ถ„์€ A ๋„์‹œ์—์„œ B ๋„์‹œ๋กœ ๊ฐˆ ๋•Œ ๋“œ๋Š” ๋น„์šฉ๊ณผ B ๋„์‹œ์—์„œ A ๋„์‹œ๋กœ ์˜ฌ ๋•Œ ๋“œ๋Š” ๋น„์šฉ์ด ๋™์ผํ•˜๋‹ค๋Š” ๊ฒƒ
๊ทธ๋ฆฌ๊ณ  50% ๋น„์šฉ์„ ๊ณ„์‚ฐํ•  ๋•Œ ์ •์ˆ˜ ๋‚˜๋ˆ„๊ธฐ๋กœ ํ•ด์ฃผ๋ฉด ํ‹€๋ฆฐ๋‹ค
100%์—์„œ ํ‹€๋ ค์„œ ์งˆ๋ฌธ ๊ฒŒ์‹œํŒ ๋ณด๊ณ  ์•Œ์•˜์Œ

profile
๐Ÿฅ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ’ฐ

0๊ฐœ์˜ ๋Œ“๊ธ€