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

54-tgyuuAn #185

Merged
merged 1 commit into from
May 18, 2024
Merged

54-tgyuuAn #185

merged 1 commit into from
May 18, 2024

Conversation

tgyuuAn
Copy link
Member

@tgyuuAn tgyuuAn commented May 13, 2024

πŸ”— 문제 링크

미확인 도착지

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

2μ‹œκ°„

μ˜€λž˜κ°„λ§Œμ—.... 적수λ₯Ό λ§Œλ‚¬λ‹€...

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

일단 이 λ¬Έμ œλŠ” λ¬Έν•΄λ ₯을 많이 μš”ν•˜λŠ” 문제인 것 κ°™λ‹€.

문제 μ΄ν•΄ν•˜λŠ” 데만 20λΆ„ 정도 κ±Έλ Έλ‹€.




μ•„λž˜μ™€ 같은 κ·Έλž˜ν”„κ°€ μ£Όμ–΄μ‘Œμ„ λ•Œ,

νšŒμƒ‰ λ…Έλ“œκ°€ μ‹œμž‘ 지점이닀.

이 λ•Œ, 검은색 λ…Έλ“œκ°€ λͺ©μ μ§€μ˜ 후보가 될 수 μžˆλŠ” λ…Έλ“œλ“€μΈλ°,

νšŒμƒ‰μ—μ„œ κ²€μ€μƒ‰μœΌλ‘œ μ΅œμ†Œκ²½λ‘œλ‘œ λ»—μ—ˆμ„ λ•Œ μ μ„ μœΌλ‘œ 주어진 경둜 <g, h>λ₯Ό 지날 수 μžˆλŠ” 검은색 λ…Έλ“œλ“€μ„ λ°˜ν™˜ν•˜λ©΄ λœλ‹€.

image

κ·ΈλŸ¬λ‹ˆκΉŒ,,, 정리해보면 μœ„μ—μ„œ λͺ©μ μ§€ 후보가 될 수 μžˆλŠ” λ…Έλ“œλŠ” 5, 6번 λ…Έλ“œμΈλ°,

μ‹œμž‘ λ…Έλ“œμΈ 2번 λ…Έλ“œμ—μ„œ λ»—μœΌλ©΄ 5번 λ…Έλ“œλŠ” λ°”λ‘œ 5μ½”μŠ€νŠΈλ₯Ό μ‚¬μš©ν•΄μ„œ 2-> 5번 경둜λ₯Ό 타고 κ°€λŠ” 것이 κ°€μž₯ λΉ λ₯΄λ‹€.

image




반면, 6번 λ…Έλ“œλŠ” 2-> 3-> 6번 경둜둜 κ°€λŠ” 것이 6 μ½”μŠ€νŠΈλ₯Ό μ¨μ„œ κ°€μž₯ λΉ λ₯΄λ‹€.

image

이 λ•Œ, 6번 λ…Έλ“œλ‘œ κ°€λŠ” μ΅œμ†Œ κ²½λ‘œμ— 점선 λ…Έλ“œμΈ 1 -> 3 λ…Έλ“œκ°€ μžˆμœΌλ―€λ‘œ 6번 λ…Έλ“œλ₯Ό λ°˜ν™˜ν•˜λ©΄ λ˜λŠ” 것이닀.




그럼 이 문제λ₯Ό μ–΄λ–»κ²Œ ν’€λ©΄ λ˜λŠλƒ?!

일단 μ‹œμž‘ λ…Έλ“œκ°€ ν•˜λ‚˜κ°€ 주어진 μƒνƒœλ‘œ μ΅œμ†Œ μ½”μŠ€νŠΈλ‘œ 각 λ…Έλ“œλ§ˆλ‹€ 길을 λ»—μ–΄μ•Ό ν•˜λ―€λ‘œ λ‹€μ΅μŠ€νŠΈλΌλ₯Ό μ‚¬μš©ν•˜λŠ” 것이 λͺ…ν™•ν•˜λ‹€.

(λ‹€μ΅μŠ€νŠΈλΌμ— λŒ€ν•œ μ„€λͺ…은 λ””μŠ€μ½”λ“œ 채널 μ•Œκ³ λ¦¬μ¦˜ λ…ΈνŠΈμ— μžˆμ–΄μš”!)




그러면 μ–΄λ–»κ²Œ ν•˜λ©΄ ν•΄λ‹Ή λ…Έλ“œκΉŒμ§€μ˜ μ΅œμ†Œ κ²½λ‘œμ— <g, h> κ²½λ‘œκ°€ μžˆμŒμ„ νŒŒμ•…ν•  수 μžˆμ„κΉŒ?




쑰금만 생각해보면 은근 κ°„λ‹¨ν•œλ°,

μ•„λž˜μ™€ 같이 κΈ°μ‘΄ λ‹€μ΅μŠ€νŠΈλΌμ—λŠ” νž™ ν˜Ήμ€ μš°μ„ μˆœμœ„ 큐에 ν•΄λ‹Ή <λ…Έλ“œκΉŒμ§€μ˜ μ½”μŠ€νŠΈ, λ…Έλ“œ>만 μ €μž₯ν–ˆμ§€λ§Œ,

heap = [[0,start_node]]

while heap:
    now_distance, now_node = heappop(heap)
    visited[now_node] = True




is_via = False
heap = [[0,start_node, is_via]]

while heap:
    now_distance, now_node, now_is_via = heappop(heap)
    visited[now_node] = True

    for next_node in graph[now_distance]:
        // 기쑴 둜직

        if (now_node, next_node) == (g, h) or (now_node, next_node) == (h, g):
            new_via= True

        heappush(heap, [new_distance, next_node, new_via])

와 같이 쀑간에 κ²½λ‘œμ— λŒ€ν•œ 정보λ₯Ό 가지고 ν•΄λ‹Ή 경둜λ₯Ό μ§€λ‚¬λŠ” 지, μ§€λ‚˜μ§€ μ•Šμ•˜λŠ” μ§€λ§Œ μ²΄ν¬ν•˜λ©΄ λœλ‹€.










μ΄λ ‡κ²Œλ§Œ ν•˜λ©΄ 풀릴 쀄 μ•Œμ•˜λ‹€.

근데 볡병이 μžˆμ—ˆλ‹€.......

μ•„λž˜ 경우λ₯Ό μƒκ°ν•΄λ³΄μž.

1
6 6 2
1 2 3
1 2 1
2 3 2
3 4 1
4 5 1
1 6 4
6 5 1
4
5
ans : 4 5

image




λͺ©μ μ§€ 후보 λ…Έλ“œ 4번 λ…Έλ“œλ‘œ κ°€λŠ” 길은 μ•„λž˜μ™€ 같이 λͺ…ν™•ν•˜λ‹€.

image

4번 λ…Έλ“œλŠ” λ°˜λ“œμ‹œ λƒ„μƒˆκ°€ λ‚˜λŠ” 경둜λ₯Ό μ§€λ‚˜κ°ˆ 수 밖에 μ—†λ‹€.




ν•˜μ§€λ§Œ 5번 λ…Έλ“œλŠ”?

μ•„λž˜μ™€ 같이 1-> 6-> 5 경둜둜 κ°€λŠ” 것과,

1->2->3->4->5 의 경둜둜 κ°€λŠ” κ²ƒμ˜ μ½”μŠ€νŠΈκ°€ κ°™λ‹€.

image

사싀 이럴 κ²½μš°μ—λŠ” λƒ„μƒˆκ°€ λ‚˜λŠ” 경둜둜 κ°€λŠ” μ΅œμ†Œ κ²½λ‘œκ°€ 있기 λ•Œλ¬Έμ— 닡에 포함을 ν•˜λŠ” 것이 λ§žλ‹€.




그럼 μœ„ κ²½μš°μ—μ„œ 닡을 λ„μΆœν•΄λ‚΄λ €λ©΄ λ˜‘κ°™μ€ μ½”μŠ€νŠΈμ—μ„œ 갈 수 μžˆλŠ” κ²½λ‘œκ°€ μ—¬λŸ¬ 가지 일 λ•Œ,

