728x90
반응형
https://www.acmicpc.net/problem/5972
5972번: 택배 배송
농부 현서는 농부 찬홍이에게 택배를 배달해줘야 합니다. 그리고 지금, 갈 준비를 하고 있습니다. 평화롭게 가려면 가는 길에 만나는 모든 소들에게 맛있는 여물을 줘야 합니다. 물론 현서는
www.acmicpc.net
#include <iostream>
#include <vector>
#include <queue>
#define MAX_VERTEX 50001
#define MAX 50001
#define INF 987654321
using namespace std;
int n, m;
int d[MAX_VERTEX];
vector<pair<int, int>> edge[MAX];
void dijkstra(){
d[1] = 0;
priority_queue<pair<int, int>> pq;
pq.push({0, 1});
while(!pq.empty()){
int current = pq.top().second;
int start_to_current_dis = -pq.top().first;
pq.pop();
if(d[current] < start_to_current_dis) continue;
for(int i = 0; i < edge[current].size(); i++){
int next = edge[current][i].second;
int start_to_next_dis = start_to_current_dis + edge[current][i].first;
if(d[next] > start_to_next_dis){
d[next] = start_to_next_dis;
pq.push({-start_to_next_dis, next});
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for(int i = 0; i <= n; i++)
d[i] = INF;
for(int i = 0; i < m; i++){
int a, b, c;
cin >> a >> b >> c;
edge[a].push_back({c, b});
edge[b].push_back({c, a});
}
dijkstra();
cout << d[n] << endl;
return 0;
}
728x90
반응형
'Algorithm > BAEKJOON' 카테고리의 다른 글
[BOJ] 11779번 최소비용 구하기 2 (C++) (0) | 2022.08.04 |
---|---|
[BOJ] 9694번 무엇을 아느냐가 아니라 누구를 아느냐가 문제다 (C++) (0) | 2022.08.04 |
[BOJ] 17250번 은하철도 (C++) (0) | 2022.08.01 |
[BOJ] 1863번 스카이라인 쉬운거 (C++) (0) | 2022.07.31 |
[BOJ] 16401번 과자 나눠주기 (C++) (0) | 2022.07.31 |