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

17-SeongHoonC #62

Merged
merged 1 commit into from
Mar 28, 2024
Merged

17-SeongHoonC #62

merged 1 commit into from
Mar 28, 2024

Conversation

SeongHoonC
Copy link
Collaborator

πŸ”— 문제 링크

νŠΉμ •ν•œ μ΅œλ‹¨ 경둜

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

2μ‹œκ°„

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

start μ—μ„œ μΆœλ°œν–ˆμ„ λ•Œ end κΉŒμ§€μ˜ μ΅œλ‹¨κ±°λ¦¬λ₯Ό κ΅¬ν•˜λŠ” λ‹€μ΅μŠ€νŠΈλΌ μ•Œκ³ λ¦¬μ¦˜μ„ μ§ λ‹€.

κ²°κ΅­ V1 V2 λ₯Ό μ§€λ‚˜κ°€λŠ” μ΅œλ‹¨κ±°λ¦¬ 경우의 μˆ˜λŠ” 두 가지가 μ‘΄μž¬ν•œλ‹€.
1 - v1 - v2 - N
1 - v2 - v1 - N

dijkstra(1,v1) + dijkstra(v1,v2) + dijkstra(v2,N)
dijkstra(1,v2) + dijkstra(v2,v1) + dijkstra(v1,N)

λ‘˜ 쀑에 μ΅œμ†Œκ°’μ΄ 닡이 λœλ‹€.

max 값을 1μ–΅μœΌλ‘œ μž‘μ•˜λ‹€κ°€ μ΅œλŒ€ 200,000 * 1000 을 보고 μ•ˆλœλ‹€λŠ” 것을 κΉ¨λ‹¬μ•˜λ‹€. 걍 9μ–΅μœΌλ‘œ λŠ˜λ €μ€¬λ‹€

λ˜ν•œ μ–΅λ‹¨μœ„ ν•© 계산 λ•Œλ¬Έμ— νƒ€μž…μ„ Long 으둜 λ³€κ²½ν–ˆλ‹€.

import java.io.BufferedReader
import java.io.InputStreamReader
import java.util.*
import kotlin.math.min

private const val INF = 987_654_321L

fun main() {
    val br = BufferedReader(InputStreamReader(System.`in`))

    val (N, E) = br.readLine().split(" ").map { it.toInt() }
    val graph = Array(N + 1) { mutableListOf<Pair<Int, Long>>() }
    for (i in 0 until E) {
        val (start, end, cost) = br.readLine().split(" ").map { it.toInt() }
        graph[start].add((end to cost.toLong()))
        graph[end].add((start to cost.toLong()))
    }

    val (v1, v2) = br.readLine().split(" ").map { it.toInt() }
    // 1 to v1 + v1 to v2 + v2 to N
    val cost1 = dijkstra(N, 1, v1, graph) + dijkstra(N, v1, v2, graph) + dijkstra(N, v2, N, graph)
    // 1 to v2 + v2 to v1 + v1 to N
    val cost2 = dijkstra(N, 1, v2, graph) + dijkstra(N, v2, v1, graph) + dijkstra(N, v1, N, graph)

    val answer = min(cost1, cost2)
    println(if (answer >= INF) -1 else answer)
}

// start to end μ΅œλ‹¨κ±°λ¦¬ κ΅¬ν•˜κΈ°
private fun dijkstra(N: Int, start: Int, end: Int, graph: Array<MutableList<Pair<Int, Long>>>): Long {
    if (start == end) {
        return 0
    }
    val pq = PriorityQueue<Edge>()
    val distance = Array(N + 1) { INF }

    distance[start] = 0
    pq.add(Edge(start, 0))

    while (pq.isNotEmpty()) {
        val (now, dist) = pq.poll()
        // ν˜„μž¬ λ…Έλ“œμ—μ„œ 갈 수 μžˆλŠ” λͺ¨λ“  엣지 쀑 μ΅œλ‹¨κ±°λ¦¬μ΄λ©΄ 큐에 λ„£λŠ”λ‹€.
        for ((next, cost) in graph[now]) {
            val nextCost: Long = dist + cost
            if (distance[next] > nextCost) {
                distance[next] = nextCost
                pq.add(Edge(next, nextCost))
            }
        }
    }

    return distance[end]
}

data class Edge(val start: Int, val cost: Long) : Comparable<Edge> {

    // 거리(λΉ„μš©)κ°€ 짧은 것이 높은 μš°μ„ μˆœμœ„λ₯Ό 가지도둝 μ„€μ •
    override operator fun compareTo(other: Edge): Int {
        return if (this.cost < other.cost) -1 else 1
    }
}

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

Max 값을 잘 μ •ν•˜μž..
Comparable 객체λ₯Ό λ§Œλ“€λ©΄ PriorityQueue 의 νƒ€μž…μœΌλ‘œ 지정 κ°€λŠ₯ν•˜λ‹€.

@alstjr7437
Copy link
Member

κ·Έλž˜ν”„ μ΅œλ‹¨ 경둜 문제λ₯Ό μ•ˆν’€κ³  κ°œλ…μ„ κΉŒλ¨Ήμ€μ§€ 였래 λ˜μ„œ λ‹€μ΅μŠ€νŠΈλΌ ν•¨μˆ˜ 뢀뢄은 인터넷을 μ°Έκ³  ν–ˆλ„€μš”!!

