hwooo
BOJ (C/C++) 1916번: 최소비용 구하기 본문
https://www.acmicpc.net/problem/1916
1916번: 최소비용 구하기
첫째 줄에 도시의 개수 N(1 ≤ N ≤ 1,000)이 주어지고 둘째 줄에는 버스의 개수 M(1 ≤ M ≤ 100,000)이 주어진다. 그리고 셋째 줄부터 M+2줄까지 다음과 같은 버스의 정보가 주어진다. 먼저 처음에는 그
www.acmicpc.net


코드
#include <stdio.h>
#include <vector>
#include <queue>
#include <algorithm>
#include <limits.h>
using namespace std;
vector<pair<int, int>> C[1001];
priority_queue <pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
int W[20001];
void Dijkstra(int s) {
int now, next, weight;
pq.push({ 0,s });
W[s] = 0;
while (!pq.empty()) {
now = pq.top().second, weight = pq.top().first;
pq.pop();
if (W[now] < weight) continue;
for (int i = 0; i < C[now].size(); i++) {
next = C[now][i].first, weight = C[now][i].second;
if (W[next] > W[now] + weight) {
W[next] = W[now] + weight;
pq.push({ W[next],next });
}
}
}
}
int main() {
int N, M;
int s, e, w;
int start, end;
scanf("%d %d", &N, &M);
for (int i = 0; i < M; i++) {
scanf("%d %d %d", &s, &e, &w);
C[s].push_back({ e,w });
}
scanf("%d %d", &start, &end);
fill(W, W + N + 1, INT_MAX);
Dijkstra(start);
printf("%d", W[end]);
return 0;
}'Study > Algorithm' 카테고리의 다른 글
| BOJ (C/C++) 1504번: 특정한 최단 경로 (0) | 2022.11.19 |
|---|---|
| BOJ (C/C++) 1238번: 파티 (0) | 2022.11.19 |
| BOJ (C/C++) 1753번: 최단경로 (0) | 2022.11.18 |
| BOJ (C/C++) 1158번: 요세푸스 문제 (0) | 2022.11.16 |
| BOJ (C/C++) 1159번: 농구 경기 (0) | 2022.11.16 |