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
- dfs
- ๊ทธ๋ฆฌ๋
- ์์ ์ ๋ ฌ
- ์ต๋จ ๊ฒฝ๋ก
- web
- c++
- ์คํ
- Reversing
- Spring
- ๋์ ํฉ
- ์ฌ๊ท
- ๋ฐ์ดํฌ์คํธ๋ผ
- DP
- ์๋ฎฌ๋ ์ด์
- GCP
- ์ด๋ถ ํ์
- ๋งต
- ๊ตฌํ
- BFS
- OS
- ์ฐ์ ์์ ํ
- java
- dynamic debugging
- CVE
- ๋ถํ ์ ๋ณต
- ๋ฌธ์์ด
- JPA
- error
- ๋ฐฑํธ๋ํน
- thymeleaf
Archives
- Today
- Total
hades
[Baekjoon] 1753๋ฒ: ์ต๋จ๊ฒฝ๋ก ๋ณธ๋ฌธ
๐ฅ ๋ฌธ์
https://www.acmicpc.net/problem/1753
๐ ์ค๊ณ
adj_matrix๋ฅผ ์ฌ์ฉํ๋ฉด, 20000*20000*4Byte๋ก ๋ฉ๋ชจ๋ฆฌ ์ด๊ณผ๊ฐ ๋ฐ์ํ๋ค. adj_list๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค.
์ฐ์ ์์ ํ๋ฅผ ์ด์ฉํ์ฌ ์ด๋ค ์ ์์ ๊ฐ ์ ์๋ ์ ๊น์ง์ ๊ฒฝ๋ก ์ค ์์ ๊ฒ๋ถํฐ ๋ฝ๋๋ค. ์ด๋ ๊ฒ ํ๋ฉด ๋ค์ ๊ฐฑ์ ํ๋ ๊ฒฝ๋ก์ ๋น์ฉ์ ์์ ์๋ ๊ฒฝ๋ก์ ๊ธธ์ด๋ณด๋ค ํญ์ ํฌ๋ค. ๋ฐ๋ผ์, ๋ฐ๋ณต๋ฌธ์ ์ต๋ ํ์๋ ๊ฐ์ ์ ๊ฐ์์ธ 300000์ด ๋๋ค.
๐ ํ์ด
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
int n, e, k, u, v, w, result = 0;
vector<vector<pair<int, int>>> adj_list(20001);
vector<int> dist(20001, 1e9 + 7);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
void Dijkstra() {
dist[k] = 0;
pq.push({ 0, k });
while (!pq.empty()) {
int cur_cost = pq.top().first;
int cur_node = pq.top().second;
pq.pop();
for (int i = 0; i < adj_list[cur_node].size(); i++) {
int next_node = adj_list[cur_node][i].second;
int next_cost = cur_cost + adj_list[cur_node][i].first;
if (dist[next_node] > next_cost) {
dist[next_node] = next_cost;
pq.push({ next_cost, next_node });
}
}
}
}
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> e >> k;
for (int i = 0; i < e; i++) {
cin >> u >> v >> w;
adj_list[u].push_back({ w, v });
}
Dijkstra();
for (int i = 1; i <= n; i++) {
if (dist[i] == 1e9 + 7) {
cout << "INF" << "\n";
}
else {
cout << dist[i] << "\n";
}
}
return 0;
}
'๐ PS > Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Baekjoon] 11675๋ฒ: ํ์๋จธ์ (0) | 2024.08.06 |
---|---|
[Baekjoon] 1916๋ฒ: ์ต์๋น์ฉ ๊ตฌํ๊ธฐ (0) | 2024.08.06 |
[Baekjoon] 1181๋ฒ: ๋จ์ด ์ ๋ ฌ (0) | 2024.08.04 |
[Baekjoon] 1504๋ฒ: ํน์ ํ ์ต๋จ ๊ฒฝ๋ก (0) | 2024.08.03 |
[Baekjoon] 1238๋ฒ: ํํฐ (0) | 2024.08.03 |