-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDIJKSTAR.cpp
More file actions
92 lines (78 loc) · 1.68 KB
/
DIJKSTAR.cpp
File metadata and controls
92 lines (78 loc) · 1.68 KB
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
//https://www.acmicpc.net/problem/1753
#include<queue>
#include<iostream>
#include<stack>
#include<algorithm>
#include<utility>
using namespace std;
queue<int> Q;
const int MAX = 20002;
constexpr int INF = 7000000;
std::vector<std::vector<std::pair<int,int>>> Graph;
std::priority_queue<std::pair<int,int>, std::vector<std::pair<int, int>>,std::greater<std::pair<int, int>> > PQ; //정점의 거리, 정점
int Distance[MAX];
void INITIALIZE_SINGLE_SOURCE(int s) //start std::vector<std::vector<int>> Graph ,int s
{
for (int i = 1; i < Graph.size() ; i++)
{
Distance[i] = INF;
}
Distance[s] = 0;
}
void DUKSTRA( int s)
{
INITIALIZE_SINGLE_SOURCE(s);
for (int i = 1; i < Graph.size(); i++)
{
PQ.push(std::make_pair(Distance[i], i));
}
while (!PQ.empty())
{
int u = PQ.top().second;
//S = S+u
PQ.pop();
for (int v = 0; v < Graph[u].size(); v++)
{
RELAX(u, Graph[u][v].first, Graph[u][v].second);
}
}
}
void RELAX(int u, int v, int w)
{
if (Distance[v] > Distance[u] + w)
{
Distance[v] = Distance[u] + w;
//predecessor_subgraph[v] = u;
PQ.push(std::make_pair(Distance[v], v));
}
}
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
//std::ifstream in("in3.txt"); //std::cin
int V, E, start;
//in >> V >> E ;
//in >> start;
std::cin >> V >> E;
std::cin >> start;
Graph.resize(V + 1);
for (int i = 0,_v,_u,_w; i < E; i++)
{
//in >> _v >> _u >> _w;
std::cin>> _v >> _u >> _w;
Graph[_v].push_back(std::make_pair(_u, _w)); //간선 ,, 인접한정점 순으로 넣는다.
}
DUKSTRA(start);
for (int i = 1; i <= V; i++)
{
if (Distance[i] == INF)
{
std::cout << "INF" << '\n';
}
else
{
std::cout << Distance[i] << '\n';
}
}
}