-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe.cpp
More file actions
48 lines (42 loc) · 1.1 KB
/
Copy pathe.cpp
File metadata and controls
48 lines (42 loc) · 1.1 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
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
#define REP(i, n, m) for(ll i=n; i<(ll)m; ++i)
#define IREP(i, n, m) for(ll i=n-1; i>=m; --i)
#define rep(i, n) REP(i, 0, n)
#define irep(i, n) IREP(i, n, 0)
#define all(v) v.begin(), v.end()
template<typename T=ll>
void warshall(vector<vector<T>> &v, function<bool(const T&, const T&)> comp=less<T>()){
ll n = v.size();
rep(i, n) rep(j, n) rep(k, n){
v[j][k] = comp(v[j][k], v[j][i]+v[i][k])? v[j][k] : (v[j][i]+v[i][k]);
}
}
int main(){
ll n, m, l;
cin >> n >> m >> l;
vvll d(n, vll(n, INT_MAX));
rep(i, n) d[i][i] = 0;
rep(i, m){
ll a, b, c;
cin >> a >> b >> c;
d[a-1][b-1] = d[b-1][a-1] = c;
}
ll q;
cin >> q;
vll s(q), t(q);
rep(i, q) cin >> s[i] >> t[i];
warshall(d);
rep(i, n) rep(j, n){
d[i][j] = i==j? 0 : d[i][j]<=l? 1 : INT_MAX;
}
warshall(d);
rep(i, q){
ll res = d[s[i]-1][t[i]-1];
cout << (res<INT_MAX? (res-1) : -1) << endl;
}
}