This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
#define int long long
using namespace std;
const int MAXN = 1e5 + 10;
void solve(int tc) {
int n;
cin >> n;
int m;
cin >> m;
int raw[n+1][n+1];
for(int i=0; i<=n; i++) {
for(int j=0; j<=n; j++) {
raw[i][j] = (i==j ? 0 : 1e16);
}
}
for(int i=1; i<=m; i++) {
int a, b, t;
cin >> a >> b >> t;
raw[a][b] = min(raw[a][b], t);
}
int K, q; cin >> K >> q;
K = min(K, n);
int dp[K+1][n+1][n+1];
for(int i=1; i<=n; i++) {
for(int j=1; j<=n; j++) {
dp[1][i][j] = raw[i][j];
}
}
for(int i=2; i<=K; i++) {
for(int j=1; j<=n; j++) {
for(int k=1; k<=n; k++) {
dp[i][j][k] = 1e16;
for(int l=1; l<=n; l++) {
dp[i][j][k] = min(min(dp[i][j][k], dp[i-1][j][k]), dp[i-1][j][l] + raw[l][k]);
}
}
}
}
while(q--) {
int c, d;
cin >> c >> d;
cout << (dp[K][c][d] == 1e16 ? -1 : dp[K][c][d]) << "\n";
}
}
int32_t main() {
ios::sync_with_stdio(0); cin.tie(0);
int t=1; //cin >> t;
for(int i=1; i<=t; i++) solve(i);
}
/*
4 7
1 2 1
1 4 10
2 3 1
2 4 5
3 2 2
3 4 1
4 3 2
2 3
1 4
4 2
3 3
*/
# | 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... |