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 |
Tags
- 그래프
- DFS
- 재귀
- 구현
- 수학
- 다익스트라
- 투 포인터
- 알고리즘
- 자료구조
- 유니온 파인드
- c++
- 우선순위 큐
- 유니티
- 시뮬레이션
- 문자열
- VR
- 스택
- BFS
- 트리
- 누적 합
- Team Fortress 2
- Unreal Engine 5
- ue5
- 그리디 알고리즘
- 백준
- 백트래킹
- XR Interaction Toolkit
- 브루트포스
- 다이나믹 프로그래밍
- 정렬
Archives
- Today
- Total
1일1알
백준 1753번 최단경로 C++ 본문
정점의 개수가 20000개여서 인접 행렬로 하면 20000*20000 = 400000000 (4억) 이라서 시간초과가 날 것 같아서 인접 리스트 방식으로 그래프를 만들고 다익스트라로 풀었다.
#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>
using namespace std;
using int64 = long long;
int v, e;
vector<vector<pair<int, int>>> vertexs;
struct VertexCost {
bool operator<(const VertexCost& other) const {
return cost < other.cost;
}
bool operator>(const VertexCost& other) const {
return cost > other.cost;
}
int vertex;
int cost;
};
void Dijikstra(int start) {
vector<int> best(v + 1, INT_MAX);
priority_queue<VertexCost, vector<VertexCost>, greater<VertexCost>> pq;
pq.push({ start,0 });
best[start] = 0;
while (!pq.empty()) {
auto curr = pq.top();
pq.pop();
if (curr.cost > best[curr.vertex])
continue;
for (auto a : vertexs[curr.vertex]) {
int nextCost = curr.cost + a.second;
if (nextCost >= best[a.first])
continue;
best[a.first] = nextCost;
pq.push({ a.first,nextCost });
}
}
for (int i = 1; i <= v; i++) {
if (best[i] == INT_MAX)
cout << "INF";
else
cout << best[i];
cout << "\n";
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> v >> e;
int start;
cin >> start;
vertexs = vector<vector<pair<int, int>>>(v + 1, vector<pair<int, int>>());
for (int i = 0; i < e; i++) {
int a, b, c;
cin >> a >> b >> c;
vertexs[a].push_back({ b,c });
}
Dijikstra(start);
};
'알고리즘' 카테고리의 다른 글
백준 11404번 플로이드 C++ (0) | 2022.06.18 |
---|---|
백준 1967번 트리의 지름 C++ (0) | 2022.06.17 |
백준 1504번 특정한 최단 경로 C++ (0) | 2022.06.15 |
백준 5639번 이진 검색 트리 C++ (0) | 2022.06.14 |
백준 1916번 최소비용 구하기 C++ (0) | 2022.06.13 |