#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(a,b) for (int a = 0; a < (b); ++a)
#define pb push_back
#define all(t) t.begin(), t.end()
struct Krawedz
{
int v, cena;
};
struct Zapytanie
{
int a, b, idx;
};
const int max_N = 5e4+5, max_K = 5, max_Q = 1e4+5;
const ll INF = 1e18+20;
int k = 0, n = 0, m = 0, q = 0, a = 0, b = 0, c = 0;
ll wyn[max_Q];
vector<Krawedz> krawedzie[max_N];
vector<Krawedz> odwrocone_krawedzie[max_N];
vector<Zapytanie> zap;
ll dist[max_K][max_N];
inline void dziel_i_rzadz(int l, int p, vector<Zapytanie> zap)
{
if (l == p) return;
int s = (l+p) / 2;
vector<Zapytanie> lewe, prawe, akt;
for (auto x : zap)
{
if (x.b/k < s) lewe.pb(x);
else if (x.a/k > s) prawe.pb(x);
else akt.pb(x);
}
rep(i,max_K)
{
rep(j,max_K) for (int f = l*k; f <= (p+1)*k; ++f) dist[j][f] = INF;
dist[i][s*k+i] = 0;
for (int j = s*k-1; j >= l*k; --j) for (auto x : krawedzie[j]) dist[i][j] = min(dist[i][j],dist[i][x.v] + x.cena);
for (int j = (s+1)*k; j < (p+1)*k; ++j) for (auto x : odwrocone_krawedzie[j]) dist[i][j] = min(dist[i][j],dist[i][x.v] + x.cena);
for (auto x : akt) if (dist[i][x.a] != INF and dist[i][x.b] != INF) wyn[x.idx] = min(wyn[x.idx],dist[i][x.a] + dist[i][x.b]);
}
dziel_i_rzadz(l,s,lewe), dziel_i_rzadz(s+1,p,prawe);
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> k >> n >> m >> q;
rep(i,m)
{
cin >> a >> b >> c;
krawedzie[a].pb({b,c}), odwrocone_krawedzie[b].pb({a,c});
}
rep(i,q)
{
cin >> a >> b;
if (a > b) swap(a,b);
zap.pb({a,b,i});
}
rep(i,max_Q) wyn[i] = INF;
dziel_i_rzadz(0,n/k,zap);
rep(i,q)
{
if(wyn[i] == INF) cout << "-1" << '\n';
else cout << wyn[i] << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
8400 KB |
Output is correct |
2 |
Correct |
1 ms |
2772 KB |
Output is correct |
3 |
Correct |
2 ms |
2684 KB |
Output is correct |
4 |
Correct |
1 ms |
2684 KB |
Output is correct |
5 |
Correct |
2 ms |
2900 KB |
Output is correct |
6 |
Correct |
2 ms |
2900 KB |
Output is correct |
7 |
Correct |
2 ms |
2900 KB |
Output is correct |
8 |
Correct |
39 ms |
9224 KB |
Output is correct |
9 |
Correct |
38 ms |
9032 KB |
Output is correct |
10 |
Correct |
18 ms |
5388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
8460 KB |
Output is correct |
2 |
Correct |
2 ms |
2772 KB |
Output is correct |
3 |
Correct |
2 ms |
2772 KB |
Output is correct |
4 |
Correct |
2 ms |
2680 KB |
Output is correct |
5 |
Correct |
2 ms |
2684 KB |
Output is correct |
6 |
Correct |
1 ms |
2772 KB |
Output is correct |
7 |
Correct |
4 ms |
3528 KB |
Output is correct |
8 |
Correct |
4 ms |
3604 KB |
Output is correct |
9 |
Correct |
40 ms |
9364 KB |
Output is correct |
10 |
Correct |
73 ms |
12512 KB |
Output is correct |
11 |
Correct |
58 ms |
10228 KB |
Output is correct |
12 |
Correct |
51 ms |
9608 KB |
Output is correct |
13 |
Correct |
73 ms |
11904 KB |
Output is correct |
14 |
Correct |
44 ms |
8840 KB |
Output is correct |
15 |
Correct |
39 ms |
7940 KB |
Output is correct |
16 |
Correct |
38 ms |
7892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2772 KB |
Output is correct |
2 |
Correct |
1 ms |
2772 KB |
Output is correct |
3 |
Correct |
1 ms |
2772 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2772 KB |
Output is correct |
6 |
Correct |
2 ms |
2772 KB |
Output is correct |
7 |
Correct |
2 ms |
2772 KB |
Output is correct |
8 |
Correct |
3 ms |
2900 KB |
Output is correct |
9 |
Correct |
2 ms |
2900 KB |
Output is correct |
10 |
Correct |
46 ms |
7892 KB |
Output is correct |
11 |
Correct |
50 ms |
9352 KB |
Output is correct |
12 |
Correct |
68 ms |
11724 KB |
Output is correct |
13 |
Correct |
73 ms |
12500 KB |
Output is correct |
14 |
Correct |
62 ms |
10696 KB |
Output is correct |
15 |
Correct |
37 ms |
7672 KB |
Output is correct |
16 |
Correct |
37 ms |
7628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2772 KB |
Output is correct |
2 |
Correct |
1 ms |
2772 KB |
Output is correct |
3 |
Correct |
1 ms |
2772 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2772 KB |
Output is correct |
6 |
Correct |
2 ms |
2772 KB |
Output is correct |
7 |
Correct |
2 ms |
2772 KB |
Output is correct |
8 |
Correct |
3 ms |
2900 KB |
Output is correct |
9 |
Correct |
2 ms |
2900 KB |
Output is correct |
10 |
Correct |
46 ms |
7892 KB |
Output is correct |
11 |
Correct |
50 ms |
9352 KB |
Output is correct |
12 |
Correct |
68 ms |
11724 KB |
Output is correct |
13 |
Correct |
73 ms |
12500 KB |
Output is correct |
14 |
Correct |
62 ms |
10696 KB |
Output is correct |
15 |
Correct |
37 ms |
7672 KB |
Output is correct |
16 |
Correct |
37 ms |
7628 KB |
Output is correct |
17 |
Correct |
66 ms |
9540 KB |
Output is correct |
18 |
Correct |
1 ms |
2772 KB |
Output is correct |
19 |
Correct |
1 ms |
2772 KB |
Output is correct |
20 |
Correct |
1 ms |
2680 KB |
Output is correct |
21 |
Correct |
1 ms |
2688 KB |
Output is correct |
22 |
Correct |
1 ms |
2772 KB |
Output is correct |
23 |
Correct |
3 ms |
3080 KB |
Output is correct |
24 |
Correct |
4 ms |
3028 KB |
Output is correct |
25 |
Correct |
3 ms |
3200 KB |
Output is correct |
26 |
Correct |
3 ms |
3156 KB |
Output is correct |
27 |
Correct |
40 ms |
8828 KB |
Output is correct |
28 |
Correct |
69 ms |
11904 KB |
Output is correct |
29 |
Correct |
84 ms |
12720 KB |
Output is correct |
30 |
Correct |
71 ms |
10916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
8400 KB |
Output is correct |
2 |
Correct |
1 ms |
2772 KB |
Output is correct |
3 |
Correct |
2 ms |
2684 KB |
Output is correct |
4 |
Correct |
1 ms |
2684 KB |
Output is correct |
5 |
Correct |
2 ms |
2900 KB |
Output is correct |
6 |
Correct |
2 ms |
2900 KB |
Output is correct |
7 |
Correct |
2 ms |
2900 KB |
Output is correct |
8 |
Correct |
39 ms |
9224 KB |
Output is correct |
9 |
Correct |
38 ms |
9032 KB |
Output is correct |
10 |
Correct |
18 ms |
5388 KB |
Output is correct |
11 |
Correct |
55 ms |
8460 KB |
Output is correct |
12 |
Correct |
2 ms |
2772 KB |
Output is correct |
13 |
Correct |
2 ms |
2772 KB |
Output is correct |
14 |
Correct |
2 ms |
2680 KB |
Output is correct |
15 |
Correct |
2 ms |
2684 KB |
Output is correct |
16 |
Correct |
1 ms |
2772 KB |
Output is correct |
17 |
Correct |
4 ms |
3528 KB |
Output is correct |
18 |
Correct |
4 ms |
3604 KB |
Output is correct |
19 |
Correct |
40 ms |
9364 KB |
Output is correct |
20 |
Correct |
73 ms |
12512 KB |
Output is correct |
21 |
Correct |
58 ms |
10228 KB |
Output is correct |
22 |
Correct |
51 ms |
9608 KB |
Output is correct |
23 |
Correct |
73 ms |
11904 KB |
Output is correct |
24 |
Correct |
44 ms |
8840 KB |
Output is correct |
25 |
Correct |
39 ms |
7940 KB |
Output is correct |
26 |
Correct |
38 ms |
7892 KB |
Output is correct |
27 |
Correct |
1 ms |
2772 KB |
Output is correct |
28 |
Correct |
1 ms |
2772 KB |
Output is correct |
29 |
Correct |
1 ms |
2772 KB |
Output is correct |
30 |
Correct |
1 ms |
2644 KB |
Output is correct |
31 |
Correct |
1 ms |
2772 KB |
Output is correct |
32 |
Correct |
2 ms |
2772 KB |
Output is correct |
33 |
Correct |
2 ms |
2772 KB |
Output is correct |
34 |
Correct |
3 ms |
2900 KB |
Output is correct |
35 |
Correct |
2 ms |
2900 KB |
Output is correct |
36 |
Correct |
46 ms |
7892 KB |
Output is correct |
37 |
Correct |
50 ms |
9352 KB |
Output is correct |
38 |
Correct |
68 ms |
11724 KB |
Output is correct |
39 |
Correct |
73 ms |
12500 KB |
Output is correct |
40 |
Correct |
62 ms |
10696 KB |
Output is correct |
41 |
Correct |
37 ms |
7672 KB |
Output is correct |
42 |
Correct |
37 ms |
7628 KB |
Output is correct |
43 |
Correct |
66 ms |
9540 KB |
Output is correct |
44 |
Correct |
1 ms |
2772 KB |
Output is correct |
45 |
Correct |
1 ms |
2772 KB |
Output is correct |
46 |
Correct |
1 ms |
2680 KB |
Output is correct |
47 |
Correct |
1 ms |
2688 KB |
Output is correct |
48 |
Correct |
1 ms |
2772 KB |
Output is correct |
49 |
Correct |
3 ms |
3080 KB |
Output is correct |
50 |
Correct |
4 ms |
3028 KB |
Output is correct |
51 |
Correct |
3 ms |
3200 KB |
Output is correct |
52 |
Correct |
3 ms |
3156 KB |
Output is correct |
53 |
Correct |
40 ms |
8828 KB |
Output is correct |
54 |
Correct |
69 ms |
11904 KB |
Output is correct |
55 |
Correct |
84 ms |
12720 KB |
Output is correct |
56 |
Correct |
71 ms |
10916 KB |
Output is correct |
57 |
Correct |
92 ms |
13916 KB |
Output is correct |
58 |
Correct |
39 ms |
9184 KB |
Output is correct |
59 |
Correct |
55 ms |
10164 KB |
Output is correct |