#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< pii > > g(n+1);
vector<vector<int> > edge(n+1,vector<int>(n+1,1e18));
while(m--) {
int a, b, t;
cin>>a>>b>>t;
edge[a][b] = min(t,edge[a][b]);
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if (edge[i][j] < 1e18) {
g[i].pb({j,edge[i][j]});
}
}
}
int k, q; cin>>k>>q;
while(q--) {
int c, d; cin>>c>>d;
vector<int> dis(n+1,1e18), vis(n+1,0), mn(n+1,1e9+5);
dis[c] = 0;
p_q < pair<pair<int,int>,int>,vector<pair<pair<int,int>,int> >,greater<pair<pair<int,int>,int> > > pq;
pq.push({{0,0},c});
while(!pq.empty()) {
pair<pair<int,int>,int> cur = pq.top();
pq.pop();
if (vis[cur.second] || cur.first.second == k) continue;
vis[cur.second] = 1;
for (auto [v,w] : g[cur.second]) {
if (dis[v] > dis[cur.second]+w ||
(dis[v] == dis[cur.second]+w && cur.first.second+1 < mn[v])) {
dis[v] = dis[cur.second]+w;
mn[v] = cur.first.second+1;
pq.push({{dis[v],mn[v]},v});
}
}
}
if (dis[d] == 1e18) cout<<-1<<endl;
else cout<<dis[d]<<endl;
}
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:42:23: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
42 | for (auto [v,w] : g[cur.second]) {
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
340 KB |
Output is correct |
2 |
Incorrect |
13 ms |
400 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |