Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

80-tgyuuAn #254

Merged
merged 1 commit into from
Nov 16, 2024
Merged

80-tgyuuAn #254

merged 1 commit into from
Nov 16, 2024

Conversation

tgyuuAn
Copy link
Member

@tgyuuAn tgyuuAn commented Nov 11, 2024

πŸ”— 문제 링크

μ΅œμ†ŒλΉ„μš© κ΅¬ν•˜κΈ° 2

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

30λΆ„

✨ μˆ˜λ„ μ½”λ“œ

image

μ•Œκ³ λ¦¬μ¦˜,,, 거의 ν•œλ‹¬λ§Œμ— ν‘ΈλŠ” 것 κ°™λ„€μš© (κ²½μ§„λŒ€νšŒ μ œμ™Έν•˜κ΅¬!)

λ‘±νƒ€μž„λ…Έμ”¨μž…λ‹ˆλ‹€!

μ†Œμ†Œν•˜κ²Œ κ³¨λ“œ 2~3 μ€‘μ—μ„œ λͺ¨μƒ‰ν•˜λ‹€κ°€ 문제 이름 보고 왠지 DP, 그리디 일 것 κ°™μ•„μ„œ κ³¨λžλŠ”λ° λ‹€λ₯Έ μ•Œκ³ λ¦¬μ¦˜μ΄μ—ˆλ„€μš” ,,




λ¬Έμ œλŠ” 되게 κ°„λ‹¨ν•©λ‹ˆλ‹€.

image

μœ„ 지문을 읽자마자, Aμ—μ„œ Bλ„μ‹œλ‘œ κ°€λŠ” λ²„μŠ€ μ΅œμ†Œ λΉ„μš©μ„ ꡬ해라 λΌλŠ” 말을 보자마자 λ‹€μ΅μŠ€νŠΈλΌ λΌλŠ” 것을 μ§κ°ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

심지어 λ…Έλ“œμ˜ κ°œμˆ˜λ„ 10만이고 κ°„μ„ μ˜ κ°œμˆ˜κ°€ 1000 이기 λ•Œλ¬Έμ—, $10만 * log(1000) = 100만$ μ΄λ―€λ‘œ μ‹œκ°„λ³΅μž‘λ„λ„ 세이프이죠!

λ§žμ•„μš” 문제 κ·Έλƒ₯ λ‹€μ΅μŠ€νŠΈλΌ ν’€λ©΄ ν’€λ¦½λ‹ˆλ‹€.

ν•˜μ§€λ§Œ λ¬Έμ œλŠ” μ—¬κΈ°μ„œ ν•œ 가지 νŠΈλ¦­μ„ 더 κ±Έμ—ˆμ–΄μš”.




image

좜λ ₯μ—μ„œ μ΅œμ†Œ λΉ„μš©μ„ κ°€μ§€λŠ” κ²½λ‘œμ—μ„œ λ„μ‹œμ˜ 개수 및 λ„μ‹œμ˜ 경둜λ₯Ό 좜λ ₯ν•΄μ•Όν•΄μš”.




이걸 처음 봀으면 λ‚œν•΄ν–ˆμ„ μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€,,

각 λ…Έλ“œλ§ˆλ‹€ νžˆμŠ€ν† λ¦¬λ₯Ό λͺ¨λ‘ μ €μž₯ν•΄μ•Όν•˜λ‚˜...? ν•˜κ³  λ§μ΄μ—μš”.

ν•˜μ§€λ§Œ μ €λŠ” #85 λ₯Ό λ°”λ‘œ λ– μ˜¬λ¦΄ 수 μžˆμ—ˆκ³ ,

ν•΄λ‹Ή λ…Έλ“œλ‘œ 갈 λ•Œ 이전 λ…Έλ“œλ§Œ 기둝해쀀 λ’€, λ§ˆμ§€λ§‰μ— λ„μ°©μ§€μ μœΌλ‘œλΆ€ν„° μ‹œμž‘ μ§€μ κΉŒμ§€ 타고타고 μ˜¬λΌκ°€λ©΄ μ™„μ„±μž…λ‹ˆλ‹€!




이 문제,, 처음 μ ‘ν•˜λ©΄ 쑰금 λ‹Ήν™©μŠ€λŸ¬μšΈ μˆ˜λ„ μžˆμ„ 것 κ°™μ•„μš”.

ν™μ£Όλ‹˜ λͺ‡λΆ„ κ±Έλ ΈλŠ”μ§€ λ§ν•΄μ£Όμ„Έμš©~!!!!

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

