Notice
Recent Posts
Recent Comments
Link
Tags
- ํ๋ฆฌ์จ๋ณด๋ฉ
- LCs
- go
- ํ๋ก๊ทธ๋๋จธ์ค
- Union-Find
- ์์ฝ๋
- ๋นํธ๋งต
- ์น๋ฆฐ์ด
- ์นด์นด์ค ์ฝํ
- DP
- ๋ค์ต์คํธ๋ผ
- js
- ์นด์นด์ค2021
- ์ํฐ๋
- DFS
- ์๊ณ ๋ฆฌ์ฆ
- BFS
- ์ฌ๊ท
- C++
- ์ด๋ถํ์
- ๋ฐฑ์๋ ํ๋ฆฌ์จ๋ณด๋ฉ
- Python
- nestjs
- ํธ๋ฆฌ
- ๋ฐฑ์ค
- ๋์ ํ๋ก๊ทธ๋๋ฐ
- golang
- ๋นํธ๋ง์คํน
- ์ฌ๋ผ์ด๋ฉ ์๋์ฐ
- ๊ฐ์ฅ๊ฐ๊น์ด๊ณตํต์กฐ์
Archives
- Today
- Total
Hello Ocean! ๐ผ
[๋ฐฑ์ค/C++] 1753. ์ต๋จ๊ฒฝ๋ก ๋ณธ๋ฌธ
๋ฐ์ํ
๋ฌธ์
์์์ ์ ๋ถํฐ, ๋ค๋ฅธ ๋ชจ๋ ์ ์ ๊น์ง์ ์ต๋จ๊ฒฝ๋ก๋ฅผ ๊ตฌํ๋ ๋จ์ํ(?) ๋ฌธ์ ์ด๋ค.
ํ์ด
๋ค์ต์คํธ๋ผ ์๊ณ ๋ฆฌ์ฆ์ ์ด์ฉํ์๋ค.
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
#define INF 987654321
int min_route[20002];
int V, E, start;
vector<pair<int, int>> adj[20002];
void DijkstraHeap() {
priority_queue<pair<int, int>> pq;
pq.push(make_pair(0, start));
min_route[start] = 0;
while (!pq.empty()) {
int dist = -pq.top().first;
int cur = pq.top().second;
pq.pop();
if (min_route[cur] < dist) continue; // ์ด๋ฏธ ์ต๋จ๊ฒฝ๋ก๋ฅผ ์ฒดํฌํ ๋
ธ๋์ธ ๊ฒฝ์ฐ ํจ์ค
for (int i = 0;i < adj[cur].size();i++) {
int there = adj[cur][i].first;
int cost = dist + adj[cur][i].second;
if (cost < min_route[there]) {
min_route[there] = cost;
pq.push(make_pair(-cost, there));
}
}
}
}
int main(void) {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> V >> E;
cin >> start;
for (int i = 0;i < E;i++) {
int a, b, cost;
cin >> a >> b >> cost;
adj[a].push_back(make_pair(b, cost));
}
for (int i = 0;i <= V;i++) {
min_route[i] = INF;
}
DijkstraHeap();
for (int i = 1;i <= V;i++) {
if (min_route[i] == INF)
cout << "INF" << "\n";
else
cout << min_route[i] << "\n";
}
return 0;
}
๊ฒฐ๊ณผ
* endl -> "\n" ์ผ๋ก ๋ณ๊ฒฝํ๋๋ 800ms๊ฐ ์ค์ด๋ค์๋ค
* ์ ์ถ๋ ฅ์์ ๋ฉ์ธํจ์์ ์๋ ์ฝ๋๋ฅผ ๋ฃ์ง ์์ผ๋ฉด ์๊ฐ์ด๊ณผ๊ฐ ๋๋ค
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
๋ฐ์ํ
'Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค/C++] 1916. ์ต์๋น์ฉ ๊ตฌํ๊ธฐ (0) | 2021.04.14 |
---|---|
[๋ฐฑ์ค/C++] 1937. ์์ฌ์์ด ํ๋ค (0) | 2021.04.06 |
[ํ๋ก๊ทธ๋๋จธ์ค/C++] ๋๋์ง (0) | 2021.03.31 |
[๋ฐฑ์ค/C++] 1005. ACM Craft (0) | 2021.03.24 |
[๋ฐฑ์ค/C++] 2225. ํฉ๋ถํด (0) | 2021.03.24 |