-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDijkstra Algorithm.cpp
88 lines (86 loc) · 1.93 KB
/
Dijkstra Algorithm.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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define re(i, n) for(int i = 0; i < n; i++)
#define rep(i,a,b) for(int i = (a); i <= (b); i++)
#define ren(i,a,b) for(int i = (a); i >= (b); i--)
#define si(x) scanf("%d",&x)
#define sl(x) scanf("%lld",&x)
#define ss(s) scanf("%s",s)
#define pi(x) printf("%d\n",x)
#define pl(x) printf("%lld\n",x)
#define ps(s) printf("%s\n",s)
class graph
{
public:
int V;
list < pair<int , int> > *l;
graph(int v)
{
V = v;
l = new list<pair<int , int> > [V];
}
void add_edge(int u , int v , int w)
{
l[u].push_back(make_pair(v , w));
l[v].push_back(make_pair(u , w));
}
int Disk(int src)
{
std::vector<int> dist(V , INT_MAX);
set<pair<int , int> > s;
s.insert(make_pair(0 , src));
dist[src] = 0;
set<pair<int , int> >::iterator it;
int temp;
while(!s.empty())
{
auto t = *(s.begin());
temp = t.second;
s.erase(s.begin());
for(auto i:l[temp])
{
if((dist[i.first] > dist[temp] + i.second))
{
it = s.find(make_pair(dist[i.first] , i.first));
dist[i.first] = dist[temp] + i.second;
if(it != s.end())
s.erase(it);
s.insert(make_pair(dist[i.first] , i.first));
}
}
}
for(int i :dist)
{
if(i != src){
if(i != INT_MAX)
cout << i << " ";
else
cout << -1 <<" ";
}
}
cout << "\n";
}
};
int main()
{
int t;
si(t);
int n , m ,a ,b ,w , s;
while(t--)
{
si(n); si(m);
graph* g = new graph(n);
re(i , m)
{
si(a); si(b); si(w);
g->add_edge(a-1 ,b-1, w);
}
si(s);
g->Disk(s-1);
delete(g);
}
return 0;
}