@tgyuuAn tgyuuAn added tgyuuAn ν•œ μ€„λ‘œλŠ” μ†Œκ°œν•  수 μ—†λŠ” λ‚¨μž. μž‘μ„± 쀑 ⏱️ labels Nov 11, 2024
@tgyuuAn tgyuuAn requested a review from H0ngJu November 11, 2024 12:42
@tgyuuAn tgyuuAn self-assigned this Nov 11, 2024
@tgyuuAn tgyuuAn marked this pull request as ready for review November 11, 2024 13:04
Copy link
Collaborator

@H0ngJu H0ngJu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. list 좜λ ₯ 포맷적용 μ•ˆν•΄μ„œ
  2. λ‹€μ΅μŠ€νŠΈλΌμ—μ„œ μ΄ˆκΈ°ν™”κ°€ 잘λͺ»λΌμ„œ

μ˜€λŠ˜λ„ 질게 ν…ŒμΌ€λ₯Ό 톡해 ν’€μ—ˆμŠ΅λ‹ˆλ‹€ ν•˜ν•«
1μ‹œκ°„ 10λΆ„ 쫌 λ„˜κ²Œ ν’€μ—ˆμŠ΅λ‹ˆλ‘₯ ...!>!!>!>!>!>!>
PRμ—μ„œ λ§μ”€ν•˜μ‹ λŒ€λ‘œ 경둜좜λ ₯ν•˜λŠ”κ²Œ λ‚―μ„€κΈ΄ ν–ˆλŠ”λ° μ—¬μ°¨μ €μ°¨ν•΄μ„œ ν•΄κ²°ν–ˆμŠ΅λ‹ˆλ‹€!!

#85 λ³΄λ‹ˆκΉŒ μ΄μ „κ²½λ‘œλ§Œμ„ μ €μž₯ν•΄μ„œ 역좔적.. ν•˜λŠ” 방법이 μžˆμ—ˆκ΅°μš” ....
μ €λŠ” λƒ…λ‹€ 배열에 λ‹€ μ €μž₯ν•˜κ³ , μƒˆ λ…Έλ“œ 경둜λ₯Ό μΆ”κ°€ν•˜μ—¬ κ°±μ‹ ν•˜λŠ” μ‹μœΌλ‘œ μ§„ν–‰ν–ˆμŠ΅λ‹ˆλ‹€
νž™μ„ μ‚¬μš©ν–ˆμœΌλ©΄ 경둜 좔적도 κ·Έλ ‡κ³  λ‹€μ΅μŠ€νŠΈλΌλ„ 쒀더 κ°„λ‹¨ν•˜κ²Œ ν’€ 수 μžˆμ—ˆκ² λ„€μš© ..

# 9μ‹œ 20λΆ„ μ‹œμž‘

import sys

def input() : return sys.stdin.readline().rstrip()

n = int(input()) # 1000
m = int(input()) # 100000
info = [ [] for _ in range(n+1)]
# λ„μ‹œλ²ˆν˜Έ(μΆœλ°œμ§€) λ„μ‹œλ²ˆν˜Έ(도착지) λ²„μŠ€λΉ„μš©
for i in range(m):
    s, e, c = map(int, input().split())
    info[s].append((e,c))
start_city, end_city = map(int, input().split())
cost = [1e9 for _ in range(n+1)] 
path = [[] for _ in range(n+1)]
visited = [False for _ in range(n+1)]

def get_min_cost_node():
    min_cost = 1e9
    idx = 0
    for i in range(1,n+1):
        if cost[i] < min_cost and not visited[i]:
            min_cost = cost[i]
            idx = i
    return idx

# λ‹€μ΅μŠ€νŠΈλΌμž„
def Dijikstra(start):
    cost[start] = 0
    visited[start] = True
    for e, c in info[start]:
        cost[e] = min(cost[e], c) #μ—¬λŸ¬ κ²½λ‘œκ°€ μžˆμ„ 경우 μ΅œμ†Ÿκ°’μœΌλ‘œ μ΄ˆκΈ°ν™”
    for i in range(n-1):
        node = get_min_cost_node()
        visited[node] = True
        for e, c in info[node]:
            if cost[e] > cost[node] + c:
                cost[e] = cost[node] + c  
                path[e] = path[node] + [node]

if start_city == end_city:
    print(0)
    print(1)
    print(start_city)
else: 
    Dijikstra(start_city)
    print(cost[end_city])
    path[end_city] = [start_city] + path[end_city] + [end_city]
    print(len(path[end_city]))  
    print(" ".join(map(str, path[end_city])))

@tgyuuAn
Copy link
Member Author

