- 2025春学期西门第25.26.27次课0522.27.29
c++
- 2025-5-29 17:12:05 @
import heapq
def solve():
import sys
input = sys.stdin.read
data = input().split()
idx = 0
n = int(data[idx]); idx +=1
m = int(data[idx]); idx +=1
S = int(data[idx]); idx +=1
T = int(data[idx]); idx +=1
adj = [[] for _ in range(n)]
for _ in range(m):
u = int(data[idx]); idx +=1
v = int(data[idx]); idx +=1
d = int(data[idx]); idx +=1
c = int(data[idx]); idx +=1
adj[u].append((v, d, c))
adj[v].append((u, d, c))
INF = float('inf')
dist = [INF] * n
cost = [INF] * n
dist[S] = 0
cost[S] = 0
heap = []
heapq.heappush(heap, (0, 0, S))
while heap:
current_dist, current_cost, u = heapq.heappop(heap)
if u == T:
print(current_cost)
return
if current_dist > dist[u] or (current_dist == dist[u] and current_cost > cost[u]):
continue
for v, d, c in adj[u]:
new_dist = current_dist + d
new_cost = current_cost + c
if new_dist < dist[v]:
dist[v] = new_dist
cost[v] = new_cost
heapq.heappush(heap, (new_dist, new_cost, v))
elif new_dist == dist[v] and new_cost < cost[v]:
cost[v] = new_cost
heapq.heappush(heap, (new_dist, new_cost, v))
print(-1)
solve()
0 条评论
目前还没有评论...