λƒ„μƒˆκ°€ λ‚˜λŠ” 경둜λ₯Ό μ§€λ‚œ 것을 λ¨Όμ € μš°μ„ μˆœμœ„ 큐 ν˜Ήμ€ νž™μ—μ„œ λ½‘μ•„λ‚΄μ•Όλ§Œ ν•œλ‹€.

μ–΄λ–»κ²Œ μš°μ„ μˆœμœ„ νμ—μ„œ κ°€μ€‘μΉ˜λ₯Ό 잘 μ‘°μ •ν•΄μ„œ λƒ„μƒˆκ°€ λ‚˜λŠ” κ²½λ‘œκ°€ μžˆλŠ” 것을 λ¨Όμ € λ½‘μœΌλ©΄ λœλ‹€.




이λ₯Ό ν•΄κ²°ν•˜λŠ” 방법은

heappush(heap, [0, start_node, 1])
    flag = [1 for _ in range(cnt_node+1)]
    
    while heap:
        now_cost, now_node, now_flag = heappop(heap)

        if now_node in visited: continue
        visited.add(now_node)
        
        if flag[now_node] == 1 and now_flag == 0: flag[now_node] = 0

        for next_node in graph[now_node]:
            cost = graph[now_node][next_node]

            if (now_cost + cost) > table[next_node]: continue
            
            table[next_node] = now_cost + cost
            
            new_flag = now_flag
            # λ§Œμ•½, μ§€κΈˆ μ›€μ§μ΄λ €λŠ” λ„λ‘œκ°€ λƒ„μƒˆκ°€ λ‚˜λŠ” λ„λ‘œμ˜€μ„ 경우 flagλ₯Ό 0둜 λ°”κΏˆ
            if (now_node, next_node) in {(g, h), (h, g)}: new_flag = 0
    
            heappush(heap, [now_cost + cost, next_node, new_flag])

λƒ„μƒˆλ‚˜λŠ” 경둜λ₯Ό 지났을 λ•Œλ₯Ό λ‚˜νƒ€λ‚΄λŠ” is_via의 값을

경둜λ₯Ό 지났을 경우 0으둜 두고,

경둜λ₯Ό μ§€λ‚˜μ§€ μ•Šμ•˜μ„ 경우 1둜 λ‘μ–΄μ„œ

μš°μ„ μˆœμœ„ 큐 ν˜Ήμ€ νž™μ—μ„œ 경둜λ₯Ό μ§€λ‚œ 경우λ₯Ό λ¨Όμ € λ„μΆœν•˜κ²Œ ν•˜λ©΄ λœλ‹€.




이거 λ§ˆμ§€λ§‰ κΊΌ μ²˜λ¦¬ν•œλ‹€κ³  μ‹œκ°„ μ’€ 였래 κ±Έλ Έλ”°.... γ…œγ…œγ…œγ…œγ…œγ…œγ…œγ…œγ…œγ…œγ…œγ…œ

도움이 많이 됐던 κΈ€ 1
도움이 많이 됐던 κΈ€ 2

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

@tgyuuAn tgyuuAn added tgyuuAn ν•œ μ€„λ‘œλŠ” μ†Œκ°œν•  수 μ—†λŠ” λ‚¨μž. μž‘μ„± 쀑 ⏱️ labels May 13, 2024
@tgyuuAn tgyuuAn self-assigned this May 13, 2024
@tgyuuAn tgyuuAn changed the title 2024-05-14 54-tgyuuAn May 13, 2024
@tgyuuAn tgyuuAn marked this pull request as ready for review May 14, 2024 07:30
@alstjr7437
Copy link
Member

μ•„λ‹ˆ 이거 κ·Έλƒ₯ 문제만 읽고 μ΄ν•΄ν•˜λ €λ‹€κ°€ λ„μ €νžˆ λ¬Έν•΄λ ₯이 μ•ˆλ˜μ„œ κ²°κ΅­ 인터넷 검색을 ν–ˆλ„€μš”,...