tgyuuAn commented Nov 16, 2024

  1. list 좜λ ₯ 포맷적용 μ•ˆν•΄μ„œ
  2. λ‹€μ΅μŠ€νŠΈλΌμ—μ„œ μ΄ˆκΈ°ν™”κ°€ 잘λͺ»λΌμ„œ

μ˜€λŠ˜λ„ 질게 ν…ŒμΌ€λ₯Ό 톡해 ν’€μ—ˆμŠ΅λ‹ˆλ‹€ ν•˜ν•« 1μ‹œκ°„ 10λΆ„ 쫌 λ„˜κ²Œ ν’€μ—ˆμŠ΅λ‹ˆλ‘₯ ...!>!!>!>!>!>!> PRμ—μ„œ λ§μ”€ν•˜μ‹ λŒ€λ‘œ 경둜좜λ ₯ν•˜λŠ”κ²Œ λ‚―μ„€κΈ΄ ν–ˆλŠ”λ° μ—¬μ°¨μ €μ°¨ν•΄μ„œ ν•΄κ²°ν–ˆμŠ΅λ‹ˆλ‹€!!

#85 λ³΄λ‹ˆκΉŒ μ΄μ „κ²½λ‘œλ§Œμ„ μ €μž₯ν•΄μ„œ 역좔적.. ν•˜λŠ” 방법이 μžˆμ—ˆκ΅°μš” .... μ €λŠ” λƒ…λ‹€ 배열에 λ‹€ μ €μž₯ν•˜κ³ , μƒˆ λ…Έλ“œ 경둜λ₯Ό μΆ”κ°€ν•˜μ—¬ κ°±μ‹ ν•˜λŠ” μ‹μœΌλ‘œ μ§„ν–‰ν–ˆμŠ΅λ‹ˆλ‹€ νž™μ„ μ‚¬μš©ν–ˆμœΌλ©΄ 경둜 좔적도 κ·Έλ ‡κ³  λ‹€μ΅μŠ€νŠΈλΌλ„ 쒀더 κ°„λ‹¨ν•˜κ²Œ ν’€ 수 μžˆμ—ˆκ² λ„€μš© ..

# 9μ‹œ 20λΆ„ μ‹œμž‘

import sys

def input() : return sys.stdin.readline().rstrip()

n = int(input()) # 1000
m = int(input()) # 100000
info = [ [] for _ in range(n+1)]
# λ„μ‹œλ²ˆν˜Έ(μΆœλ°œμ§€) λ„μ‹œλ²ˆν˜Έ(도착지) λ²„μŠ€λΉ„μš©
for i in range(m):
    s, e, c = map(int, input().split())
    info[s].append((e,c))
start_city, end_city = map(int, input().split())
cost = [1e9 for _ in range(n+1)] 
path = [[] for _ in range(n+1)]
visited = [False for _ in range(n+1)]

def get_min_cost_node():
    min_cost = 1e9
    idx = 0
    for i in range(1,n+1):
        if cost[i] < min_cost and not visited[i]:
            min_cost = cost[i]
            idx = i
    return idx

# λ‹€μ΅μŠ€νŠΈλΌμž„
def Dijikstra(start):
    cost[start] = 0
    visited[start] = True
    for e, c in info[start]:
        cost[e] = min(cost[e], c) #μ—¬λŸ¬ κ²½λ‘œκ°€ μžˆμ„ 경우 μ΅œμ†Ÿκ°’μœΌλ‘œ μ΄ˆκΈ°ν™”
    for i in range(n-1):
        node = get_min_cost_node()
        visited[node] = True
        for e, c in info[node]:
            if cost[e] > cost[node] + c:
                cost[e] = cost[node] + c  
                path[e] = path[node] + [node]

if start_city == end_city:
    print(0)
    print(1)
    print(start_city)
else: 
    Dijikstra(start_city)
    print(cost[end_city])
    path[end_city] = [start_city] + path[end_city] + [end_city]
    print(len(path[end_city]))  
    print(" ".join(map(str, path[end_city])))

λ‹€μ΅μŠ€νŠΈλΌμž„ 무엇 γ…‹γ…‹γ…‹γ…‹γ…‹γ…‹γ…‹γ…‹γ…‹

@tgyuuAn tgyuuAn merged commit ee3bbc0 into main Nov 16, 2024
9 checks passed
@tgyuuAn tgyuuAn deleted the 80-tgyuuAn branch November 16, 2024 09:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tgyuuAn ν•œ μ€„λ‘œλŠ” μ†Œκ°œν•  수 μ—†λŠ” λ‚¨μž. 리뷰 μ™„λ£Œ βœ”οΈ
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants