#include<bits/stdc++.h>
#define taskname "B"
using namespace std;
const int lim = 75;
const int INF = 1e9;
template<class T>bool minimize(T& a, T b){
if(a > b){
a = b;
return true;
}
return false;
}
struct Data{
int w, u, t;
Data(){}
Data(int w, int u, int t){
this->w = w;
this->u = u;
this->t = t;
}
friend bool operator <(const Data a, const Data b){
return a.w > b.w;
}
};
int g[lim][lim], dp[lim][lim][lim];
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if(fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
}
int n, m;
cin >> n >> m;
memset(g, 0x3f, sizeof(g));
for(int _ = 0; _ < m; _++){
int u, v, w;
cin >> u >> v >> w;
minimize(g[u][v], w);
}
memset(dp, 0x3f, sizeof(dp));
for(int i = 1; i <= n; i++){
priority_queue<Data>pq;
pq.push(Data(dp[i][i][0] = 0, i, 0));
while(!pq.empty()){
Data c = pq.top();
pq.pop();
if(c.t + 1 < lim && c.w == dp[i][c.u][c.t]){
for(int v = 1; v <= n; v++){
if(g[c.u][v] < INF && minimize(dp[i][v][c.t + 1], c.w + g[c.u][v])){
pq.push(Data(dp[i][v][c.t + 1], v, c.t + 1));
}
}
}
}
}
int k, q;
cin >> k >> q;
minimize(k, lim - 1);
for(int _ = 0; _ < q; _++){
int a, b, ans = INF;
cin >> a >> b;
for(int i = 0; i <= k; i++){
minimize(ans, dp[a][b][i]);
}
cout << (ans < INF ? ans : -1) << "\n";
}
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:29:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
29 | freopen(taskname".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |