반응형
https://www.acmicpc.net/problem/1916
1916번: 최소비용 구하기
첫째 줄에 도시의 개수 N(1 ≤ N ≤ 1,000)이 주어지고 둘째 줄에는 버스의 개수 M(1 ≤ M ≤ 100,000)이 주어진다. 그리고 셋째 줄부터 M+2줄까지 다음과 같은 버스의 정보가 주어진다. 먼저 처음에는 그
www.acmicpc.net
#include <bits/stdc++.h>
using namespace std;
int res[1001];
struct Node {
int x, dist;
bool operator<(const Node& b) const{
return dist > b.dist;
}
};
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
memset(res, 0x3f, sizeof(res));
int n, m, st, end;
vector<Node> map[1001];
cin >> n >> m;
for (int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
map[a].push_back({ b,c });
}
cin >> st >> end;
priority_queue<Node> Q;
Q.push({ st,0 });
res[st] = 0;
while (!Q.empty()) {
int x = Q.top().x;
int d = Q.top().dist;
Q.pop();
if (res[x] < d) continue;
res[x] = d;
if (x == end) {
cout << d << '\n';
return 0;
}
for (int i = 0; i < map[x].size(); i++) {
int nx = map[x][i].x;
int nd = map[x][i].dist;
if (res[nx] > nd + d) {
Q.push({ nx,nd + d });
}
}
}
return 0;
}
https://www.acmicpc.net/problem/1916
반응형
'Algorithm > problem' 카테고리의 다른 글
백준 9370번 : 미확인 도착지 - 다익스트라 (0) | 2022.02.15 |
---|---|
백준 11779번 : 최소비용 구하기2 - 다익스트라 (0) | 2022.02.15 |
백준 10800 : 컬러볼 누적합, 투포인터 활용 C++ (0) | 2022.02.14 |
백준 10166번: 관중석 문제풀이코드 C++ (0) | 2022.02.13 |
백준 2573번 : 빙산 - BFS 활용 C++ (0) | 2022.02.13 |