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 | 31 |
Tags
- 과대적합
- 3d
- 기울기 소실
- 베버의 법칙
- 딥러닝
- 동적계획법
- 단층 퍼셉트론
- 다익스트라
- 알고리즘
- dl
- 손실 함수
- 이진 분류
- 범용 근사 정리
- bfs
- BOJ
- 백준
- 순방향 신경망
- 베르누이 분포
- vanishing gradient
- 이진분류
- OpenGL
- union-find
- deep learning
- c++
- 경사하강법
- Perceptron
- DP
- feedforward neural network
- 계단 함수
- dijkstra
Archives
- Today
- Total
Hello COCOBALL!
C++ [BOJ] 백준 5972번 택배 배송 본문
https://www.acmicpc.net/problem/5972
SOLUTION
다익스트라 알고리즘으로 간단하게 풀 수 있는 문제
코드
#include <iostream>
#include <vector>
#include <queue>
#define MAX 50001
#define INF 1e9
using namespace std;
int n, m;
int dist[MAX + 1];
vector<pair<int, int>>v[MAX + 1];
void dijkstra(int start) {
for (int i = 1; i <= n; i++) dist[i] = INF;
priority_queue<pair<int, int>>pq;
pq.push({ 0,start });
dist[start] = 0;
while (!pq.empty()) {
int cost = -pq.top().first;
int cur = pq.top().second;
pq.pop();
for (int i = 0; i < v[cur].size(); i++) {
int next = v[cur][i].first;
int newcost = v[cur][i].second + cost;
if (dist[next] > newcost) {
dist[next] = newcost;
pq.push({ -dist[next],next });
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
cin >> n >> m;
while (m--) {
int a, b, c;
cin >> a >> b >> c;
v[a].push_back({ b,c });
v[b].push_back({ a,c });
}
dijkstra(1);
cout << dist[n];
}
'BOJ' 카테고리의 다른 글
C++ [BOJ] 백준 1699번 제곱수의 합 (0) | 2022.07.08 |
---|---|
C++ [BOJ] 백준 2294번 동전 2 (0) | 2022.07.08 |
C++ [BOJ] 백준 1504번 특정한 최단 경로 (0) | 2022.07.07 |
C++ [BOJ] 백준 12851번 숨바꼭질 2 (0) | 2022.07.05 |
C++ [BOJ] 백준 13549번 숨바꼭질 3 (0) | 2022.07.05 |