-
Notifications
You must be signed in to change notification settings - Fork 186
/
dist_from_source.cpp
62 lines (56 loc) · 1.4 KB
/
dist_from_source.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
//https://www.codechef.com/problems/BUG2K17B
#include<bits/stdc++.h>
#define mp pair<int,int>
using namespace std;
int debugg=0;
class Node{
public:
int dist;
vector<pair<int,int> > next;//first contains city index .. second contains distance
void clear(){
dist=INT_MAX;
next.clear();
}
} node[100002];
struct priority_fxn{
bool operator() (int a,int b){
return node[a].dist > node[b].dist;
}
};
void dijakstra(int source){
int n,d,temp;
priority_queue<int,vector<int>,priority_fxn> q;
//init queue
node[source].dist=0;
q.push(source);
while(!q.empty()){
temp=q.top();
for(auto elem : node[temp].next){
n=elem.first;
d=elem.second;
if(node[n].dist > node[temp].dist + d){
node[n].dist = node[temp].dist + d;
q.push(n);
}
}
q.pop();
}
}
int main(){
int t,a,b,c,n,p;
cin>>t;
while(t--){
cin>>n>>p;
for(int i=1;i<=n;i++)
node[i].clear();
while(p--){
cin>>a>>b>>c;
node[a].next.push_back(mp(b,c));
node[b].next.push_back(mp(a,c));
}
dijakstra(1);
if(node[n].dist==INT_MAX)cout<<"NONE"<<endl;
else
cout<<node[n].dist<<endl;
}
}