-
Notifications
You must be signed in to change notification settings - Fork 0
/
TRVCOST.cpp
58 lines (57 loc) · 1.03 KB
/
TRVCOST.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
/*
PROBLEM : TRVCOST
user: kart123
Algo : Djisktra shortest-path
*/
#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define INF INT_MAX
int dist[5050];
bool vis[5050];
using namespace std;
int main()
{
int n,p,q,cost,start,end;
cin >> n;
vector < pii > v[505];
for(int i = 0;i<n;i++)
{
scanf("%d%d%d",&p,&q,&cost);
v[p].pb(mp(cost,q));
v[q].pb(mp(cost,p));
}
scanf("%d",&start);
priority_queue <pii ,vector<pii >,greater <pii > > Q;
for(int i = 0;i<1050;i++)
vis[i] = false,dist[i] = INF;
dist[start] = 0;
Q.push(mp(0,start));
while(!Q.empty())
{
int d = Q.top().first;
int ui = Q.top().second;
Q.pop();
for(int i=0;i<v[ui].size();i++)
{
int vi = v[ui][i].second;
int w = v[ui][i].first;
if(!vis[vi] && w + dist[ui] < dist[vi]){
dist[vi] = w + dist[ui];
Q.push(mp(dist[vi],vi));
}
}
vis[ui] = true;
}
scanf("%d",&q);
while(q--)
{
scanf("%d",&end);
if(dist[end] != INF)
printf("%d\n",dist[end]);
else
printf("NO PATH\n");
}
return 0;
}