#include <iostream>
#include <vector>
#include <queue>
#include <climits>
using namespace std;
int N, M;
vector<pair<int, int>> table[1001];
int dijkstra(int s, int target) {
// min heap (first 기준)
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push(make_pair(0, s));
int dist[1001];
for (int i = 1; i <= N; i++) {
dist[i] = INT_MAX;
}
dist[s] = 0;// 출발지로 가는 비용은 0
while (!pq.empty()) {
int u = pq.top().second;
int w = pq.top().first;
pq.pop();
if (dist[u] < w) continue;
for (int i = 0; i < table[u].size(); i++) {
int v = table[u][i].second;
int x = table[u][i].first;
if (dist[v] > w + x) {
dist[v] = w + x;
pq.push(make_pair(w + x, v));
}
}
}
return dist[target];
}
int main()
{
cin.tie(NULL);
ios_base::sync_with_stdio(false);
scanf("%d %d", &N, &M);
int departure, arrival;
int u, v, w;
for (int i = 0; i < M; i++) {
// u -> v 로 갈때 비용 w
scanf("%d %d %d", &u, &v, &w);
table[u].push_back(make_pair(w, v));
}
scanf("%d %d", &departure, &arrival);
int ans = dijkstra(departure, arrival);
printf("%d\n", ans);
return 0;
}
Leave a comment