-
Notifications
You must be signed in to change notification settings - Fork 0
/
dijkstra.cpp
181 lines (170 loc) · 10.3 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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
** Vishal Raj Roy
** Indian Institute of Technology Kharagpur
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define posLSB(X) __builtin_ctz(X)
#define num1bit(X) __builtin_popcount(X)
#define numlead0(X) __builtin_clz(X)
#define gcd(X,Y) __gcd(X,Y)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define ifpresent(CON,VAL) (CON.find(VAL) != CON.end())
#define all(x) x.begin(),x.end()
#define at(X,N) get<N>(X)
#define ins insert
#define xx first
#define yy second
#define sz(x) ((int)x.size())
#define count_ones __builtin_popcountl
#define nl cout<<"\n";
#define name(X) (#X)
#define watch(X) cout << (#X) << " is " << (X) << "\n"
#define sqr(x) ((x)*(x))
#define mod(x, m) ((((x) % (m)) + (m)) % (m))
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define mem(x,val) memset((x),(val),sizeof(x))
#define rite(X) freopen(X,"w",stdout)
#define read(X) freopen(X,"r",stdin)
//FOR(i,1,10) gives 1,2,3,....,9 and FOR(i,10,1) gives 9,8,....,1 also FOR(it, end(v), begin(v)) and FOR(it, begin(v), end(v))
#define FOR(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define SEL(X,C) for( auto & X : C )
#define ima INT_MAX
#define imi INT_MIN
#define lma LLONG_MAX
#define lmi LLONG_MIN
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {} template<typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cout << *it << " = " << a << "\n"; err(++it, args...);}
template <class T> struct custom_hash { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } T operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM);} T operator()(tuple<uint64_t,uint64_t> x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(get<0>(x) + FIXED_RANDOM)^(splitmix64(get<1>(x) + FIXED_RANDOM) >> 1); } }; // T operator()(pair<uint64_t,uint64_t> x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x.first + FIXED_RANDOM)^(splitmix64(x.second + FIXED_RANDOM) >> 1); } };
template <class L, class R> ostream & operator<<(ostream &os, pair<L,R> P) { return os << "(" << P.xx << "," << P.yy << ")"; } template<class L, class R> ostream &operator<<(ostream &os, tuple<L,R> P) {return os << "(" << get<0>(P) << "," << get<1>(P) <<")"; } template<class L, class R, class S> ostream &operator<<(ostream &os, tuple<L,R,S> P) { return os << "(" << get<0>(P) << "," << get<1>(P) << ","<< get<2>(P) <<")"; } template<class T, class A> ostream &operator<<(ostream& os, vector<T,A> V) {os /*<< name(V)<< " is "*/ << "\n"; FOR(i,0,sz(V)) os <<" "<< name(V) << "["<<i<<"] = "<< V[i] << "\n"; return os; /*<<"\n";*/ } template<class T, class H, class P, class A> ostream &operator<<(ostream& os, unordered_set<T,H,P,A> S) { os /*name(S) << " is"*/<<" ["; int i = 0; for(auto ELE : S) {os << ELE; if(i < sz(S)-1 ) os<<", "; i++; } return os << "]"; /*<<"\n"; */ } template<class S, class T, class H, class P, class A> ostream &operator<<(ostream& os, unordered_map<S,T,H,P,A> M) { os /*name(S) << " is"*/<<"\n"; for( auto ELE : M ) os <<" "<< name(M) << "["<<ELE.xx<<"] = "<< ELE.yy << "\n"; return os; /*<<"\n";*/ } template<class T, class C, class A> ostream &operator<<(ostream& os, multiset<T,C,A> S) { os /*name(S) << " is"*/<<"["; int i = 0; for(auto ELE : S) {os << ELE; if(i < sz(S)-1 ) os<<", "; i++; } return os << "]"; /*<<"\n"; */ } template<class T, class R> ostream &operator<<(ostream& os, gp_hash_table<T,null_type,custom_hash<R>> S) { os /*name(S) << " is"*/<<" ["; int i = 0; for(auto ELE : S) {os << ELE; if(i < sz(S)-1 ) os<<", "; i++; } return os << "]"; /*<<"\n"; */ } template<class T, class Z, class R> ostream &operator<<(ostream& os, gp_hash_table<T,Z,custom_hash<R>> M) { os /*name(S) << " is"*/<<"\n"; for( auto ELE : M ) os <<" "<< name(M) << "["<<ELE.xx<<"] = "<< ELE.yy << "\n"; return os; /*<<"\n";*/ } template<class T, class R> ostream &operator<<(ostream& os, cc_hash_table<T,null_type,custom_hash<R>> S) { os /*name(S) << " is"*/<<" ["; int i = 0; for(auto ELE : S) {os << ELE; if(i < sz(S)-1 ) os<<", "; i++; } return os << "]"; /*<<"\n"; */ } template<class T, class Z, class R> ostream &operator<<(ostream& os, cc_hash_table<T,Z,custom_hash<R>> M) { os /*name(S) << " is"*/<<"\n"; for( auto ELE : M ) os <<" "<< name(M) << "["<<ELE.xx<<"] = "<< ELE.yy << "\n"; return os; /*<<"\n";*/ }
using st = string; typedef long long int li; typedef pair<int, int> ii; typedef pair<int, ii> iii; typedef tuple<int,int> i2; typedef tuple<int,int,int> i3; typedef vector<int> vi; typedef vector<ii> vii; typedef vector<i2> vi2; typedef vector<i3> vi3; typedef vector<iii> viii; typedef vector<vi> vvi; typedef vector<vvi> vvvi; typedef pair<li, li> ll; typedef pair<li, ll> lll; typedef tuple<li,li> l2; typedef tuple<li,li,li> l3; typedef vector<li> vl; typedef vector<ll> vll; typedef vector<l2> vl2; typedef vector<l3> vl3; typedef vector<lll> vlll; typedef vector<vl> vvl; typedef vector<vvl> vvvl; typedef double db; typedef complex<db> cd; typedef vector<cd> vcd; template <class S> using ve = vector<S>; template <class S> using gr = greater<S>; template <class S> using le = less<S>; template <class S, class T = le<S>> using ms = multiset<S,T>; template <class S,class T = null_type, class R = size_t> using gp = gp_hash_table<S,T,custom_hash<R>>; template <class S,class T = null_type, class R = size_t> using cc = cc_hash_table<S,T,custom_hash<R>>; template <class S,class T, class R = size_t> using um = unordered_map<S,T,custom_hash<R>>; template <class S, class R = size_t> using us = unordered_set<S,custom_hash<R>>;
/*
Welcome to VISHAL RAJ ROY's template library :) the following are here to serve you>>
v_ = vector of '_' | li = long long int | For fast insertion/deletion ->
vl = vector of long | db = double | gp<T,S,R>
vi = vector of int | st = string | For fast read/write -> cc<T,S,R>
supported: | ii = pair<int,int> | For slow everything ->
1) vi, vvi, vvvi 4) vl, vvl, vvvl | iii = pair<int,ii> | um<T,S,R> .. R = ( size_t or uint64_t )
2) vii, viii 5) vll, vlll | i2 = tuple<int,int> | and us<T,R> .. R = ( size_t or uint64_t )
3) vi2, vi3 6) vl2, vl3 | cdb = complex double | supported :
7) vcd | i3 = tuple<int,int,int> | int/li/pair -> int/li/string/pair...pair<int/li,int/li>
watch(X) is the WATCH DOG, it can print anything and everything, COULD'NT believe? just try it>>
error(a,b,c,......) takes many parameters and shows you their true face>>
*/
vi yy;
const double pi = 2 * acos(0.0);
const int oo = 0x3f3f3f3f; // don't use for long long
const double inf = 1.0/0.0;
// o(n^2+m) implementation for dense graphs ( n^2 ~ m )
void dijkstra_for_dense_graphs( int s, ve<vii> & alist, vi & p, vi & d ) {
int n = sz(alist); p.assign(n,-1); d.assign(n,oo);
queue <int> vis;
FOR(i,0,n)
vis.push(i);
d[s] = 0; int sd = n-1;
FOR(i,0,n) {
int v = vis.front(); vis.pop();
FOR(j,0,sd) {
int curr = vis.front(); vis.pop();
if( d[curr] < d[v] )
swap(v,curr);
vis.push(curr);
} sd--;
if( d[v] >= oo )
break;
SEL(ed,alist[v]) {
int u = ed.xx, dis = ed.yy;
if( d[u] > d[v]+dis )
d[u] = d[v]+dis, p[u] = v;
}
}
}
// o(nlog(m)+mlog(n))
void dijkstra_for_sparse_graphs_w_priority_queue( int s, ve<vii> & alist, vi & d, vi & p ) {
int n = sz(alist);
d.assign(n,oo);
p.assign(n,-1);
d[s] = 0;
priority_queue<ii, vii, gr<ii>> pq;
pq.push({0,s});
while( !pq.empty() ) {
ii top = pq.top(); pq.pop();
int u = top.yy, dis = top.xx;
if( dis != d[u] )
continue;
SEL(ed,alist[u]) {
int v = ed.xx, dist = ed.yy;
if( d[v] > d[u]+dist )
d[v] = d[u]+dist, p[v] = u, pq.push({d[v],v});
}
}
}
// o(nlog(m)+mlog(n))
// Has better performance than dijkstra_for_sparse_graphs_w_priority_queue
struct comp {
bool operator() ( int a, int b ) const {
return (d[a] < d[b] || (d[a] == d[b] && a < b) );
}
};
typedef tree<
int,
null_type,
comp,
rb_tree_tag>
ordered_set; // set from pbds can also be used
vi d;
void dijkstra_for_sparse_graph_w_set_no_pair( int s, ve<vii> & alist, vi & p ) {
int n = sz(alist); p.assign(n,-1); d.assign(n,oo);
set<int,comp> ver; d[s] = 0;
ver.insert(s);
while( !ver.empty() ) {
int v = *ver.begin();
ver.erase(ver.begin());
SEL(ed,alist[v]) {
int u = ed.xx, dis = ed.yy;
if( d[u] > d[v]+dis )
ver.erase(u), d[u] = d[v]+dis, p[u] = v, ver.insert(u);
}
}
}
// set using pair<int,int>
typedef tree<
int,
null_type,
less<ii>,
rb_tree_tag>
ordered_set; // set from pbds can also be used
void dijkstra_for_sparse_graph_w_set_w_pair(int s, ve<vii> &alist, vi &p)
{
int n = sz(alist);
p.assign(n, -1);
d.assign(n, oo);
set<ii> ver; // ordered_set ver;
d[s] = 0;
ver.insert({0,s});
while (!ver.empty())
{
int v = ver.begin()->second;
ver.erase(ver.begin());
SEL(ed, alist[v])
{
int u = ed.xx, dis = ed.yy;
if (d[u] > d[v] + dis)
ver.erase({d[u],u}), d[u] = d[v] + dis, p[u] = v, ver.insert({d[u],u});
}
}
}
int main() {
ios_base::sync_with_stdio(0);
return 0;
}