제출 #526686

#제출 시각아이디문제언어결과실행 시간메모리
526686joelauAutobus (COCI22_autobus)C++14
70 / 70
421 ms2068 KiB
#include <bits/stdc++.h>
using namespace std;

int N,M,K,Q, lst[75][75], dist[75][5000], inf = 1e9;
priority_queue< pair<int,int>, vector< pair<int,int> >, greater< pair<int,int> > > pq;

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    cin >> N >> M;
    for (int i = 0; i < N; ++i) for (int j = 0; j < N; ++j) lst[i][j] = i == j ? 0 : inf;
    for (int i = 0; i < M; ++i) {
        int u,v,w; cin >> u >> v >> w; u--, v--;
        lst[u][v] = min(lst[u][v],w);
    }
    cin >> K >> Q; K = min(K,N-1);
    for (int i = 0; i < N; ++i) for (int j = 0; j < N*N; ++j) dist[i][j] = inf;
    for (int i = 0; i < N; ++i) {
        dist[i][i] = 0;
        pq.emplace(0,i);
        while (!pq.empty()) {
            int d,u; tie(d,u) = pq.top(); pq.pop();
            if (d > dist[i][u]) continue;
            for (int v = 0; v < N; ++v) if (dist[i][u] + lst[u%N][v] < dist[i][u/N*N+N+v]) {
                dist[i][u/N*N+N+v] = dist[i][u] + lst[u%N][v];
                pq.emplace(dist[i][u/N*N+N+v],u/N*N+N+v);
            }
        }
    }
    while (Q--) {
        int u,v; cin >> u >> v; u--, v--;
        int ans = inf;
        for (int i = 0; i <= K; ++i) ans = min(ans,dist[u][i*N+v]);
        if (ans == inf) ans = -1;
        cout << ans << '\n';
    }

    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...