Notice
Recent Posts
Recent Comments
Link
Tags
- ํธ๋ฆฌ
- Python
- ์ํฐ๋
- nestjs
- ์์ฝ๋
- ์นด์นด์ค2021
- C++
- ์๊ณ ๋ฆฌ์ฆ
- ์ฌ๋ผ์ด๋ฉ ์๋์ฐ
- ๊ฐ์ฅ๊ฐ๊น์ด๊ณตํต์กฐ์
- BFS
- ๋นํธ๋งต
- ํ๋ฆฌ์จ๋ณด๋ฉ
- js
- Union-Find
- ์ด๋ถํ์
- ๋นํธ๋ง์คํน
- LCs
- go
- golang
- DFS
- ์น๋ฆฐ์ด
- ๋ฐฑ์๋ ํ๋ฆฌ์จ๋ณด๋ฉ
- ํ๋ก๊ทธ๋๋จธ์ค
- ์ฌ๊ท
- ๋์ ํ๋ก๊ทธ๋๋ฐ
- ๋ค์ต์คํธ๋ผ
- ๋ฐฑ์ค
- ์นด์นด์ค ์ฝํ
- DP
Archives
- Today
- Total
Hello Ocean! ๐ผ
[๋ฐฑ์ค/C++] 1916. ์ต์๋น์ฉ ๊ตฌํ๊ธฐ ๋ณธ๋ฌธ
๋ฐ์ํ
๋ฌธ์
ํ์ด
๋ค์ต์คํธ๋ผ ์๊ณ ๋ฆฌ์ฆ์ ์ด์ฉํด์ ํ์๋ค.
์๊ฐ ๋ณต์ก๋๋ฅผ ๋ฎ์ถ๊ธฐ ์ํด ํ์ ์ด์ฉํด ๊ตฌํํ๋ค.
์๊ฐ๋ณต์ก๋ : ๋ ธ๋๊ฐ V,๊ฐ์ ์ด E๊ฐ ์ผ ๋, O(E*logV)
๋ฐฑ์ง ์ํ์์ ๊ตฌํํ๋ ค๋ ์๊ฐ์ด ๋์ง ์์์, ์ ๋ฒ์ ๊ตฌํํด๋๋ ์ฝ๋๋ฅผ ์ถฉ๋ถํ ์์งํ ํ์ ๊ตฌํํด๋ณด์๋ค.
์ฝ๋
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
#define INF 987654321
vector<pair<int, int>> adjs[1001];
int min_dist[1001];
void Dijkstra(int start) {
priority_queue<pair<int, int>> pq;
pq.push(make_pair(0, start));
min_dist[start] = 0; //////
while (!pq.empty()) {
int cost = -pq.top().first;
int cur = pq.top().second;
pq.pop();/////
if (min_dist[cur] < cost) continue;
for (int i = 0;i < adjs[cur].size();i++) {
int there = adjs[cur][i].first;
int new_cost = cost + adjs[cur][i].second;
if (min_dist[there] > new_cost) {
min_dist[there] = new_cost;
pq.push(make_pair(-new_cost, there));
}
}
}
}
int main(void) {
// ์
์ถ๋ ฅ ์๊ฐ ๋จ์ถ์ฉ
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, m, a, b;
cin >> n >> m;
for (int i = 0;i < m;i++) {
int start, end, cost;
cin >> start >> end >> cost;
adjs[start].push_back(make_pair(end, cost));
}
cin >> a >> b;
fill(&min_dist[0], &min_dist[1002], INF);
Dijkstra(a);
cout << min_dist[b] << endl;
return 0;
}
๊ฒฐ๊ณผ
๋ฐ์ํ
'Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค/C++] 1238. ํํฐ (0) | 2021.04.28 |
---|---|
[๋ฐฑ์ค/C++] 1915. ๊ฐ์ฅ ํฐ ์ ์ฌ๊ฐํ (0) | 2021.04.14 |
[๋ฐฑ์ค/C++] 1937. ์์ฌ์์ด ํ๋ค (0) | 2021.04.06 |
[๋ฐฑ์ค/C++] 1753. ์ต๋จ๊ฒฝ๋ก (0) | 2021.04.06 |
[ํ๋ก๊ทธ๋๋จธ์ค/C++] ๋๋์ง (0) | 2021.03.31 |