κ·Έλƒ₯ λ‹€μ΅μŠ€νŠΈλΌ μ•Œκ³ λ¦¬μ¦˜μ—λ‹€κ°€ μ΅œλ‹¨ 경둜λ₯Ό λ³΄λŠ”λ° ν•΄λ‹Ή μ΅œλ‹¨ κ²½λ‘œκ°€ 점선을 μ§€λ‚˜κ²Œ 되면 True둜 λ°”κΏ”μ„œ ν•˜λ©΄ λ˜κ² κ΅°μš”..!!
λ§žλ‚˜μš”...?

일단 μ΄ν•΄ν•˜κ³  μ½”λ“œλ₯Ό κ΅¬ν˜„ν•΄λ³΄λ €κ³ μš”!(사싀 λ‹€μ΅μŠ€νŠΈλΌ 였래만이라 κΉŒλ¨Ήμ–΄μ„œ λ…ΈνŠΈ 보고 λ‹€μ‹œ 곡뢀해야할듯..)

@tgyuuAn
Copy link
Member Author

tgyuuAn commented May 16, 2024

μ•„λ‹ˆ 이거 κ·Έλƒ₯ 문제만 읽고 μ΄ν•΄ν•˜λ €λ‹€κ°€ λ„μ €νžˆ λ¬Έν•΄λ ₯이 μ•ˆλ˜μ„œ κ²°κ΅­ 인터넷 검색을 ν–ˆλ„€μš”,...

κ·Έλƒ₯ λ‹€μ΅μŠ€νŠΈλΌ μ•Œκ³ λ¦¬μ¦˜μ—λ‹€κ°€ μ΅œλ‹¨ 경둜λ₯Ό λ³΄λŠ”λ° ν•΄λ‹Ή μ΅œλ‹¨ κ²½λ‘œκ°€ 점선을 μ§€λ‚˜κ²Œ 되면 True둜 λ°”κΏ”μ„œ ν•˜λ©΄ λ˜κ² κ΅°μš”..!! λ§žλ‚˜μš”...?

일단 μ΄ν•΄ν•˜κ³  μ½”λ“œλ₯Ό κ΅¬ν˜„ν•΄λ³΄λ €κ³ μš”!(사싀 λ‹€μ΅μŠ€νŠΈλΌ 였래만이라 κΉŒλ¨Ήμ–΄μ„œ λ…ΈνŠΈ 보고 λ‹€μ‹œ 곡뢀해야할듯..)

λ§žμŠ΅λ‹ˆλ‹€ λ§žκ³ μš” ~ ~~~~

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도 감이 μ•ˆμ™€μ„œ
νƒœκ·œλ‹˜ PR + λ‹€λ₯Έ 풀이 κΈ€ μ •λ…ν–ˆμŠ΅λ‹ˆλ‹€ ..

풀이λ₯Ό 보고 μ΄ν•΄ν•˜λŠ”λ° ν•œμ°Έ κ±Έλ Έλ„€μš” ......... πŸ˜…

λ§ˆμ•„μΉ¨ μ§€κΈˆ μ „κ³΅μ—μ„œ λ‹€μ΅μŠ€νŠΈλΌ ν•˜κ³  μžˆμ—ˆλŠ”λ° κ³΅λΆ€ν•˜κ³  λ‹€μ‹œ 이 문제 λ„μ „ν•΄λ³΄κ² μλ‹ˆλ‹€ πŸ”₯πŸ”₯πŸ”₯πŸ”₯

PS.
image
μš” λΆ€λΆ„ μ˜€νƒ€μΈ 것 같은데 1->3->6 인거죠 ??

Copy link
Collaborator

@SeongHoonC SeongHoonC left a comment

Choose a reason for hiding this comment

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

μ €λž‘ 풀이가 μ™„μ „ λ‹¬λΌμ„œ λ‚¨κ²¨λ΄…λ‹ˆλ‹€.

s,g,h μ—μ„œ 좜발 μ΅œλ‹¨κ±°λ¦¬λ₯Ό κ΅¬ν•˜κ³ ..

