This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
#define p_q priority_queue
#define endl '\n'
#define pb push_back
int n, m;
signed main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin>>n>>m;
vector<vector<int> > g(n+1,vector<int>(n+1,1e18)), dp(n+1,vector<int>(n+1,1e18))
, cnt(n+1,vector<int>(n+1,0));
for (int i = 1; i <= n; ++i) dp[i][i] = 0;
while(m--) {
int a, b, t;
cin>>a>>b>>t;
dp[a][b] = min(t,dp[a][b]);
cnt[a][b] = 1;
}
int k, q; cin>>k>>q;
for (int l = 1; l <= n; ++l) {
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if (dp[i][j] > dp[i][l]+dp[l][j] && cnt[i][l]+cnt[l][j] <= k) {
dp[i][j] = dp[i][l]+dp[l][j];
cnt[i][j] = cnt[i][l]+cnt[l][j];
}
}
}
}
while(q--) {
int c, d; cin>>c>>d;
if (dp[c][d] >= 1e18) cout<<-1<<endl;
else cout<<dp[c][d]<<endl;
}
return 0;
}
# | 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... |