Submission #697043

# Submission time Handle Problem Language Result Execution time Memory
697043 2023-02-08T02:09:43 Z vjudge1 Toll (BOI17_toll) C++17
100 / 100
418 ms 176752 KB
#include<bits/stdc++.h>

using namespace std;

#define mems(a, v) memset(a, v, sizeof(a))
#define all(v) v.begin(), v.end()
#define f first
#define sc second
#define pb push_back
#define mp make_pair
#define speed() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pi 3.14159265358979323846
#define BFS_BLACK 1
#define BFS_WHITE -1
#define BFS_GREY 2
#define DFS_BLACK 1
#define DFS_WHITE -1
#define DFS_GREY 2
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef vector<pair<ll,ll>> vii;
typedef vector<vi> vvi;
const ll LOG = 18;
const ll maxn = 50005;
const ll mod = 1e9 + 7;
const ll inf = 1e9;

struct Matrix
{
	ll a[5][5];
	Matrix()
	{
		for(ll i=0; i<5; i++)
        {
			for (ll j=0; j<5; j++)
			{
				a[i][j] = inf;
			}
		}
	}
};

Matrix up[maxn][18];

Matrix calc(Matrix x, Matrix y)
{
    Matrix c;
    for (ll i=0; i<5; i++)
    {
        for (ll j=0; j<5; j++)
        {
            c.a[i][j] = inf;
            for (ll k=0; k<5; k++)
            {
                c.a[i][j] = min(c.a[i][j], x.a[i][k] + y.a[k][j]);
            }
        }
    }
    return c;
}
signed main()
{
	ll k, n, m, o;
    cin >> k >> n >> m >> o;
    for(ll i=0; i<m; i++)
    {
    	ll u, v, w;
    	cin >> u >> v >> w;
    	up[u/k][0].a[u%k][v%k] = w;
    }
    for(ll i=1; (1<<i)<=n; i++)
    {
    	for(ll j=0; j+(1<<i)-1<=n; j++)
    	{
    		up[j][i] = calc(up[j][i-1], up[j+(1<<(i-1))][i-1]);
    	}
    }
    for(ll i=0; i<o; i++)
    {
    	ll a, b;
    	cin >> a >> b;
    	Matrix res;
    	for(ll j=0; j<5; j++)
    	{
    		res.a[j][j] = 0;
    	}
    	if(a == b)
    	{
    		cout << 0 << endl;
    		continue;
    	}
    	ll div1 = a/k, div2 = b/k;
    	ll dist = div2 - div1;
    	for(ll j=16; j>=0; j--)
    	{
    		if(dist & (1 << j))
    		{
    			res = calc(res, up[div1][j]);
    			div1 += (1 << j);
    		}
    	}
    	if(res.a[a%k][b%k] == inf)
    	{
    	    cout << -1 << endl;
    	}
    	else
        {
            cout << res.a[a%k][b%k] << endl;
        }
    }
}

