-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path75.cpp
More file actions
140 lines (122 loc) · 3.43 KB
/
75.cpp
File metadata and controls
140 lines (122 loc) · 3.43 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#pragma warning(disable:4996)
#include <iostream>
#include <cstdint>
#include <algorithm>
#include <functional>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <sstream>
#include <unordered_set>
#include <unordered_map>
using namespace std;
#define pw(a) ((a) * (a))
#define vit vector<int>::iterator
#define sit set<int>::iterator
#define dqit deque<int>::iterator
#define FILE_IO {freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);}
#define FAST_IO ios_base::sync_with_stdio(0); cin.tie(NULL), cout.tie(NULL)
#define sortRev greater<int>()
const double pi = 3.14159265359;
const long long mod = 1e9 + 7;
typedef uint64_t uit;
typedef long long ll;
typedef long long int lli;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<double, int> pdi;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> p2ll;
typedef pair<string, int> psi;
typedef pair<ld, ld> p2ld;
typedef pair<char, char> pcc;
typedef pair<ll, string> plls;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<string> vstr;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pdi> vpdi;
typedef vector<p2ll> vp2ll;
typedef vector<vector<int>> vvi;
typedef vector<vector<bool>> vvb;
typedef vector<vector<char>> vvc;
typedef vector<vector<long long>> vvll;
typedef vector<vector<pii>> vvpii;
typedef vector<vector<vector<int>>> vvvi;
typedef deque<int> dqi;
typedef queue<int> qi;
typedef queue<pii> qpii;
int GCD(int a, int b) { return !b ? a : GCD(b, a % b); }
inline double DIST(const pii& a, const pii& b) { return sqrt(pw((double)(a.first - b.first)) + pw((double)(a.second - b.second))); }
inline int DEC(char x) { return (int)(x - '0'); }
/*__________________________________________________________________________________________________________________________
https://www.spoj.com/problems/SHPATH/
____________________________________________________________________________________________________________________________*/
#define distance first
#define vertex second
const int INF = 999999999;
map<string, int> note;
vpii grp[10005];
int V;
struct option {
bool operator() (const pair<int, int>& a, const pair<int, int>& b) const {
return a.first > b.first;
}
};
inline int dijkstra(string cityA, string cityB) {
vi dist(V + 5, INF);
dist[note[cityA]] = 0;
priority_queue<pii, vpii, option> pq;
pq.push(pii(0, note[cityA]));
while (!pq.empty()) {
pii u = pq.top();
pq.pop();
if (u.distance != dist[u.vertex]) {
continue;
}
for (int i = 0; i < grp[u.vertex].size(); ++i) {
pii v = grp[u.vertex][i];
if (dist[v.vertex] > u.distance + v.distance) {
dist[v.vertex] = u.distance + v.distance;
pq.push(pii(dist[v.vertex], v.vertex));
}
}
}
return dist[note[cityB]];
}
int main() {
FAST_IO;
//FILE_IO;
int tc, q;
string cityA, cityB;
cin >> tc;
while (tc--) {
cin >> V;
for (int n, v, w, i = 1; i <= V; ++i) {
cin >> cityA >> n;
note[cityA] = i;
while (n--) {
cin >> v >> w;
grp[i].push_back(pii(w, v));
}
}
cin >> q;
while (q--) {
cin >> cityA >> cityB;
cout << dijkstra(cityA, cityB) << endl;
}
note.clear();
for (int i = 1; i <= V; ++i) {
grp[i].clear();
}
}
return 0;
}