Submission #1300858

#TimeUsernameProblemLanguageResultExecution timeMemory
1300858Sir_Ahmed_ImranAutobus (COCI22_autobus)C++20
30 / 70
1095 ms7268 KiB
            //    01001100 01001111 01010100 01000001    \\
            //                                           \\
            //                ╦  ╔═╗╔╦╗╔═╗               \\
            //                ║  ║ ║ ║ ╠═╣               \\
            //                ╩═╝╚═╝ ╩ ╩ ╩               \\
            //                                           \\
            //    01001100 01001111 01010100 01000001    \\
 
#include <bits/stdc++.h>
using namespace std;
#define nl '\n'
#define ff first
#define ss second
#define ll long long
#define ld long double
#define terminator main
#define pll pair<ll,ll>
#define add insert
#define append push_back
#define pii pair<int,int>
#define all(x) (x).begin(),(x).end()
#define L0TA ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)

const int N = 71;

int n;
ll dp[N][N][N];
vector<pii> a[N];

void compute(int s){
    ll w;
    int v, k;
    for(int i = 1; i <= n; i++)
        for(int j = 0; j < n; j++)
            dp[s][i][j] = 1e9;
    dp[s][s][0] = 0;
    for(int i = 1; i <= n; i++)
        dp[s][i][n] = 0;
    priority_queue<pair<ll, pii>, vector<pair<ll, pii>>, greater<pair<ll, pii>>> Q;
    Q.push({0, {s, 0}});
    while(!Q.empty()){
        w = Q.top().ff;
        v = Q.top().ss.ff;
        k = Q.top().ss.ss;
        Q.pop();
        if(w == dp[s][v][k]){
            for(auto & [i, j] : a[v]){
                if(w + j < dp[s][i][k + 1]){
                    dp[s][i][k + 1] = w + j;
                    Q.push({w + j, {i, k + 1}});
                }
            }
        }
    }
}

void solve(){
    ll ans;
    int m, v, u, w, k, q;
    cin >> n >> m;
    for(int i = 1; i <= m; i++){
        cin >> v >> u >> w;
        a[v].append({u, w});
    }
    for(int i = 1; i <= n; i++)
        compute(i);
    cin >> k >> q;
    k = min(k, n - 1);
    while(q--){
        cin >> v >> u;
        ans = 1e9;
        for(int i = 0; i <= k; i++)
            ans = min(ans, dp[v][u][i]);
        if(ans < 1e9) cout << ans << nl;
        else cout << "-1\n";
    }

}

int terminator(){
    L0TA;
    int T = 1;
    //cin >> T;
    while(T--)
        solve();
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...