# Verdict Execution time Memory Grader output
1 Correct 323 ms 176688 KB Output is correct
2 Correct 79 ms 176332 KB Output is correct
3 Correct 71 ms 176316 KB Output is correct
4 Correct 68 ms 176424 KB Output is correct
5 Correct 71 ms 176432 KB Output is correct
6 Correct 72 ms 176368 KB Output is correct
7 Correct 77 ms 176424 KB Output is correct
8 Correct 316 ms 176496 KB Output is correct
9 Correct 311 ms 176544 KB Output is correct
10 Correct 278 ms 176472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 381 ms 176672 KB Output is correct
2 Correct 70 ms 176420 KB Output is correct
3 Correct 69 ms 176332 KB Output is correct
4 Correct 67 ms 176376 KB Output is correct
5 Correct 68 ms 176420 KB Output is correct
6 Correct 68 ms 176360 KB Output is correct
7 Correct 96 ms 176488 KB Output is correct
8 Correct 96 ms 176556 KB Output is correct
9 Correct 301 ms 176496 KB Output is correct
10 Correct 366 ms 176712 KB Output is correct
11 Correct 346 ms 176728 KB Output is correct
12 Correct 325 ms 176592 KB Output is correct
13 Correct 281 ms 176752 KB Output is correct
14 Correct 244 ms 176696 KB Output is correct
15 Correct 222 ms 176544 KB Output is correct
16 Correct 224 ms 176608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 66 ms 176332 KB Output is correct
2 Correct 70 ms 176400 KB Output is correct
3 Correct 70 ms 176428 KB Output is correct
4 Correct 69 ms 176332 KB Output is correct
5 Correct 71 ms 176328 KB Output is correct
6 Correct 74 ms 176436 KB Output is correct
7 Correct 69 ms 176376 KB Output is correct
8 Correct 73 ms 176488 KB Output is correct
9 Correct 69 ms 176460 KB Output is correct
10 Correct 285 ms 176556 KB Output is correct
11 Correct 303 ms 176672 KB Output is correct
12 Correct 347 ms 176736 KB Output is correct
13 Correct 341 ms 176680 KB Output is correct
14 Correct 321 ms 176632 KB Output is correct
15 Correct 214 ms 176496 KB Output is correct
16 Correct 222 ms 176588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 66 ms 176332 KB Output is correct
2 Correct 70 ms 176400 KB Output is correct
3 Correct 70 ms 176428 KB Output is correct
4 Correct 69 ms 176332 KB Output is correct
5 Correct 71 ms 176328 KB Output is correct
6 Correct 74 ms 176436 KB Output is correct
7 Correct 69 ms 176376 KB Output is correct
8 Correct 73 ms 176488 KB Output is correct
9 Correct 69 ms 176460 KB Output is correct
10 Correct 285 ms 176556 KB Output is correct
11 Correct 303 ms 176672 KB Output is correct
12 Correct 347 ms 176736 KB Output is correct
13 Correct 341 ms 176680 KB Output is correct
14 Correct 321 ms 176632 KB Output is correct
15 Correct 214 ms 176496 KB Output is correct
16 Correct 222 ms 176588 KB Output is correct
17 Correct 321 ms 176676 KB Output is correct
18 Correct 76 ms 176408 KB Output is correct
19 Correct 67 ms 176332 KB Output is correct
20 Correct 68 ms 176372 KB Output is correct
21 Correct 67 ms 176416 KB Output is correct
22 Correct 69 ms 176352 KB Output is correct
23 Correct 77 ms 176460 KB Output is correct
24 Correct 83 ms 176444 KB Output is correct
25 Correct 97 ms 176456 KB Output is correct
26 Correct 80 ms 176452 KB Output is correct
27 Correct 339 ms 176468 KB Output is correct
28 Correct 372 ms 176660 KB Output is correct
29 Correct 371 ms 176676 KB Output is correct
30 Correct 339 ms 176660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 323 ms 176688 KB Output is correct
2 Correct 79 ms 176332 KB Output is correct
3 Correct 71 ms 176316 KB Output is correct
4 Correct 68 ms 176424 KB Output is correct
5 Correct 71 ms 176432 KB Output is correct
6 Correct 72 ms 176368 KB Output is correct
7 Correct 77 ms 176424 KB Output is correct
8 Correct 316 ms 176496 KB Output is correct
9 Correct 311 ms 176544 KB Output is correct
10 Correct 278 ms 176472 KB Output is correct
11 Correct 381 ms 176672 KB Output is correct
12 Correct 70 ms 176420 KB Output is correct
13 Correct 69 ms 176332 KB Output is correct
14 Correct 67 ms 176376 KB Output is correct
15 Correct 68 ms 176420 KB Output is correct
16 Correct 68 ms 176360 KB Output is correct
17 Correct 96 ms 176488 KB Output is correct
18 Correct 96 ms 176556 KB Output is correct
19 Correct 301 ms 176496 KB Output is correct
20 Correct 366 ms 176712 KB Output is correct
21 Correct 346 ms 176728 KB Output is correct
22 Correct 325 ms 176592 KB Output is correct
23 Correct 281 ms 176752 KB Output is correct
24 Correct 244 ms 176696 KB Output is correct
25 Correct 222 ms 176544 KB Output is correct
26 Correct 224 ms 176608 KB Output is correct
27 Correct 66 ms 176332 KB Output is correct
28 Correct 70 ms 176400 KB Output is correct
29 Correct 70 ms 176428 KB Output is correct
30 Correct 69 ms 176332 KB Output is correct
31 Correct 71 ms 176328 KB Output is correct
32 Correct 74 ms 176436 KB Output is correct
33 Correct 69 ms 176376 KB Output is correct
34 Correct 73 ms 176488 KB Output is correct
35 Correct 69 ms 176460 KB Output is correct
36 Correct 285 ms 176556 KB Output is correct
37 Correct 303 ms 176672 KB Output is correct
38 Correct 347 ms 176736 KB Output is correct
39 Correct 341 ms 176680 KB Output is correct
40 Correct 321 ms 176632 KB Output is correct
41 Correct 214 ms 176496 KB Output is correct
42 Correct 222 ms 176588 KB Output is correct
43 Correct 321 ms 176676 KB Output is correct
44 Correct 76 ms 176408 KB Output is correct
45 Correct 67 ms 176332 KB Output is correct
46 Correct 68 ms 176372 KB Output is correct
47 Correct 67 ms 176416 KB Output is correct
48 Correct 69 ms 176352 KB Output is correct
49 Correct 77 ms 176460 KB Output is correct
50 Correct 83 ms 176444 KB Output is correct
51 Correct 97 ms 176456 KB Output is correct
52 Correct 80 ms 176452 KB Output is correct
53 Correct 339 ms 176468 KB Output is correct
54 Correct 372 ms 176660 KB Output is correct
55 Correct 371 ms 176676 KB Output is correct
56 Correct 339 ms 176660 KB Output is correct
57 Correct 418 ms 176676 KB Output is correct
58 Correct 312 ms 176572 KB Output is correct
59 Correct 355 ms 176748 KB Output is correct