Submission #511263

#TimeUsernameProblemLanguageResultExecution timeMemory
511263akhan42Toll (BOI17_toll)C++17
17 / 100
275 ms56568 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...