답안 #697036

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
697036 2023-02-08T01:58:06 Z vjudge1 Toll (BOI17_toll) C++11
100 / 100
422 ms 177880 KB
#include<bits/stdc++.h>

using namespace std;

#define ll long long
typedef pair<ll, ll> pii;
#define rose() ios_base::sync_with_stdio(NULL); cin.tie(nullptr); cout.tie(nullptr);
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define gcd __gcd
#define getl(s) getline(cin, s);
#define setpre(x) fixed << setprecision(x)
#define mset(a) memset(a, 0, sizeof(a))
#define endl '\n'



const ll hanni = 1e4, minji = 1e9;

struct Matrix{
	long long int a[5][5];
	Matrix(){
		for(int i = 0; i < 5; i++){
			for (int j = 0; j < 5; j++){
				a[i][j] = minji;
			}
		}
	}
};

Matrix mul(Matrix a, Matrix b){
    Matrix c;
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
            c.a[i][j] = minji;
            for (int k = 0; k < 5; k++) {
                c.a[i][j] = min(c.a[i][j], a.a[i][k] + b.a[k][j]);
            }
        }
    }
    return c;
}

Matrix up[50005][18];
signed main(){
	int k, n, m, o;
    cin >> k >> n >> m >> o;
    for(int i=1; i<=m; i++) {
    	int u, v, w;
    	cin >> u >> v >> w;
//     	up[j][i]: tầng thứ 2^i bên cạnh tầng j
    	up[u/k][0].a[u%k][v%k] = w;
//     	a[x][y]: x là vị trí của đỉnh u trong tầng u nằm, y tương tự là của v
    }
    for(int i=1; (1 << i) <= n; i++){
    	for(int j=0; j + (1 << i) - 1 <= n; j++){
    		up[j][i] = mul(up[j][i-1], up[j+(1<<(i-1))][i-1]);
    	}
    }
    for(int i = 0; i<o; i++){
    	int a, b;
    	cin >> a >> b;
    	Matrix res;
    	for(int j = 0; j < 5; j++){
    		res.a[j][j] = 0;
    	}
    	if(a == b){
    		cout << 0 << endl;
    		continue;
    	}
    	int div1 = a / k, div2 = b / k;
    	int dist = div2 - div1;
    	for(int j = 16; j >= 0; j--){
    		if(dist & (1 << j)){
    			res = mul(res, up[div1][j]);
    			div1 += (1 << j);
    		}
    	}
    	if(res.a[a % k][b % k] == minji) cout << -1 << endl;
    	else cout << res.a[a % k][b % k] << endl;
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 307 ms 177476 KB Output is correct
2 Correct 85 ms 176392 KB Output is correct
3 Correct 73 ms 176316 KB Output is correct
4 Correct 73 ms 176424 KB Output is correct
5 Correct 88 ms 176320 KB Output is correct
6 Correct 73 ms 176344 KB Output is correct
7 Correct 78 ms 176380 KB Output is correct
8 Correct 299 ms 177344 KB Output is correct
9 Correct 347 ms 177320 KB Output is correct
10 Correct 261 ms 176480 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 354 ms 177684 KB Output is correct
2 Correct 70 ms 176340 KB Output is correct
3 Correct 69 ms 176344 KB Output is correct
4 Correct 74 ms 176320 KB Output is correct
5 Correct 77 ms 176400 KB Output is correct
6 Correct 71 ms 176412 KB Output is correct
7 Correct 98 ms 176504 KB Output is correct
8 Correct 95 ms 176492 KB Output is correct
9 Correct 291 ms 177328 KB Output is correct
10 Correct 364 ms 177716 KB Output is correct
11 Correct 342 ms 177740 KB Output is correct
12 Correct 342 ms 177720 KB Output is correct
13 Correct 262 ms 177700 KB Output is correct
14 Correct 244 ms 177688 KB Output is correct
15 Correct 223 ms 177684 KB Output is correct
16 Correct 239 ms 177632 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 68 ms 176332 KB Output is correct
2 Correct 69 ms 176364 KB Output is correct
3 Correct 67 ms 176468 KB Output is correct
4 Correct 68 ms 176356 KB Output is correct
5 Correct 71 ms 176396 KB Output is correct
6 Correct 71 ms 176364 KB Output is correct
7 Correct 73 ms 176344 KB Output is correct
8 Correct 76 ms 176384 KB Output is correct
9 Correct 82 ms 176464 KB Output is correct
10 Correct 273 ms 177224 KB Output is correct
11 Correct 306 ms 177696 KB Output is correct
12 Correct 325 ms 177800 KB Output is correct
13 Correct 331 ms 177696 KB Output is correct
14 Correct 334 ms 177700 KB Output is correct
15 Correct 222 ms 177512 KB Output is correct
16 Correct 205 ms 177612 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 68 ms 176332 KB Output is correct
2 Correct 69 ms 176364 KB Output is correct
3 Correct 67 ms 176468 KB Output is correct
4 Correct 68 ms 176356 KB Output is correct
5 Correct 71 ms 176396 KB Output is correct
6 Correct 71 ms 176364 KB Output is correct
7 Correct 73 ms 176344 KB Output is correct
8 Correct 76 ms 176384 KB Output is correct
9 Correct 82 ms 176464 KB Output is correct
10 Correct 273 ms 177224 KB Output is correct
11 Correct 306 ms 177696 KB Output is correct
12 Correct 325 ms 177800 KB Output is correct
13 Correct 331 ms 177696 KB Output is correct
14 Correct 334 ms 177700 KB Output is correct
15 Correct 222 ms 177512 KB Output is correct
16 Correct 205 ms 177612 KB Output is correct
17 Correct 310 ms 177800 KB Output is correct
18 Correct 68 ms 176332 KB Output is correct
19 Correct 68 ms 176408 KB Output is correct
20 Correct 78 ms 176392 KB Output is correct
21 Correct 74 ms 176332 KB Output is correct
22 Correct 70 ms 176380 KB Output is correct
23 Correct 79 ms 176372 KB Output is correct
24 Correct 78 ms 176420 KB Output is correct
25 Correct 92 ms 176412 KB Output is correct
26 Correct 79 ms 176500 KB Output is correct
27 Correct 304 ms 177208 KB Output is correct
28 Correct 334 ms 177740 KB Output is correct
29 Correct 393 ms 177784 KB Output is correct
30 Correct 320 ms 177680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 307 ms 177476 KB Output is correct
2 Correct 85 ms 176392 KB Output is correct
3 Correct 73 ms 176316 KB Output is correct
4 Correct 73 ms 176424 KB Output is correct
5 Correct 88 ms 176320 KB Output is correct
6 Correct 73 ms 176344 KB Output is correct
7 Correct 78 ms 176380 KB Output is correct
8 Correct 299 ms 177344 KB Output is correct
9 Correct 347 ms 177320 KB Output is correct
10 Correct 261 ms 176480 KB Output is correct
11 Correct 354 ms 177684 KB Output is correct
12 Correct 70 ms 176340 KB Output is correct
13 Correct 69 ms 176344 KB Output is correct
14 Correct 74 ms 176320 KB Output is correct
15 Correct 77 ms 176400 KB Output is correct
16 Correct 71 ms 176412 KB Output is correct
17 Correct 98 ms 176504 KB Output is correct
18 Correct 95 ms 176492 KB Output is correct
19 Correct 291 ms 177328 KB Output is correct
20 Correct 364 ms 177716 KB Output is correct
21 Correct 342 ms 177740 KB Output is correct
22 Correct 342 ms 177720 KB Output is correct
23 Correct 262 ms 177700 KB Output is correct
24 Correct 244 ms 177688 KB Output is correct
25 Correct 223 ms 177684 KB Output is correct
26 Correct 239 ms 177632 KB Output is correct
27 Correct 68 ms 176332 KB Output is correct
28 Correct 69 ms 176364 KB Output is correct
29 Correct 67 ms 176468 KB Output is correct
30 Correct 68 ms 176356 KB Output is correct
31 Correct 71 ms 176396 KB Output is correct
32 Correct 71 ms 176364 KB Output is correct
33 Correct 73 ms 176344 KB Output is correct
34 Correct 76 ms 176384 KB Output is correct
35 Correct 82 ms 176464 KB Output is correct
36 Correct 273 ms 177224 KB Output is correct
37 Correct 306 ms 177696 KB Output is correct
38 Correct 325 ms 177800 KB Output is correct
39 Correct 331 ms 177696 KB Output is correct
40 Correct 334 ms 177700 KB Output is correct
41 Correct 222 ms 177512 KB Output is correct
42 Correct 205 ms 177612 KB Output is correct
43 Correct 310 ms 177800 KB Output is correct
44 Correct 68 ms 176332 KB Output is correct
45 Correct 68 ms 176408 KB Output is correct
46 Correct 78 ms 176392 KB Output is correct
47 Correct 74 ms 176332 KB Output is correct
48 Correct 70 ms 176380 KB Output is correct
49 Correct 79 ms 176372 KB Output is correct
50 Correct 78 ms 176420 KB Output is correct
51 Correct 92 ms 176412 KB Output is correct
52 Correct 79 ms 176500 KB Output is correct
53 Correct 304 ms 177208 KB Output is correct
54 Correct 334 ms 177740 KB Output is correct
55 Correct 393 ms 177784 KB Output is correct
56 Correct 320 ms 177680 KB Output is correct
57 Correct 422 ms 177748 KB Output is correct
58 Correct 306 ms 177352 KB Output is correct
59 Correct 349 ms 177880 KB Output is correct