-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdjikstra.sublime-snippet
81 lines (72 loc) · 1.36 KB
/
djikstra.sublime-snippet
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
<snippet>
<content><![CDATA[
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define vll vector<ll>
#define F first
#define S second
#define pll pair<ll,ll>
#define FOR1(i,a) for(i=0;i<=a;i++)
#define FOR2(i,a,b) for(i=a;i<=b;i++)
#define endl '\n'
#define clr(a) memset(a,0,sizeof(a))
#define all(x) x.begin(),x.end()
typedef long long ll;
ll n,dist[10000];
map<pair<ll,ll>,ll>l;
struct comp
{
bool operator()(ll a,ll b)
{
return dist[a]>dist[b];
}
};
void djikstra(vector<vll >graph,ll s)
{
ll i,prev[n];
priority_queue<ll,vector<ll>,comp>Q;
FOR1(i,n-1)
{
if(i==s)
{dist[i]=0;prev[i]=-1;}
else
{dist[i]=10000;prev[i]=-1;}
Q.push(i);
}
while(!Q.empty())
{
ll u=Q.top();Q.pop();
for(auto&v:graph[u])
{
ll alt=dist[u]+l[mp(u,v)];
if(alt<dist[v])
dist[v]=alt,prev[v]=u;
}
}
FOR1(i,n-1)
{
cout<<endl<<"dist to "<<i<<":"<<dist[i];
}
}
int main()
{
ll i,e;cin>>n>>e;
vector<vll >graph(n);
FOR1(i,e-1)
{
ll u,v,w;cin>>u>>v>>w;
graph[u].pb(v);
l[mp(u,v)]=w;// use unordered map for efficiency
}
ll s;cin>>s;
djikstra(graph,s);
return 0;
}
]]></content>
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<tabTrigger>djikstra</tabTrigger>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<!-- <scope>source.python</scope> -->
</snippet>