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>
using namespace std;
#define endl '\n'
typedef long long ll;
ll n,m,a,b,w;
ll k,q,c,d;
ll adj[72][72];
priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,greater<pair<ll,ll>>> pq;
ll dp[72][72][72];
int main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
for(int i = 0; i < m; i++) {
cin >> a >> b >> w;
if(adj[a][b] == 0 || adj[a][b] > w)adj[a][b] = w;
}
cin >> k >> q;
if(k > 70) k = 70;
for(int i = 0; i < 71; i++) {
for(int j = 0; j < 71; j++) {
for(int l= 0; l < 71; l++) dp[i][j][l] = -1;
}
}
for(int j = 1; j <= n; j++) {
pq.push({0,j});
for(int i = 0; i < 72; i++) dp[j][j][i] = 0;
while(!pq.empty()) {
a = pq.top().first;
b = pq.top().second;
pq.pop();
for(int i = 1; i <= n; i++) {
bool able = false;
if(adj[b][i] == 0) continue;
for(int l = a; l < k; l++) {
if(dp[j][b][l] != -1 && (dp[j][i][l + 1] == -1 || dp[j][i][l + 1] > dp[j][b][l] + adj[b][i])) {
dp[j][i][l + 1] = dp[j][b][l] + adj[b][i];
if(!able) pq.push({l + 1,i});
able = true;
}
}
}
}
}
/*for(int i = 1; i < 5; i++) {
cout << "i = " << i << endl;
for(int j = 1; j < 5; j++) {
cout << "j = " << j << endl;
for(int l= 0; l < 2; l++) {
cout << dp[i][j][l] << ' ';
}
cout << endl;
}
}*/
for(int i = 0; i < q; i++) {
cin >> c >> d;
cout << dp[c][d][k] << 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... |