s -> target 이 μ•„λž˜ λ‘κ°œ 쀑 ν•˜λ‚˜μ™€ 값이 같은지 λΉ„κ΅ν•©λ‹ˆλ‹€.

  1. s μ—μ„œ g κΉŒμ§€ μ΅œλ‹¨κ±°λ¦¬ + h μ—μ„œ λͺ©μ μ§€κΉŒμ§€ μ΅œλ‹¨κ±°λ¦¬ + g μ—μ„œ h λΉ„μš©
  2. s μ—μ„œ h κΉŒμ§€ μ΅œλ‹¨κ±°λ¦¬ + g μ—μ„œ λͺ©μ μ§€κΉŒμ§€ μ΅œλ‹¨κ±°λ¦¬ + g μ—μ„œ h λΉ„μš©
const val INF_1 = 987_654_321
fun main() {
    val br = BufferedReader(InputStreamReader(System.`in`))
    val T = br.readLine().toInt()

    repeat(T) {
        program(br)
    }
}

private fun program(br: BufferedReader) {
    val (n, m, t) = br.getIntInputs()
    val (s, g, h) = br.getIntInputs()

    val graph = Array(n + 1) { Array(n + 1) { INF_1 } }

    repeat(m) {
        val (a, b, d) = br.getIntInputs()
        graph[a][b] = d
        graph[b][a] = d
    }
    val distances = dijkstra(graph = graph, start = s, n = n)
    val distanceFormH = dijkstra(graph, h, n)
    val distanceFormG = dijkstra(graph, g, n)
    val gToHCost = graph[g][h]

    val candidates = (0 until t).map { br.readLine().toInt() }

    val answer = candidates.filter { candi ->
        distances[candi] == distances[g] + distanceFormH[candi] + gToHCost || distances[candi] == distances[h] + distanceFormG[candi] + gToHCost
    }

    println(answer.sorted().joinToString(" "))
}

private fun dijkstra(graph: Array<Array<Int>>, start: Int, n: Int): Array<Int> {
    val queue = ArrayDeque<Int>()
    queue.add(start)

    val distance = Array(n + 1) { INF_1 }
    distance[start] = 0
    while (queue.isNotEmpty()) {
        val now = queue.removeFirst()
        for (i in 1 until graph.size) {
            val cost = distance[now] + graph[now][i]
            if (cost >= distance[i]) {
                continue
            }
            distance[i] = cost
            queue.add(i)
        }
    }
    return distance
}

private fun BufferedReader.getIntInputs() = readLine().split(" ").map { it.toInt() }

@tgyuuAn
Copy link
Member Author

tgyuuAn commented May 18, 2024

μ €λž‘ 풀이가 μ™„μ „ λ‹¬λΌμ„œ λ‚¨κ²¨λ΄…λ‹ˆλ‹€.

s,g,h μ—μ„œ 좜발 μ΅œλ‹¨κ±°λ¦¬λ₯Ό κ΅¬ν•˜κ³ ..

s -> target 이 μ•„λž˜ λ‘κ°œ 쀑 ν•˜λ‚˜μ™€ 값이 같은지 λΉ„κ΅ν•©λ‹ˆλ‹€.

  1. s μ—μ„œ g κΉŒμ§€ μ΅œλ‹¨κ±°λ¦¬ + h μ—μ„œ λͺ©μ μ§€κΉŒμ§€ μ΅œλ‹¨κ±°λ¦¬ + g μ—μ„œ h λΉ„μš©
  2. s μ—μ„œ h κΉŒμ§€ μ΅œλ‹¨κ±°λ¦¬ + g μ—μ„œ λͺ©μ μ§€κΉŒμ§€ μ΅œλ‹¨κ±°λ¦¬ + g μ—μ„œ h λΉ„μš©
const val INF_1 = 987_654_321
fun main() {
    val br = BufferedReader(InputStreamReader(System.`in`))
    val T = br.readLine().toInt()

    repeat(T) {
        program(br)
    }
}

private fun program(br: BufferedReader) {
    val (n, m, t) = br.getIntInputs()
    val (s, g, h) = br.getIntInputs()

    val graph = Array(n + 1) { Array(n + 1) { INF_1 } }

    repeat(m) {
        val (a, b, d) = br.getIntInputs()
        graph[a][b] = d
        graph[b][a] = d
    }
    val distances = dijkstra(graph = graph, start = s, n = n)
    val distanceFormH = dijkstra(graph, h, n)
    val distanceFormG = dijkstra(graph, g, n)
    val gToHCost = graph[g][h]

    val candidates = (0 until t).map { br.readLine().toInt() }

    val answer = candidates.filter { candi ->
        distances[candi] == distances[g] + distanceFormH[candi] + gToHCost || distances[candi] == distances[h] + distanceFormG[candi] + gToHCost
    }

    println(answer.sorted().joinToString(" "))
}

private fun dijkstra(graph: Array<Array<Int>>, start: Int, n: Int): Array<Int> {
    val queue = ArrayDeque<Int>()
    queue.add(start)

    val distance = Array(n + 1) { INF_1 }
    distance[start] = 0
    while (queue.isNotEmpty()) {
        val now = queue.removeFirst()
        for (i in 1 until graph.size) {
            val cost = distance[now] + graph[now][i]
            if (cost >= distance[i]) {
                continue
            }
            distance[i] = cost
            queue.add(i)
        }
    }
    return distance
}

private fun BufferedReader.getIntInputs() = readLine().split(" ").map { it.toInt() }
  1. μ›λž˜ μ‹œμž‘μ§€ μ—μ„œ λ‹€μ΅μŠ€νŠΈλΌ ν•œ 번,
  2. G ν˜Ήμ€ H λ…Έλ“œ μ—μ„œ 각각 ν•œλ²ˆ μ”©

총 λ‹€μ΅μŠ€νŠΈλΌκ°€ 3번 λŒμ•„κ°€κΈ° λ•Œλ¬Έμ— μ‘°κΈˆμ€ λΉ„νš¨μœ¨μ μΌ 수 μžˆλŠ”λ° μ €λŠ” 생각도 λͺ»ν•΄λ³Έ ν’€μ΄λ„€μš”...

μ†Œμ€‘ν•©λ‹ˆλ‹€..

λ­”κ°€ 이런 ν’€μ΄λ‘œ ν’€μ–΄μ•Ό ν•˜λŠ” λ¬Έμ œκ°€ μžˆμ—ˆλŠ”λ°,,, μž μ‹œλ§Œμš” ..

@tgyuuAn
Copy link
Member Author

tgyuuAn commented May 18, 2024

@SeongHoonC μ°Ύμ•˜λ”° #84 이거도 ν•œ 번 ν’€μ–΄λ³΄μ‹œλŠ” κ±° μΆ”μ²œλ“œλ¦½λ‹ˆλ‹€!!

3λͺ…μ˜ 리뷰어 μ€‘μ—μ„œ 혼자 ν’€μ–΄μ£Όμ…¨λ„€μš”.... 정말 κ°μ‚¬λ“œλ¦½λ‹ˆλ‹€ (__)

@alstjr7437 @H0ngJu 퍽퍽

@SeongHoonC
Copy link
Collaborator

@SeongHoonC μ°Ύμ•˜λ”° #84 이거도 ν•œ 번 ν’€μ–΄λ³΄μ‹œλŠ” κ±° μΆ”μ²œλ“œλ¦½λ‹ˆλ‹€!!

3λͺ…μ˜ 리뷰어 μ€‘μ—μ„œ 혼자 ν’€μ–΄μ£Όμ…¨λ„€μš”.... 정말 κ°μ‚¬λ“œλ¦½λ‹ˆλ‹€ (__)

@alstjr7437 @H0ngJu 퍽퍽

이미 ν’€μ—ˆμ§€μš© γ…Žγ…Ž

@tgyuuAn tgyuuAn merged commit 45ac9fd into main May 18, 2024
3 checks passed
@tgyuuAn tgyuuAn deleted the 54-tgyuuAn branch May 18, 2024 12:22
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.

4 participants