제출 #699302

#제출 시각아이디문제언어결과실행 시간메모리
699302GitalAutobus (COCI22_autobus)C++11
15 / 70
1103 ms328648 KiB
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;

priority_queue<pair<pair<ll,ll>,pair<vector<bool>,int>>,vector<pair<pair<ll,ll>,pair<vector<bool>,int>>>,greater<pair<pair<ll,ll>,pair<vector<bool>,int>>>> pq;
ll dp[71][71];
ll path[71][71];
int n,m,k,q;
int a,b,t;
int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n >> m;
    for(int i = 0; i < m; i++) {
        cin >> a >> b >> t;
        if(path[a][b] == 0 || path[a][b] > t) path[a][b] = t;
    }
    cin >> k >> q;
    for(int i = 1; i <= n; i++) {
        vector<bool> temp(71,false);
        for(int j = 1; j <= 70; j++) {
            dp[i][j] = -1;
        }
        dp[i][i] = 0;
        pq.push({{0,i},{temp,0}});
        while(!pq.empty()) {
            ll a = pq.top().first.first;
            ll b = pq.top().first.second;
            vector<bool> checked = pq.top().second.first;
            int cnt = pq.top().second.second;
            pq.pop();
            if(cnt >= k) continue;
            if(checked[b]) continue;
            checked[b] = true;
            for(int j = 1; j <= n; j++) {
                if(path[b][j] != 0 && !checked[j]) {
                    pq.push({{a + path[b][j],j},{checked,cnt+1}});
                    if(dp[i][j] == -1 || dp[i][j] > a + path[b][j]) {
                        dp[i][j] = a + path[b][j];
                    }
                }
            }
        }
    }
    for(int i = 0; i < q; i++) {
        cin >> a >> b;
        cout << dp[a][b] << endl;
    }
    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...