Submission #696498

# Submission time Handle Problem Language Result Execution time Memory
696498 2023-02-06T15:45:32 Z vjudge1 Toll (BOI17_toll) C++14
100 / 100
444 ms 177284 KB
#include <iostream>
using namespace std;
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] = 999999999;
			}
		}
	}
};
MATRIX f[50005][18];
MATRIX mul(MATRIX a, MATRIX b){
    MATRIX res;
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
            res.a[i][j] = 999999999;
            for (int k = 0; k < 5; k++) {
                res.a[i][j] = min(res.a[i][j], a.a[i][k] + b.a[k][j]);
            }
        }
    }
    return res;
}
int main() {
	int n, k, m, o;
    cin >> k >> n >> m >> o;
    for (int i = 1; i <= m; i++) {
    	int a, b, t;
    	cin >> a >> b >> t;
    	f[a / k][0].a[a % k][b % k] = t;
    }
    for (int i = 1; (1 << i) <= n; i++){
    	for (int j = 0; j + (1 << i) - 1 <= n; j++) {
    		f[j][i] = mul(f[j][i - 1], f[j + (1 << (i - 1))][i - 1]);
    	}
    }
    for (int jj = 0; jj < o; jj++) {
    	int a, b;
    	cin >> a >> b;
    	MATRIX s;
    	for (int j = 0; j < 5; j++) {
    		s.a[j][j] = 0;
    	}
    	if (a == b) {
    		cout << 0 << endl;
    		continue;
    	}
    	int h = a / k, g = b / k;
    	if (g <= h) {
    		cout << -1 << endl;
    	}
    	else {
    		int l = g - h;
    		for (int j = 16; j >= 0; j--) {
    			if (l & (1 << j)) {
    				s = mul(s, f[h][j]);
    				h += (1 << j);
    			}
    		}
    		if (s.a[a % k][b % k] == 999999999) cout << -1 << endl;
    		else cout << s.a[a % k][b % k] << endl;
    	}
    }
}
# Verdict Execution time Memory Grader output
1 Correct 353 ms 177284 KB Output is correct
2 Correct 67 ms 176316 KB Output is correct
3 Correct 68 ms 176432 KB Output is correct
4 Correct 69 ms 176340 KB Output is correct
5 Correct 74 ms 176392 KB Output is correct
6 Correct 72 ms 176428 KB Output is correct
7 Correct 73 ms 176440 KB Output is correct
8 Correct 319 ms 177160 KB Output is correct
9 Correct 318 ms 177192 KB Output is correct
10 Correct 304 ms 176484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 358 ms 177192 KB Output is correct
2 Correct 68 ms 176320 KB Output is correct
3 Correct 68 ms 176332 KB Output is correct
4 Correct 73 ms 176448 KB Output is correct
5 Correct 67 ms 176328 KB Output is correct
6 Correct 68 ms 176328 KB Output is correct
7 Correct 110 ms 176440 KB Output is correct
8 Correct 107 ms 176496 KB Output is correct
9 Correct 310 ms 177112 KB Output is correct
10 Correct 371 ms 177192 KB Output is correct
11 Correct 353 ms 177168 KB Output is correct
12 Correct 328 ms 177148 KB Output is correct
13 Correct 300 ms 177196 KB Output is correct
14 Correct 237 ms 177176 KB Output is correct
15 Correct 234 ms 177192 KB Output is correct
16 Correct 228 ms 177252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 67 ms 176332 KB Output is correct
2 Correct 70 ms 176396 KB Output is correct
3 Correct 72 ms 176400 KB Output is correct
4 Correct 68 ms 176332 KB Output is correct
5 Correct 69 ms 176432 KB Output is correct
6 Correct 71 ms 176424 KB Output is correct
7 Correct 74 ms 176328 KB Output is correct
8 Correct 74 ms 176452 KB Output is correct
9 Correct 76 ms 176392 KB Output is correct
10 Correct 278 ms 177192 KB Output is correct
11 Correct 323 ms 177196 KB Output is correct
12 Correct 340 ms 177188 KB Output is correct
13 Correct 387 ms 177188 KB Output is correct
14 Correct 324 ms 177120 KB Output is correct
15 Correct 223 ms 177100 KB Output is correct
16 Correct 250 ms 177200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 67 ms 176332 KB Output is correct
2 Correct 70 ms 176396 KB Output is correct
3 Correct 72 ms 176400 KB Output is correct
4 Correct 68 ms 176332 KB Output is correct
5 Correct 69 ms 176432 KB Output is correct
6 Correct 71 ms 176424 KB Output is correct
7 Correct 74 ms 176328 KB Output is correct
8 Correct 74 ms 176452 KB Output is correct
9 Correct 76 ms 176392 KB Output is correct
10 Correct 278 ms 177192 KB Output is correct
11 Correct 323 ms 177196 KB Output is correct
12 Correct 340 ms 177188 KB Output is correct
13 Correct 387 ms 177188 KB Output is correct
14 Correct 324 ms 177120 KB Output is correct
15 Correct 223 ms 177100 KB Output is correct
16 Correct 250 ms 177200 KB Output is correct
17 Correct 327 ms 176836 KB Output is correct
18 Correct 68 ms 176408 KB Output is correct
19 Correct 72 ms 176324 KB Output is correct
20 Correct 68 ms 176460 KB Output is correct
21 Correct 68 ms 176344 KB Output is correct
22 Correct 70 ms 176332 KB Output is correct
23 Correct 78 ms 176332 KB Output is correct
24 Correct 80 ms 176436 KB Output is correct
25 Correct 79 ms 176380 KB Output is correct
26 Correct 77 ms 176332 KB Output is correct
27 Correct 295 ms 176476 KB Output is correct
28 Correct 362 ms 176532 KB Output is correct
29 Correct 387 ms 176472 KB Output is correct
30 Correct 339 ms 176416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 353 ms 177284 KB Output is correct
2 Correct 67 ms 176316 KB Output is correct
3 Correct 68 ms 176432 KB Output is correct
4 Correct 69 ms 176340 KB Output is correct
5 Correct 74 ms 176392 KB Output is correct
6 Correct 72 ms 176428 KB Output is correct
7 Correct 73 ms 176440 KB Output is correct
8 Correct 319 ms 177160 KB Output is correct
9 Correct 318 ms 177192 KB Output is correct
10 Correct 304 ms 176484 KB Output is correct
11 Correct 358 ms 177192 KB Output is correct
12 Correct 68 ms 176320 KB Output is correct
13 Correct 68 ms 176332 KB Output is correct
14 Correct 73 ms 176448 KB Output is correct
15 Correct 67 ms 176328 KB Output is correct
16 Correct 68 ms 176328 KB Output is correct
17 Correct 110 ms 176440 KB Output is correct
18 Correct 107 ms 176496 KB Output is correct
19 Correct 310 ms 177112 KB Output is correct
20 Correct 371 ms 177192 KB Output is correct
21 Correct 353 ms 177168 KB Output is correct
22 Correct 328 ms 177148 KB Output is correct
23 Correct 300 ms 177196 KB Output is correct
24 Correct 237 ms 177176 KB Output is correct
25 Correct 234 ms 177192 KB Output is correct
26 Correct 228 ms 177252 KB Output is correct
27 Correct 67 ms 176332 KB Output is correct
28 Correct 70 ms 176396 KB Output is correct
29 Correct 72 ms 176400 KB Output is correct
30 Correct 68 ms 176332 KB Output is correct
31 Correct 69 ms 176432 KB Output is correct
32 Correct 71 ms 176424 KB Output is correct
33 Correct 74 ms 176328 KB Output is correct
34 Correct 74 ms 176452 KB Output is correct
35 Correct 76 ms 176392 KB Output is correct
36 Correct 278 ms 177192 KB Output is correct
37 Correct 323 ms 177196 KB Output is correct
38 Correct 340 ms 177188 KB Output is correct
39 Correct 387 ms 177188 KB Output is correct
40 Correct 324 ms 177120 KB Output is correct
41 Correct 223 ms 177100 KB Output is correct
42 Correct 250 ms 177200 KB Output is correct
43 Correct 327 ms 176836 KB Output is correct
44 Correct 68 ms 176408 KB Output is correct
45 Correct 72 ms 176324 KB Output is correct
46 Correct 68 ms 176460 KB Output is correct
47 Correct 68 ms 176344 KB Output is correct
48 Correct 70 ms 176332 KB Output is correct
49 Correct 78 ms 176332 KB Output is correct
50 Correct 80 ms 176436 KB Output is correct
51 Correct 79 ms 176380 KB Output is correct
52 Correct 77 ms 176332 KB Output is correct
53 Correct 295 ms 176476 KB Output is correct
54 Correct 362 ms 176532 KB Output is correct
55 Correct 387 ms 176472 KB Output is correct
56 Correct 339 ms 176416 KB Output is correct
57 Correct 444 ms 176540 KB Output is correct
58 Correct 313 ms 176420 KB Output is correct
59 Correct 344 ms 176432 KB Output is correct