답안 #511264

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
511264 2022-01-15T13:13:45 Z akhan42 Toll (BOI17_toll) C++17
100 / 100
313 ms 57228 KB
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
//using namespace __gnu_pbds;

#define F first
#define S second
#define forn(i, n) for(int i = 0; i < n; ++i)
#define forbn(i, b, n) for(int i = b; i < n; ++i)
#define sz(v) (int)v.size()
#define pb push_back
#define eb emplace_back
#define all(v) v.begin(), v.end()
#define mp make_pair

typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef long long ll;
//typedef tree<ii, null_type, less<ii>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

template <class T> inline void mineq(T &a, T b) { a = min(a, b); }
template <class T> inline void maxeq(T &a, T b) { a = max(a, b); }


const int INF = 1000 * 1000 * 1000;
const int MX = 50 * 1000 + 10;
const int mxpw = 20;

vii gr[MX][mxpw];


void printt(int i) {
	cerr << i << '\n';
	forn(d, mxpw) {
		cerr << gr[i][d][0].F << ' ' << gr[i][d][0].S << '\n';
	}
}

void solve() {
	int k, n, m, o;
	cin >> k >> n >> m >> o;
	forn(_, m) {
		int a, b, t;
		cin >> a >> b >> t;

		gr[a][0].eb(b, t);
	}

	forbn(d, 1, mxpw) {
		forn(a, n) {
			vi mns(k, INF);
			int fac = -1;
			for(ii nb_t: gr[a][d - 1]) {
				int nb = nb_t.F, t = nb_t.S;
				for(ii nb2_t: gr[nb][d - 1]) {
					int nb2 = nb2_t.F, t2 = nb2_t.S;
					mineq(mns[nb2 % k], t + t2);
					fac = nb2 / k;
				}
			}

			forn(t, k) {
				if(mns[t] != INF) {
					gr[a][d].eb(k * fac + t, mns[t]);
				}
			}
		}
	}

	forn(_, o) {
		int a, b;
		cin >> a >> b;

		int diff = b / k - a / k;
		if(diff >= 0) {
			vii positions;
			positions.eb(a, 0);

			for(int pw = 0; diff; pw++, diff >>= 1) {
				if(diff & 1)
				{
					vi mns(k, INF);
					int fac = -1;
					for(ii pos_t: positions) {
						int pos = pos_t.F, t = pos_t.S;
						for(ii nb2_t: gr[pos][pw]) {
							int nb2 = nb2_t.F, t2 = nb2_t.S;

							mineq(mns[nb2 % k], t + t2);
							fac = nb2 / k;
						}
					}
					positions.clear();
					forn(t, k) {
						if(mns[t] != INF) {
							positions.eb(k * fac + t, mns[t]);
						}
					}
				}
			}

			bool printed = false;
			for(ii pos: positions) {
				if(pos.F == b) {
					cout << pos.S << '\n';
					printed = true;
				}
			}
			if(!printed)
				cout << -1 << '\n';
		} else {
			cout << -1 << '\n';
		}
	}
}


int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
//	cout.tie(nullptr);

//	freopen("262144.in", "r", stdin);
//	freopen("262144.out", "w", stdout);

	int t = 1;
