-
Notifications
You must be signed in to change notification settings - Fork 698
/
Dijkstra.cpp
97 lines (87 loc) · 2.33 KB
/
Dijkstra.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
Dijkstra with Path:
int arrival[N], departure[N], vis[N], parent[N];
vector<pair<int, int> > g[N];
void dijkstra(int source, int destination)
{
for(int i=1;i<=n;i++)
{
arrival[i]=1e18;
departure[i]=1e18;
vis[i]=0;
}
arrival[source]=0;
set<pair<int, int> > s;
s.insert({0, source});
while(!s.empty())
{
auto x = *(s.begin());
s.erase(x);
vis[x.second]=1;
departure[x.second]=arrival[x.second];
for(auto it:g[x.second])
{
if(arrival[it.first] > departure[x.second] + it.second)
{
s.erase({arrival[it.first], it.first});
arrival[it.first]=departure[x.second] + it.second;
s.insert({arrival[it.first], it.first});
parent[it.first]=x.second;
}
}
}
if(!vis[destination])
{
cout<<"-1";
return;
}
int v=destination;
vector<int> ans;
while(parent[v])
{
ans.push_back(v);
v=parent[v];
}
ans.push_back(source);
reverse(ans.begin(), ans.end());
for(auto it:ans)
cout<<it<<" ";
}
-------------------------------------------------------------------------------------------------------------------------------------
Simple Dijkstra:
int arrival[N][N], departure[N][N];
vector<pair<int, int> > g[N];
void dijkstra(int source, int destination, int arrival[], int departure[])
{
for(int i=1;i<=n;i++)
{
arrival[i]=inf;
departure[i]=inf;
}
arrival[source]=0;
set<pair<int, int> > s;
s.insert({0, source});
while(!s.empty())
{
auto x = *(s.begin());
s.erase(x);
departure[x.second]=arrival[x.second];
for(auto it:g[x.second])
{
if(arrival[it.first] > departure[x.second] + it.second)
{
s.erase({arrival[it.first], it.first});
arrival[it.first]=departure[x.second] + it.second;
s.insert({arrival[it.first], it.first});
}
}
}
}
//Problem 1 (Direct Dijkstra): https://codeforces.com/contest/20/problem/C
//Solution 1: http://codeforces.com/contest/20/submission/39892416
//Problem 2: http://codeforces.com/contest/230/problem/D
//Solution 2: http://codeforces.com/contest/230/submission/39892295
//Problem 3 (count point on edges): https://codeforces.com/problemset/problem/144/D
//Solution 3: https://codeforces.com/contest/144/submission/45963496
//Problem 4 (all source Dijkstra): https://codeforces.com/contest/96/problem/D
//Solution 4: https://codeforces.com/contest/96/submission/45959572
//Problem 5: https://p.ip.fi/woUJ (Lexicographically shortest path from U to V)