Submission #1188086

#TimeUsernameProblemLanguageResultExecution timeMemory
1188086Haciyev12Autobus (COCI22_autobus)C++20
15 / 70
29 ms4024 KiB
#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 dis[75][75], fly[75][75][75];
void solve(){
    ll n,m;
    cin >> n >> m;
    for(int i = 1; i <= n; i++){
        for(int j= 1; j <= n; j++){
            for(int h = 1; h <= n; h++){
                fly[j][h][i] = INF;
            }
        }
    }
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            if(i == j){
                dis[i][j] = 0;
            }
            else{
                dis[i][j] = INF;
            }
            fly[i][j][0] = dis[i][j];
        }
    }
    ll x,y,z;
    for(int i = 1; i <= m; i++){
        cin >> x >>y>>z;
        dis[x][y] = min(dis[x][y], z);
        fly[x][y][1] = dis[x][y];
    }
   // cout << fly[1][4][2] << endl;
    for(int i = 1; i < n; i++){
        for(int f = 1; f <= n; f++){

            for(int j = 1; j <= n; j++){
                for(int h = 1; h <= n; h++){//j = 1, h = 4, i = 2,                 1,2,1 + 2 4    j , f i - 1 + f h
                    
                    fly[j][h][i] = min(fly[j][h][i], fly[j][f][i - 1] + dis[f][h]);
                    
                }
            }
        }
    }
    ll k,q;
    cin >> k >> q;
    ll u, p; 
  while(q--){
    cin >> u >> p;
   
    if(u == p){
        cout << 0 << endl;
        continue;
    }
    if(fly[u][p][k] == INF || fly[u][p][k] == 0){
        cout << -1  << endl;
        continue;
    }
    cout << fly[u][p][k] << endl;
  }
}
int main() {
    fast;
    ll t = 1;
   //  cin >> t;
    while(t--){
        solve();
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...