-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path22_dijkstraAlgorithm2.cpp
132 lines (108 loc) ยท 3.24 KB
/
22_dijkstraAlgorithm2.cpp
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int number = 6;
int INF = 1000000000;
vector<pair<int, int>> a[7]; // ๊ฐ์ ์ ๋ณด์
๋๋ค.
int d[7]; // ์ต์ ๋น์ฉ์
๋๋ค.
void dijkstra(int start) {
d[start] = 0;
priority_queue<pair<int, int>> pq; // ํ ๊ตฌ์กฐ ์
๋๋ค.
pq.push(make_pair(0, start)); // ๋ณธ์ธ์ 0
// ๊ฐ๊น์ด ์์๋๋ก ์ฒ๋ฆฌํ๋ฏ๋ก ํ๋ฅผ ์ฌ์ฉํฉ๋๋ค.
while (!pq.empty()) {
int current = pq.top().second;
// ์งง์ ๊ฒ์ด ๋จผ์ ์ค๋๋ก ์์ํ(-)ํฉ๋๋ค.
int distance = -pq.top().first;
pq.pop();
// ์ต๋จ ๊ฑฐ๋ฆฌ๊ฐ ์๋๊ฒฝ์ฐ ์คํตํฉ๋๋ค.
if (d[current] < distance) continue;
for (int i = 0; i < a[current].size(); i++) {
// ์ ํ๋ ๋
ธ๋์ ์ธ์ ๋
ธ๋
int next = a[current][i].second;
// ์ ํ๋ ๋
ธ๋๋ฅผ ์ธ์ ๋
ธ๋๋ก ๊ฑฐ์ณ์ ๊ฐ๋ ๋น์ฉ
int nextDistance = distance + a[current][i].first;
// ๊ธฐ์กด์ ์ต์ ๋น์ฉ๋ณด๋ค ๋ ์ ๋ ดํ๋ค๋ฉด ๊ต์ฒดํฉ๋๋ค.
if (nextDistance < d[next]) {
d[next] = nextDistance;
pq.push(make_pair(-nextDistance, next));
}
}
}
}
int main(void) {
// ๊ธฐ๋ณธ์ ์ผ๋ก ์ฐ๊ฒฐ๋์ง ์์ ๊ฒฝ์ฐ ๋น์ฉ์ ๋ฌดํ์
๋๋ค.
for (int i = 1; i <= number; i++) {
d[i] = INF;
}
a[1].push_back(make_pair(2, 2));
a[1].push_back(make_pair(5, 3));
a[1].push_back(make_pair(1, 4));
a[2].push_back(make_pair(2, 1));
a[2].push_back(make_pair(3, 3));
a[2].push_back(make_pair(2, 4));
a[3].push_back(make_pair(5, 1));
a[3].push_back(make_pair(3, 2));
a[3].push_back(make_pair(3, 4));
a[3].push_back(make_pair(1, 5));
a[3].push_back(make_pair(5, 6));
a[4].push_back(make_pair(1, 1));
a[4].push_back(make_pair(2, 2));
a[4].push_back(make_pair(3, 3));
a[4].push_back(make_pair(1, 5));
a[5].push_back(make_pair(1, 3));
a[5].push_back(make_pair(1, 4));
a[5].push_back(make_pair(2, 6));
a[6].push_back(make_pair(5, 3));
a[6].push_back(make_pair(2, 5));
dijkstra(1);
// ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํฉ๋๋ค.
for (int i = 1; i <= number; i++) {
printf("%d ", d[i]);
}
}
// ------------------------ ๋ค๋ฅธ ๋ฒ์ -------------------------------
// ์ธ์ด : C++ , (์ฑ๊ณต/์คํจ) : 1/0 , ๋ฉ๋ชจ๋ฆฌ : 2024 KB , ์๊ฐ : 0ms
#include <iostream>
#include <vector>
#include <algorithm>
#include <string.h> // memset
#include <queue>
using namespace std;
#define X first
#define Y second
int v,e,st;
// {๋น์ฉ, ์ ์ ๋ฒํธ}
vector<pair<int,int>> adj[20005];
const int INF = 1e9+10;
int d[20005];
int main(void){
// ์
์ถ๋ ฅ ์๋ ์ต์ ํ
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin>>v>>e>>st;
fill(d,d+v+1,INF);
while(e--){
int u,v,w;
cin>>u>>v>>w;
adj[u].push_back({w,v});
}
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;
d[st] = 0;
pq.push({d[st],st});
while(!pq.empty()){
auto cur = pq.top(); pq.pop();
if(d[cur.Y] != cur.X) continue; // ๊ฑฐ๋ฆฌ๊ฐ ๋ค๋ฅด๋ฉด ์์๋ฅผ ๋ฒ๋ฆฐ๋ค.
for(auto nxt: adj[cur.Y]){
if(d[nxt.Y] <= d[cur.Y]+nxt.X) continue;
d[nxt.Y] = d[cur.Y] + nxt.X;
pq.push({d[nxt.Y],nxt.Y});
}
}
for(int i=1;i<=v;i++){
if(d[i] == INF) cout << "INF\n";
else cout<<d[i]<<"\n";
}
return 0;
}