#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5e4 + 10;
const int Inf = 0x3f3f3f3f;
vector<pair<int, int>> Adj[N], Order, Order2[N];
int n, m, k, o, C[N], D[N], mark[N], ans[N];
void Dijkstra(int v)
{
memset(D, 63, sizeof D);
memset(mark, 0, sizeof mark);
D[v] = 0;
priority_queue<pair<int, int>> PQ;
PQ.emplace(0, v);
while (!PQ.empty())
{
int v = PQ.top().second; PQ.pop();
if (mark[v]) continue;
mark[v] = true;
for (auto [u, w] : Adj[v])
if (D[u] > D[v] + w)
D[u] = D[v] + w, PQ.emplace(-D[u], u);
}
}
void DFS(int v, int comp)
{
mark[v] = true;
C[v] = comp;
for (auto [u, w] : Adj[v])
D[u] = D[v] + w, DFS(u, comp);
}
void Subtask1()
{
for (int i = 0; i < n; i++)
if (!mark[i]) DFS(i, i);
for (auto [u, v] : Order)
{
if (u < v) swap(u, v);
if (C[u] == C[v])
printf("%d\n", D[u] - D[v]);
else
printf("%d\n", -1);
}
}
signed main()
{
scanf("%d%d%d%d", &k, &n, &m, &o);
for (int i = 0; i < m; i++)
{
int a, b, t;
scanf("%d%d%d", &a, &b, &t);
Adj[a].emplace_back(b, t);
}
vector<int> V;
for (int i = 0; i < o; i++)
{
int a, b;
scanf("%d%d", &a, &b);
Order.emplace_back(a, b);
Order2[a].emplace_back(b, i);
V.push_back(a);
}
sort(V.begin(), V.end());
V.resize(unique(V.begin(), V.end()) - V.begin());
if (k == 1) {Subtask1(); return 0;}
for (int x : V)
{
Dijkstra(x);
for (auto [u, i] : Order2[x])
ans[i] = D[u];
}
for (int i = 0; i < o; i++)
printf("%d\n", (ans[i] == Inf ? -1 : ans[i]));
return 0;
}
Compilation message
toll.cpp: In function 'int main()':
toll.cpp:47:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
47 | scanf("%d%d%d%d", &k, &n, &m, &o);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:51:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
51 | scanf("%d%d%d", &a, &b, &t);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
toll.cpp:58:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
58 | scanf("%d%d", &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
7692 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
3 ms |
2644 KB |
Output is correct |
8 |
Correct |
32 ms |
5324 KB |
Output is correct |
9 |
Correct |
22 ms |
5196 KB |
Output is correct |
10 |
Correct |
6 ms |
3412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
5016 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
3028 KB |
Output is correct |
4 |
Correct |
3 ms |
3028 KB |
Output is correct |
5 |
Correct |
1 ms |
3028 KB |
Output is correct |
6 |
Correct |
2 ms |
3028 KB |
Output is correct |
7 |
Correct |
4 ms |
3092 KB |
Output is correct |
8 |
Correct |
5 ms |
3476 KB |
Output is correct |
9 |
Correct |
22 ms |
5356 KB |
Output is correct |
10 |
Correct |
52 ms |
5944 KB |
Output is correct |
11 |
Correct |
37 ms |
5148 KB |
Output is correct |
12 |
Correct |
27 ms |
4908 KB |
Output is correct |
13 |
Correct |
55 ms |
5848 KB |
Output is correct |
14 |
Correct |
39 ms |
4772 KB |
Output is correct |
15 |
Correct |
33 ms |
4388 KB |
Output is correct |
16 |
Correct |
31 ms |
4472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
3028 KB |
Output is correct |
3 |
Correct |
2 ms |
3028 KB |
Output is correct |
4 |
Correct |
2 ms |
3052 KB |
Output is correct |
5 |
Correct |
2 ms |
3028 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
5 ms |
3028 KB |
Output is correct |
8 |
Correct |
12 ms |
3208 KB |
Output is correct |
9 |
Correct |
8 ms |
3028 KB |
Output is correct |
10 |
Correct |
21 ms |
4948 KB |
Output is correct |
11 |
Correct |
263 ms |
4596 KB |
Output is correct |
12 |
Correct |
336 ms |
5452 KB |
Output is correct |
13 |
Correct |
355 ms |
5736 KB |
Output is correct |
14 |
Correct |
299 ms |
5116 KB |
Output is correct |
15 |
Correct |
200 ms |
4352 KB |
Output is correct |
16 |
Correct |
196 ms |
4348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
3028 KB |
Output is correct |
3 |
Correct |
2 ms |
3028 KB |
Output is correct |
4 |
Correct |
2 ms |
3052 KB |
Output is correct |
5 |
Correct |
2 ms |
3028 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
5 ms |
3028 KB |
Output is correct |
8 |
Correct |
12 ms |
3208 KB |
Output is correct |
9 |
Correct |
8 ms |
3028 KB |
Output is correct |
10 |
Correct |
21 ms |
4948 KB |
Output is correct |
11 |
Correct |
263 ms |
4596 KB |
Output is correct |
12 |
Correct |
336 ms |
5452 KB |
Output is correct |
13 |
Correct |
355 ms |
5736 KB |
Output is correct |
14 |
Correct |
299 ms |
5116 KB |
Output is correct |
15 |
Correct |
200 ms |
4352 KB |
Output is correct |
16 |
Correct |
196 ms |
4348 KB |
Output is correct |
17 |
Execution timed out |
3069 ms |
4732 KB |
Time limit exceeded |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
7692 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
3 ms |
2644 KB |
Output is correct |
8 |
Correct |
32 ms |
5324 KB |
Output is correct |
9 |
Correct |
22 ms |
5196 KB |
Output is correct |
10 |
Correct |
6 ms |
3412 KB |
Output is correct |
11 |
Correct |
36 ms |
5016 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
3028 KB |
Output is correct |
14 |
Correct |
3 ms |
3028 KB |
Output is correct |
15 |
Correct |
1 ms |
3028 KB |
Output is correct |
16 |
Correct |
2 ms |
3028 KB |
Output is correct |
17 |
Correct |
4 ms |
3092 KB |
Output is correct |
18 |
Correct |
5 ms |
3476 KB |
Output is correct |
19 |
Correct |
22 ms |
5356 KB |
Output is correct |
20 |
Correct |
52 ms |
5944 KB |
Output is correct |
21 |
Correct |
37 ms |
5148 KB |
Output is correct |
22 |
Correct |
27 ms |
4908 KB |
Output is correct |
23 |
Correct |
55 ms |
5848 KB |
Output is correct |
24 |
Correct |
39 ms |
4772 KB |
Output is correct |
25 |
Correct |
33 ms |
4388 KB |
Output is correct |
26 |
Correct |
31 ms |
4472 KB |
Output is correct |
27 |
Correct |
1 ms |
2644 KB |
Output is correct |
28 |
Correct |
2 ms |
3028 KB |
Output is correct |
29 |
Correct |
2 ms |
3028 KB |
Output is correct |
30 |
Correct |
2 ms |
3052 KB |
Output is correct |
31 |
Correct |
2 ms |
3028 KB |
Output is correct |
32 |
Correct |
2 ms |
2644 KB |
Output is correct |
33 |
Correct |
5 ms |
3028 KB |
Output is correct |
34 |
Correct |
12 ms |
3208 KB |
Output is correct |
35 |
Correct |
8 ms |
3028 KB |
Output is correct |
36 |
Correct |
21 ms |
4948 KB |
Output is correct |
37 |
Correct |
263 ms |
4596 KB |
Output is correct |
38 |
Correct |
336 ms |
5452 KB |
Output is correct |
39 |
Correct |
355 ms |
5736 KB |
Output is correct |
40 |
Correct |
299 ms |
5116 KB |
Output is correct |
41 |
Correct |
200 ms |
4352 KB |
Output is correct |
42 |
Correct |
196 ms |
4348 KB |
Output is correct |
43 |
Execution timed out |
3069 ms |
4732 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |