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
- 트리
- VR
- 스택
- 알고리즘
- 투 포인터
- 정렬
- 유니온 파인드
- 누적 합
- 자료구조
- 수학
- ue5
- 유니티
- 우선순위 큐
- Unreal Engine 5
- 다이나믹 프로그래밍
- 브루트포스
- c++
- 시뮬레이션
- 구현
- 그리디 알고리즘
- 백준
- 다익스트라
- 백트래킹
- Team Fortress 2
- 문자열
- 그래프
- XR Interaction Toolkit
- DFS
- 재귀
- BFS
Archives
- Today
- Total
1일1알
백준 17396번 백도어 C++ 본문
다익스트라
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>
#include <bitset>
using namespace std;
using int64 = long long;
int n, m;
struct VertexCost {
int64 vertex;
int64 cost;
bool operator<(const VertexCost& other) const{
return cost < other.cost;
}
bool operator>(const VertexCost& other) const{
return cost > other.cost;
}
};
vector<vector<pair<int64, int64>>> graph;
vector<bool> CanGo;
int64 Dijikstra() {
priority_queue<VertexCost, vector<VertexCost>, greater<VertexCost>> pq;
vector<int64> best(n, INT64_MAX);
pq.push({ 0,0 });
best[0] = 0;
while (!pq.empty()) {
auto curr = pq.top();
pq.pop();
if (best[curr.vertex] < curr.cost) continue;
for (auto a : graph[curr.vertex]) {
int64 nextVertex = a.first;
int64 nextCost = curr.cost + a.second;
if (!CanGo[nextVertex]) continue;
if (best[nextVertex] <= nextCost) continue;
pq.push({ nextVertex,nextCost });
best[nextVertex] = nextCost;
}
}
if (best[n - 1] == INT64_MAX) return -1;
return best[n - 1];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m;
graph = vector<vector<pair<int64, int64>>>(n, vector<pair<int64, int64>>());
CanGo = vector<bool>(n);
for (int i = 0; i < n; i++) {
int canGo;
cin >> canGo;
if (canGo == 0) CanGo[i] = true;
else CanGo[i] = false;
}
CanGo[n - 1] = true;
for (int i = 0; i < m; i++) {
int64 a, b, t;
cin >> a >> b >> t;
graph[a].push_back({ b,t });
graph[b].push_back({ a,t });
}
int64 ans = Dijikstra();
cout << ans;
}
'알고리즘' 카테고리의 다른 글
백준 2252번 줄 세우기 C++ (1) | 2022.09.23 |
---|---|
백준 14442번 벽 부수고 이동하기 2 C++ (1) | 2022.09.21 |
백준 1890번 점프 C++ (0) | 2022.09.19 |
백준 17135번 캐슬 디펜스 C++ (0) | 2022.09.18 |
백준 25307번 시루의 백화점 구경 C++ (0) | 2022.09.17 |