//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ve vector
typedef long long ll;
int C;
ve<ve<pair<int, int>>> G;
ve<int> Comp, D;
void dfs(int u, int d, int c) {
int v, t;
Comp[u] = c;
D[u] = d;
for (pair<int, int> e : G[u]) {
tie(v, t) = e;
if (Comp[v] == 0) dfs(v, d + t, c);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int k, n, m, o, a, b, t;
cin >> k >> n >> m >> o;
G = ve<ve<pair<int, int>>>(n);
Comp = ve<int>(n);
D = ve<int>(n);
for (int i = 0; i < m; i++) {
cin >> a >> b >> t;
G[a].push_back({b, t});
}
C = 0;
for (int i = 0; i < n; i++) {
if (Comp[i] == 0) {
C++;
dfs(i, 0, C);
}
}
for (int i = 0; i < o; i++) {
cin >> a >> b;
if (Comp[a] == Comp[b]) {
if (b >= a) cout << D[b] - D[a] << endl;
else cout << -1 << endl;
}
else cout << -1 << endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
5880 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
7 ms |
384 KB |
Output is correct |
6 |
Correct |
7 ms |
384 KB |
Output is correct |
7 |
Correct |
7 ms |
384 KB |
Output is correct |
8 |
Correct |
47 ms |
4600 KB |
Output is correct |
9 |
Correct |
49 ms |
4344 KB |
Output is correct |
10 |
Correct |
27 ms |
2176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
64 ms |
4096 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
5880 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
7 ms |
384 KB |
Output is correct |
6 |
Correct |
7 ms |
384 KB |
Output is correct |
7 |
Correct |
7 ms |
384 KB |
Output is correct |
8 |
Correct |
47 ms |
4600 KB |
Output is correct |
9 |
Correct |
49 ms |
4344 KB |
Output is correct |
10 |
Correct |
27 ms |
2176 KB |
Output is correct |
11 |
Incorrect |
64 ms |
4096 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |