-
Notifications
You must be signed in to change notification settings - Fork 1
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
17-SeongHoonC #62
Conversation
κ·Έλν μ΅λ¨ κ²½λ‘ λ¬Έμ λ₯Ό μνκ³ κ°λ μ κΉλ¨Ήμμ§ μ€λ λμ λ€μ΅μ€νΈλΌ ν¨μ λΆλΆμ μΈν°λ·μ μ°Έκ³ νλ€μ!! μμ΄λμ΄λ κΈ°λ³Έμ μΌλ‘ λκ°μ΄ νμ΅λλ€!!
λ€λ¦¬λ©΄ λλκΉ κ²°κ΅ 3κ°μ λ€μ΅μ€νΈλΌλ₯Ό λλ¦¬κ³ μλλ₯Ό ν΅ν΄ κ²°κ³Όκ°μ μ°Ύμ λμ΅λλ€!!
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) |
λ°λ‘ Readmeμ λ¬Έμ μ μ μ μΌλΆλ¬ μΆκ°λ₯Ό μνμ 건κ°μ!!? |
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 | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
λ°λ‘ μ°μ μμ ν λ°μ΄ν° ꡬ쑰κΉμ§ λ§λ€μ΄μ ν΄μΌνλ건κ°μ μ½νλ¦°μμλ????
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
κ·Έλ μ΅λλ€ νν
There was a problem hiding this 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))
π λ¬Έμ λ§ν¬
νΉμ ν μ΅λ¨ κ²½λ‘
βοΈ μμλ μκ°
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 μΌλ‘ λ³κ²½νλ€.
π μλ‘κ² μκ²λ λ΄μ©
Max κ°μ μ μ νμ..
Comparable κ°μ²΄λ₯Ό λ§λ€λ©΄ PriorityQueue μ νμ μΌλ‘ μ§μ κ°λ₯νλ€.