#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define per(i, a, b) for (int i = b - 1; i >= (a); --i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) x.size()
#define pb push_back
#define umap unordered_map
#define uset unordered_set
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef long long ll;
const int INF = 1000000007;
vii graph[50001];
int rgt[50001], dist[50001];
void solve(int u, int d) {
dist[u] = d;
if (graph[u].empty()) {
rgt[u] = u;
solve(u + 1, 0);
} else {
int v, w;
tie(v, w) = graph[u][0];
solve(v, d + w);
rgt[u] = rgt[v];
}
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
int k, n, m, o;
cin >> k >> n >> m >> o;
rep(i, 0, m) {
int u, v, w;
cin >> u >> v >> w;
graph[u].emplace_back(v, w);
}
if (k == 1) {
solve(0,0);
while(o--){
int a, b;
cin >> a >> b;
if(b<a||rgt[a]<b)cout<<-1<<endl;
else cout<<dist[b]-dist[a]<<endl;
}
} else {
int preva = -1;
set<ii> pq;
rep(i, 0, o) {
int a, b;
cin >> a >> b;
if (preva != a) {
fill(dist, dist + n, INF);
dist[a] = 0;
rep(i, 0, n) pq.insert({dist[i], i});
while (!pq.empty()) {
int d, u;
tie(d, u) = *pq.begin();
pq.erase(pq.begin());
trav(edge, graph[u]) {
int v, w;
tie(v, w) = edge;
if (d + w >= dist[v]) continue;
pq.erase({dist[v], v});
dist[v] = d + w;
pq.emplace(d + w, v);
}
}
}
preva = a;
if (dist[b] == INF)
cout << "-1\n";
else
cout << dist[b] << endl;
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
26 ms |
9540 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
65 ms |
5608 KB |
Output is correct |
2 |
Runtime error |
3 ms |
3532 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
3 ms |
3532 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
3 ms |
3532 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
26 ms |
9540 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |