-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcses_1672.cpp
More file actions
48 lines (43 loc) · 1007 Bytes
/
cses_1672.cpp
File metadata and controls
48 lines (43 loc) · 1007 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <climits>
using namespace std;
typedef long long ll;
const int MAXN = 587;
ll d[MAXN][MAXN];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n, m, q, a, b, c;
cin >> n >> m >> q;
for (int i = 0; i < MAXN; i++)
for (int j = 0; j < MAXN; j++)
{
if (i == j)
d[i][j] = 0;
else
d[i][j] = LLONG_MAX / 2;
}
for (int i = 0; i < m; i++)
{
cin >> a >> b >> c;
if (c < d[a][b])
d[a][b] = c;
if (c < d[b][a])
d[b][a] = c;
}
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (d[i][j] > d[i][k] + d[k][j])
d[i][j] = d[i][k] + d[k][j];
while (q--)
{
cin >> a >> b;
if (d[a][b] == LLONG_MAX / 2)
cout << "-1\n";
else
cout << d[a][b] << "\n";
}
return 0;
}