μ•„μ΄λ””μ–΄λŠ” 기본적으둜 λ˜‘κ°™μ΄ ν–ˆμŠ΅λ‹ˆλ‹€!!
vκΉŒμ§€ κ°€λŠ”λ°
μš°μ„ 

  1. μ²˜μŒλΆ€ν„° v1,v2κΉŒμ§€ κ°€λŠ” 경우 계산
  2. v1μ—μ„œ v2, vκ°€λŠ” 경우 계산
  3. v2μ—μ„œ v1, vκ°€λŠ” 경우 계산

듀리면 λ˜λ‹ˆκΉŒ κ²°κ΅­ 3개의 λ‹€μ΅μŠ€νŠΈλΌλ₯Ό 돌리고 μ•„λž˜λ₯Ό 톡해 결과값을 μ°Ύμ•„ λƒˆμŠ΅λ‹ˆλ‹€!!

  1. start -> v1 -> v2 -> v 경우
  2. start -> v2 -> v1 -> v 경우
  3. 1,2쀑에 더 μž‘μ€ κ°’μœΌλ‘œ 갈 수 μžˆλŠ” κ²½μš°κ°€ μ •λ‹΅
from heapq import * 
import sys

input = sys.stdin.readline
# μ΅œλŒ€κ°’ μ„ μ–Έ
INF = int(1e9)

v, e = map(int, input().split())
graph = [[] for _ in range(v + 1)]

for _ in range(e):
    x, y, cost = map(int, input().split())
    graph[x].append((y, cost))
    graph[y].append((x, cost))


def dijkstra(start):
    distance = [INF] * (v + 1)
    q = []
    heappush(q, (0, start))
    distance[start] = 0

    while q:
        dist, now = heappop(q)

        if distance[now] < dist:
            continue

        for i in graph[now]:
            cost = dist + i[1]

            if distance[i[0]] > cost:
                distance[i[0]] = cost
                heappush(q, (cost, i[0]))

    return distance

v1, v2 = map(int, input().split())

v_distance = dijkstra(1)
v1_distance = dijkstra(v1)
v2_distance = dijkstra(v2)

v1_path = v_distance[v1] + v1_distance[v2] + v2_distance[v]
v2_path = v_distance[v2] + v2_distance[v1] + v1_distance[v]

result = min(v1_path, v2_path)
print(result if result < INF else -1)

@alstjr7437
Copy link
Member

λ”°λ‘œ Readme에 문제 선정은 μΌλΆ€λŸ¬ μΆ”κ°€λ₯Ό μ•ˆν•˜μ‹ κ±΄κ°€μš”!!?

Comment on lines +53 to +59
data class Edge(val start: Int, val cost: Long) : Comparable<Edge> {

// 거리(λΉ„μš©)κ°€ 짧은 것이 높은 μš°μ„ μˆœμœ„λ₯Ό 가지도둝 μ„€μ •
override operator fun compareTo(other: Edge): Int {
return if (this.cost < other.cost) -1 else 1
}
}
Copy link
Member

Choose a reason for hiding this comment

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

λ”°λ‘œ μš°μ„ μˆœμœ„ 큐 데이터 κ΅¬μ‘°κΉŒμ§€ λ§Œλ“€μ–΄μ„œ ν•΄μ•Όν•˜λŠ”κ±΄κ°€μš” μ½”ν‹€λ¦°μ—μ„œλŠ”????

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

κ·Έλ ‡μŠ΅λ‹ˆλ‹€ ν•˜ν•˜

Copy link
Member

@fnzksxl fnzksxl left a comment

Choose a reason for hiding this comment

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

였,, μ˜ˆμ „μ— 재밌게 ν’€μ—ˆλ˜ λ¬Έμ œλ„€μš”. 저도 λ˜‘κ°™μ΄ ν’€μ—ˆμŠ΅λ‹ˆλ‹€
쀑간 μ§€μ κΉŒμ§€μ˜ λ‹€μ΅μŠ€νŠΈλΌλž‘ κ·Έ 지점뢀터 λκΉŒμ§€μ˜ λ‹€μ΅μŠ€νŠΈλΌλ₯Ό λŒλ¦¬λŠ” 아이디어!

import sys
import heapq

def dijkstra(start,end):
    distLst=[inf]*(n+1)
    q=[]
    heapq.heappush(q,(0,start))
    distLst[start]=0

    while q:
        dist,node=heapq.heappop(q)
        if distLst[node]<dist:
            continue
        for weight,edge in graph[node]:
            cost=distLst[node]+weight
            if cost<distLst[edge]:
                distLst[edge]=cost
                heapq.heappush(q,(cost,edge))
    return distLst[end]
        
inf=sys.maxsize
n,e=map(int,sys.stdin.readline().split())

graph=[[] for _ in range(n+1)]

for _ in range(e):
    e1,e2,weight=map(int,sys.stdin.readline().split())
    graph[e1].append((weight,e2))
    graph[e2].append((weight,e1))

v1,v2=map(int,sys.stdin.readline().split())

path1=dijkstra(1,v1)+dijkstra(v1,v2)+dijkstra(v2,n)
path2=dijkstra(1,v2)+dijkstra(v2,v1)+dijkstra(v1,n)

if path1 >= inf and path2>= inf:
    print(-1)
else:
    print(min(path1,path2))

@SeongHoonC SeongHoonC merged commit 4b70a31 into main Mar 28, 2024
6 checks passed
@SeongHoonC SeongHoonC deleted the 17-SeongHoonC branch March 28, 2024 04:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants