- 문제 링크 : boj.kr/1916
- 난이도 : G5
- 태그 :다익스트라
코드
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define INF 987654321
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll, ll>;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n, m;
cin >> n >> m;
vector<vector<pii>> edge(n+1);
vector<int> dist(n+1, INF);
for(int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
edge[a].emplace_back(make_pair(b, c));
}
int s, e;
cin >> s >> e;
dist[s] = 0;
priority_queue<pii> _pq;
_pq.push({s, 0});
while(!_pq.empty()) {
pii curr = _pq.top(); _pq.pop();
if(curr.second > dist[curr.first]) continue;
for(auto u : edge[curr.first]) {
int b = u.first, c = u.second;
if(dist[curr.first] + c < dist[b]) {
dist[b] = dist[curr.first]+c;
_pq.push({b, dist[b]});
}
}
}
cout << dist[e];
return 0;
}
풀이
다익스트라 기본문제이다.
728x90
'PS' 카테고리의 다른 글
BOJ 6593 : 상범 빌딩 (0) | 2024.05.03 |
---|---|
BOJ 2170 : 선 긋기 (0) | 2024.05.02 |
BOJ 26216 : 은나무 (0) | 2024.04.30 |
BOJ 16398 : 행성 연결 (0) | 2024.04.29 |
BOJ 5696 : 숫자 세기 (0) | 2024.04.28 |