-
Notifications
You must be signed in to change notification settings - Fork 0
/
shpath.cpp
70 lines (67 loc) · 1.56 KB
/
shpath.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
63
64
65
66
67
68
69
70
#include <iostream>
#include <cstring> //memset
#include <cstdio>
#include <algorithm>
#include <bitset>
#include <string>
#include <vector>
#include <utility>
#include <cstdio>
#include <queue>
#include <cmath>
#include <list>
#include <set>
#include <unordered_map>
using namespace std;
typedef pair <int, int> pii;
vector < pii > V[10000];
int dist[10000];
void djikstra(int src, int sz, int dest) {
for (int i = 0; i < sz; i++)
dist[i] = 1e9;
dist[src] = 0;
priority_queue <pii, vector<pii>, greater<pii> > Q;
Q.push(make_pair(0, src));
while (!Q.empty()) {
pii curr = Q.top();
Q.pop();
if (curr.second == dest) {
printf("%d\n", dist[dest]);
return;
}
for (int i = 0; i < V[curr.second].size(); i++) {
pii temp = V[curr.second][i];
if (dist[curr.second] + temp.first < dist[temp.second]) {
dist[temp.second] = dist[curr.second] + temp.first;
Q.push(make_pair(dist[temp.second], temp.second));
}
}
}
}
int main() {
freopen("input.txt", "r", stdin);
char name[20], src[20], dest[20];
unordered_map <string, int> M;
int test_cases, nodes, neighbours, queries, dst, wt;
scanf("%d", &test_cases);
while (test_cases--) {
scanf("%d", &nodes);
for (int i = 0; i < nodes; i++)
V[i].clear();
for (int i = 0; i < nodes; i++) {
scanf("%s", name);
M[name] = i;
scanf("%d", &neighbours);
for (int j = 0; j < neighbours; j++) {
scanf("%d %d", &dst, &wt);
V[i].push_back(make_pair(wt, dst-1));
}
}
cin >> queries;
while (queries--) {
scanf("%s %s", src, dest);
djikstra(M[src], nodes, M[dest]);
}
}
return 0;
}