-
Notifications
You must be signed in to change notification settings - Fork 0
/
djikstra's.cpp
47 lines (40 loc) · 937 Bytes
/
djikstra's.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
#include <iostream>
#include <set>
#include <vector>
using namespace std;
typedef vector<int> vi;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<vii> vvii;
const int MAX = 1001;
const int MAXINT = 1000000000;
int n;
vvii G(MAX);
vi D(MAX, MAXINT);
void Dijkstra(int s)
{
set<ii> Q;
D[s] = 0;
Q.insert(ii(0,s));
while(!Q.empty())
{
ii top = *Q.begin();
Q.erase(Q.begin());
int v = top.second;
int d = top.first;
for (vii::const_iterator it = G[v].begin(); it != G[v].end(); it++)
{
int v2 = it->first;
int cost = it->second;
if (D[v2] > D[v] + cost)
{
if (D[v2] != 1000000000)
{
Q.erase(Q.find(ii(D[v2], v2)));
}
D[v2] = D[v] + cost;
Q.insert(ii(D[v2], v2));
}
}
}
}