#include <bits/stdc++.h>
#define int long long
#define ff first
#define ss second
using namespace std;
struct Edge {
int x, y, t;
Edge(int xi, int yi, int ti):x(xi),y(yi),t(ti){}
};
int k, n, m, q, x, y, t;
vector<vector<Edge>> graph;
signed main() {
cin >> k >> n >> m >> q;
if(k == 1) {
vector<int> a(n, 1e10), prefix(n);
for(int i = 0; i < m; i++) {
cin >> x >> y >> t;
a[x] = t;
}
for(int i = 0; i < n; i++) prefix[i] = a[i] + (i == 0 ? 0 : prefix[i - 1]);
while(q--) {
cin >> x >> y;
if(prefix[y - 1] - (x == 0 ? 0 : prefix[x - 1]) >= 1e10) cout << "-1\n";
else cout << (prefix[y - 1] - (x == 0 ? 0 : prefix[x - 1])) << "\n";
}
return 0;
}
graph = vector<vector<Edge>>(n);
for(int i = 0; i < m; i++) {
cin >> x >> y >> t;
graph[x].push_back(Edge(x, y, t));
}
priority_queue<pair<int,int>> pq;
vector<int> dist(n, LLONG_MAX);
pair<int,int> temp;
pq.push({0, 0});
while(pq.size()) {
temp = pq.top();
pq.pop();
temp.ff = -temp.ff;
//cout << temp.ff << " " << temp.ss << "\n";
if(dist[temp.ss] < temp.ff) continue;
dist[temp.ss] = temp.ff;
for(auto j : graph[temp.ss]) {
if(temp.ff + j.t < dist[j.y]) pq.push({-(temp.ff + j.t), j.y});
}
}
while(q--) {
cin >> x >> y;
if(dist[y] == LLONG_MAX) cout << "-1\n";
else cout << dist[y] << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
1112 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
448 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
47 ms |
1220 KB |
Output is correct |
9 |
Correct |
46 ms |
1620 KB |
Output is correct |
10 |
Correct |
16 ms |
1248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
93 ms |
5160 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
16 ms |
348 KB |
Output is correct |
8 |
Correct |
18 ms |
604 KB |
Output is correct |
9 |
Correct |
44 ms |
1212 KB |
Output is correct |
10 |
Correct |
124 ms |
7252 KB |
Output is correct |
11 |
Correct |
90 ms |
5316 KB |
Output is correct |
12 |
Correct |
61 ms |
4532 KB |
Output is correct |
13 |
Correct |
132 ms |
8064 KB |
Output is correct |
14 |
Correct |
63 ms |
4948 KB |
Output is correct |
15 |
Correct |
55 ms |
4072 KB |
Output is correct |
16 |
Correct |
53 ms |
3920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
1112 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
448 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
47 ms |
1220 KB |
Output is correct |
9 |
Correct |
46 ms |
1620 KB |
Output is correct |
10 |
Correct |
16 ms |
1248 KB |
Output is correct |
11 |
Correct |
93 ms |
5160 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
16 ms |
348 KB |
Output is correct |
18 |
Correct |
18 ms |
604 KB |
Output is correct |
19 |
Correct |
44 ms |
1212 KB |
Output is correct |
20 |
Correct |
124 ms |
7252 KB |
Output is correct |
21 |
Correct |
90 ms |
5316 KB |
Output is correct |
22 |
Correct |
61 ms |
4532 KB |
Output is correct |
23 |
Correct |
132 ms |
8064 KB |
Output is correct |
24 |
Correct |
63 ms |
4948 KB |
Output is correct |
25 |
Correct |
55 ms |
4072 KB |
Output is correct |
26 |
Correct |
53 ms |
3920 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
29 |
Halted |
0 ms |
0 KB |
- |