#include "bits/stdc++.h"
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long
#define pb push_back
#define in insert
#define F first
#define S second
#define vll vector<ll>
#define all(v) v.begin(), v.end()
#define rep(a, b, c) for (int(a) = (b); (a) < (c); (a)++) // i, 0, n
#define endl '\n'
#define pii pair<ll,ll>
#define yes cout << "Yes" << endl;
#define no cout << "No" << endl;
using namespace std;
const ll INF = 1e18, mod = 1e9 + 7, N = 1e5 + 5;
ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a % b);
}
ll lcm(ll a, ll b) {
return (a / gcd(a, b)) * b;
}
ll modpow(ll a, ll b) {
ll ans = 1;
while(b > 0) {
if(b % 2 == 1) {
ans *= a;
ans %= mod;
}
a *= a;
a %= mod;
b >>= 1;
}
return ans % mod;
}
ll inv(ll a) {
return modpow(a, mod-2) % mod;
}
ll n,m;
ll k,q;
vector<pii>g[75];
ll used[75];
ll cnt= 0;
ll sum = 0, fh = 1;
ll dis[75][75];
void dfs(ll s){
used[s] =1 ;
for(auto i : g[s]){
ll val = i.S;
ll nod = i.F;
if(!used[nod]){
cnt++;
if(cnt > k){
cnt--;
return;
}
sum += val;
// cout << nod << endl;
dis[fh][nod] = sum;
dfs(nod);
cnt--;
sum -= val;
}
}
}
void solve(){
cin >> n >> m;
ll x,y,z;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
if(i == j){
continue;
}
dis[i][j] = -1;
}
}
for(int i = 0; i < m; i++){
cin >> x >> y >> z;
g[x].pb({y,z});
}
cin >> k >> q;
cnt = 0;
sum = 0;
// dfs(1);
//cout << dis[1][4] << endl;
cnt = 0;
for(int i = 1; i <= n; i++){
fh = i;
cnt = 0;
sum = 0;
dfs(i);
for(int j = 1; j <= n; j++){
used[j] = 0;
}
}
// cout << dis[1][4] << endl;
ll u, p;
while(q--){
cin >> u >> p;
if(u == p){
cout << 0 << endl;
continue;
}
if(dis[u][p] == -1){
cout << -1 << endl;
continue;
}
cout << dis[u][p] << endl;
}
}
int main() {
fast;
ll t = 1;
// cin >> t;
while(t--){
solve();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |