Submission #511263

# Submission time Handle Problem Language Result Execution time Memory
511263 2022-01-15T13:10:30 Z akhan42 Toll (BOI17_toll) C++17
17 / 100
275 ms 56568 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]);
				}
			}
		}
	}
//	cerr << "here2\n";
//
//	printt(100);
//	printt(1000);
//	printt(10000);
//	printt(25000);


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

		int diff = (b - 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 comp = 1;
//int att[MX];
//
//void dfs(int node) {
//	att[node] = comp;
//	for(ii nb_t: gr[node][0]) {
//		int nb = nb_t.F, t = nb_t.S;
//		if(att[nb]) {
//			cerr << "ERROR\n";
//			continue;
//		}
//		dfs(nb);
//	}
//}
//
//void solve2() {
//	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);
//	}
//
//	cerr << "here\n";
//
//	forn(a, n) {
////		cerr << a << ' ' << sz(gr[a][0]) << '\n';
//		if(!att[a]) {
//			dfs(a);
//			comp += 1;
//		}
//	}
//
//	cerr << "here\n";
//
//	forn(_, o) {
//		int a, b;
//		cin >> a >> b;
//		if(att[a] == att[b]) {
//			cout << "good\n";
//		} else {
//			cout << -1 << ' ' ;
//		}
//	}
//}


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();
	}
}
# Verdict Execution time Memory Grader output
1 Correct 130 ms 46796 KB Output is correct
2 Correct 12 ms 23756 KB Output is correct
3 Correct 15 ms 23740 KB Output is correct
4 Correct 15 ms 23756 KB Output is correct
5 Correct 13 ms 23944 KB Output is correct
6 Correct 16 ms 23812 KB Output is correct
7 Correct 17 ms 23936 KB Output is correct
8 Correct 151 ms 39664 KB Output is correct
9 Correct 81 ms 30912 KB Output is correct
10 Correct 56 ms 23824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 170 ms 43584 KB Output is correct
2 Correct 16 ms 23808 KB Output is correct
3 Correct 11 ms 23756 KB Output is correct
4 Correct 16 ms 23756 KB Output is correct
5 Correct 15 ms 23736 KB Output is correct
6 Correct 12 ms 23756 KB Output is correct
7 Correct 22 ms 24064 KB Output is correct
8 Correct 19 ms 24064 KB Output is correct
9 Correct 89 ms 39420 KB Output is correct
10 Correct 275 ms 56568 KB Output is correct
11 Correct 199 ms 46896 KB Output is correct
12 Correct 121 ms 33428 KB Output is correct
13 Correct 246 ms 53848 KB Output is correct
14 Correct 156 ms 42792 KB Output is correct
15 Correct 190 ms 48172 KB Output is correct
16 Correct 207 ms 47972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 23800 KB Output is correct
2 Incorrect 11 ms 23696 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 14 ms 23800 KB Output is correct
2 Incorrect 11 ms 23696 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 130 ms 46796 KB Output is correct
2 Correct 12 ms 23756 KB Output is correct
3 Correct 15 ms 23740 KB Output is correct
4 Correct 15 ms 23756 KB Output is correct
5 Correct 13 ms 23944 KB Output is correct
6 Correct 16 ms 23812 KB Output is correct
7 Correct 17 ms 23936 KB Output is correct
8 Correct 151 ms 39664 KB Output is correct
9 Correct 81 ms 30912 KB Output is correct
10 Correct 56 ms 23824 KB Output is correct
11 Correct 170 ms 43584 KB Output is correct
12 Correct 16 ms 23808 KB Output is correct
13 Correct 11 ms 23756 KB Output is correct
14 Correct 16 ms 23756 KB Output is correct
15 Correct 15 ms 23736 KB Output is correct
16 Correct 12 ms 23756 KB Output is correct
17 Correct 22 ms 24064 KB Output is correct
18 Correct 19 ms 24064 KB Output is correct
19 Correct 89 ms 39420 KB Output is correct
20 Correct 275 ms 56568 KB Output is correct
21 Correct 199 ms 46896 KB Output is correct
22 Correct 121 ms 33428 KB Output is correct
23 Correct 246 ms 53848 KB Output is correct
24 Correct 156 ms 42792 KB Output is correct
25 Correct 190 ms 48172 KB Output is correct
26 Correct 207 ms 47972 KB Output is correct
27 Correct 14 ms 23800 KB Output is correct
28 Incorrect 11 ms 23696 KB Output isn't correct
29 Halted 0 ms 0 KB -