//	cin >> t;
	while(t--) {
		solve();
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 125 ms 46828 KB Output is correct
2 Correct 12 ms 23756 KB Output is correct
3 Correct 12 ms 23784 KB Output is correct
4 Correct 13 ms 23748 KB Output is correct
5 Correct 15 ms 23900 KB Output is correct
6 Correct 13 ms 23884 KB Output is correct
7 Correct 13 ms 23884 KB Output is correct
8 Correct 100 ms 38636 KB Output is correct
9 Correct 83 ms 29936 KB Output is correct
10 Correct 35 ms 23808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 160 ms 43648 KB Output is correct
2 Correct 12 ms 23756 KB Output is correct
3 Correct 11 ms 23756 KB Output is correct
4 Correct 12 ms 23756 KB Output is correct
5 Correct 12 ms 23756 KB Output is correct
6 Correct 12 ms 23732 KB Output is correct
7 Correct 16 ms 23888 KB Output is correct
8 Correct 23 ms 24100 KB Output is correct
9 Correct 89 ms 38704 KB Output is correct
10 Correct 237 ms 54312 KB Output is correct
11 Correct 163 ms 45204 KB Output is correct
12 Correct 133 ms 32152 KB Output is correct
13 Correct 230 ms 51368 KB Output is correct
14 Correct 144 ms 41380 KB Output is correct
15 Correct 184 ms 46824 KB Output is correct
16 Correct 174 ms 46752 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23756 KB Output is correct
2 Correct 11 ms 23756 KB Output is correct
3 Correct 11 ms 23784 KB Output is correct
4 Correct 12 ms 23756 KB Output is correct
5 Correct 14 ms 23788 KB Output is correct
6 Correct 16 ms 23928 KB Output is correct
7 Correct 14 ms 23936 KB Output is correct
8 Correct 15 ms 24264 KB Output is correct
9 Correct 15 ms 24140 KB Output is correct
10 Correct 93 ms 39620 KB Output is correct
11 Correct 162 ms 46776 KB Output is correct
12 Correct 231 ms 56396 KB Output is correct
13 Correct 242 ms 57060 KB Output is correct
14 Correct 208 ms 54584 KB Output is correct
15 Correct 189 ms 48156 KB Output is correct
16 Correct 174 ms 47908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23756 KB Output is correct
2 Correct 11 ms 23756 KB Output is correct
3 Correct 11 ms 23784 KB Output is correct
4 Correct 12 ms 23756 KB Output is correct
5 Correct 14 ms 23788 KB Output is correct
6 Correct 16 ms 23928 KB Output is correct
7 Correct 14 ms 23936 KB Output is correct
8 Correct 15 ms 24264 KB Output is correct
9 Correct 15 ms 24140 KB Output is correct
10 Correct 93 ms 39620 KB Output is correct
11 Correct 162 ms 46776 KB Output is correct
12 Correct 231 ms 56396 KB Output is correct
13 Correct 242 ms 57060 KB Output is correct
14 Correct 208 ms 54584 KB Output is correct
15 Correct 189 ms 48156 KB Output is correct
16 Correct 174 ms 47908 KB Output is correct
17 Correct 189 ms 46672 KB Output is correct
18 Correct 12 ms 23756 KB Output is correct
19 Correct 12 ms 23756 KB Output is correct
20 Correct 12 ms 23756 KB Output is correct
21 Correct 11 ms 23800 KB Output is correct
22 Correct 12 ms 23756 KB Output is correct
23 Correct 14 ms 23944 KB Output is correct
24 Correct 15 ms 24124 KB Output is correct
25 Correct 21 ms 24372 KB Output is correct
26 Correct 18 ms 24200 KB Output is correct
27 Correct 78 ms 39460 KB Output is correct
28 Correct 235 ms 56508 KB Output is correct
29 Correct 233 ms 57156 KB Output is correct
30 Correct 211 ms 52448 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 125 ms 46828 KB Output is correct
2 Correct 12 ms 23756 KB Output is correct
3 Correct 12 ms 23784 KB Output is correct
4 Correct 13 ms 23748 KB Output is correct
5 Correct 15 ms 23900 KB Output is correct
6 Correct 13 ms 23884 KB Output is correct
7 Correct 13 ms 23884 KB Output is correct
8 Correct 100 ms 38636 KB Output is correct
9 Correct 83 ms 29936 KB Output is correct
10 Correct 35 ms 23808 KB Output is correct
11 Correct 160 ms 43648 KB Output is correct
12 Correct 12 ms 23756 KB Output is correct
13 Correct 11 ms 23756 KB Output is correct
14 Correct 12 ms 23756 KB Output is correct
15 Correct 12 ms 23756 KB Output is correct
16 Correct 12 ms 23732 KB Output is correct
17 Correct 16 ms 23888 KB Output is correct
18 Correct 23 ms 24100 KB Output is correct
19 Correct 89 ms 38704 KB Output is correct
20 Correct 237 ms 54312 KB Output is correct
21 Correct 163 ms 45204 KB Output is correct
22 Correct 133 ms 32152 KB Output is correct
23 Correct 230 ms 51368 KB Output is correct
24 Correct 144 ms 41380 KB Output is correct
25 Correct 184 ms 46824 KB Output is correct
26 Correct 174 ms 46752 KB Output is correct
27 Correct 12 ms 23756 KB Output is correct
28 Correct 11 ms 23756 KB Output is correct
29 Correct 11 ms 23784 KB Output is correct
30 Correct 12 ms 23756 KB Output is correct
31 Correct 14 ms 23788 KB Output is correct
32 Correct 16 ms 23928 KB Output is correct
33 Correct 14 ms 23936 KB Output is correct
34 Correct 15 ms 24264 KB Output is correct
35 Correct 15 ms 24140 KB Output is correct
36 Correct 93 ms 39620 KB Output is correct
37 Correct 162 ms 46776 KB Output is correct
38 Correct 231 ms 56396 KB Output is correct
39 Correct 242 ms 57060 KB Output is correct
40 Correct 208 ms 54584 KB Output is correct
41 Correct 189 ms 48156 KB Output is correct
42 Correct 174 ms 47908 KB Output is correct
43 Correct 189 ms 46672 KB Output is correct
44 Correct 12 ms 23756 KB Output is correct
45 Correct 12 ms 23756 KB Output is correct
46 Correct 12 ms 23756 KB Output is correct
47 Correct 11 ms 23800 KB Output is correct
48 Correct 12 ms 23756 KB Output is correct
49 Correct 14 ms 23944 KB Output is correct
50 Correct 15 ms 24124 KB Output is correct
51 Correct 21 ms 24372 KB Output is correct
52 Correct 18 ms 24200 KB Output is correct
53 Correct 78 ms 39460 KB Output is correct
54 Correct 235 ms 56508 KB Output is correct
55 Correct 233 ms 57156 KB Output is correct
56 Correct 211 ms 52448 KB Output is correct
57 Correct 313 ms 57228 KB Output is correct
58 Correct 93 ms 39492 KB Output is correct
59 Correct 186 ms 47060 KB Output is correct