-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshortestpath.cpp
More file actions
executable file
·72 lines (56 loc) · 967 Bytes
/
shortestpath.cpp
File metadata and controls
executable file
·72 lines (56 loc) · 967 Bytes
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
//Dijkstra's using min_prioriy_queue
//O(V+ElogV)
#include <bits/stdc++.h>
#define f first
#define s second
#define ll long long int
#define SIZE 1000000
using namespace std;
vector < pair < ll,ll> > v[SIZE];
bool vis[SIZE];
ll dist[SIZE];
void djk()
{
memset(vis, false, sizeof(vis));
dist[1]=0;
multiset < pair <ll,ll> > s;
s.insert({0,1});
while(!s.empty())
{
pair< ll, ll> p = *(s.begin());
s.erase(s.begin());
ll u = p.second;
if(vis[u])continue;
vis[u]=true;
for(ll i=0; i<(ll)v[u].size(); i++)
{
ll e = v[u][i].first;
ll w = v[u][i].second;
if( dist[e]>(dist[u]+w) )
{
dist[e]=(dist[u]+w);
s.insert({dist[e], e});
}
}
}
}
int main(void)
{
ll n, m;
ll x, y, w;
cin>>n>>m;
for(ll i=0; i<m; i++)
{
cin>>x>>y>>w;
pair<ll, ll> p;
p = {y, w};
v[x].push_back(p);
}
for(ll j=2; j<=n; j++)
dist[j]=INT_MAX;
djk();
//for(ll i=2;i<=n; i++)
cout<<dist[n];
cout<<"\n";
return 0;
}