Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- bfs
- 알고리즘
- 자료구조
- DP
- cs
- 코딩
- 오에스
- Stack
- 정석
- 구현
- 스택
- 브루트포스
- vector
- 정석학술정보관
- 그래프
- 컴퓨터공학과
- c++
- 컴공과
- 너비우선탐색
- 문제풀이
- 북리뷰
- 오퍼레이팅시스템
- 코테
- Operating System
- OS
- 컴공
- 개발
- Computer science
- coding
- 백준
Archives
- Today
- Total
Little Jay
[C++] 백준 1916번 - 최소비용 구하기 본문
Naive한 다익스트라 문제
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
#define INF 987654321
#define pii pair<int, int>
priority_queue<pii, vector<pii>, greater<pii>> pq;
int graph[1005][1005];
int dist[1005];
int n, m;
int u, v, w;
int start, last;
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
graph[i][j] = INF;
}
}
for (int i = 1; i <= n; i++) {
graph[i][i] = 0;
dist[i] = INF;
}
for (int i = 0; i < m; i++) {
cin >> u >> v >> w;
if (graph[u][v] > w) graph[u][v] = w;
}
cin >> start >> last;
dist[start] = 0;
pq.push({ 0, start });
while (!pq.empty()) {
int x = pq.top().first;
int U = pq.top().second;
pq.pop();
for (int i = 1; i <= n; i++) {
int V = i;
int W = graph[U][i];
if (W == INF) continue;
if (x + W < dist[V]) {
dist[V] = x + W;
pq.push({ x + W, V });
}
}
}
cout << dist[last] << endl;
return 0;
}
'알고리즘 > BOJ' 카테고리의 다른 글
[C++] 백준 9375번 - 패션왕 신해빈 (0) | 2022.07.02 |
---|---|
[C++] 백준 1389번 - 케빈 베이컨의 6단계 법칙 (0) | 2022.07.02 |
[C++] 백준 3613번 - Java vs C++ (0) | 2022.07.01 |
[C++] 백준 11049번 - 행렬 곱셈 순서 (0) | 2022.07.01 |
[C++] 백준 2150번 - Strongly Connected Component (0) | 2022.07.01 |
Comments