Submission #1093568

#TimeUsernameProblemLanguageResultExecution timeMemory
1093568ThanhsToll (BOI17_toll)C++14
0 / 100
3073 ms80212 KiB
#include <bits/stdc++.h> using namespace std; #define fi first #define se second // #define int long long #define endl '\n' #define setmin(x, y) x = ((x) < (y) ? (x) : (y)) #define setmax(x, y) x = ((x) > (y) ? (x) : (y)) mt19937 hdp(chrono::high_resolution_clock::now().time_since_epoch().count()); int rand(int l, int r){return l + ((hdp() % (r - l + 1)) + r - l + 1) % (r - l + 1);} const int NM = 5e4 + 5; const int LG = 16; const int inf = 1e9; int n, m, q, k; struct node { int dt[5][5]; node() {for (int i = 0; i < 5; i++) for (int j = 0; j < 5; j++) dt[i][j] = inf;} node operator*(const node& o) { node res; for (int i = 0; i < k; i++) for (int j = 0; j < k; j++) for (int t = 0; t < k; t++) setmin(res.dt[i][j], dt[i][t] + o.dt[t][j]); return res; } }st[NM][LG]; void solve() { cin >> k >> n >> m >> q; for (int i = 1; i <= m; i++) { int u, v, w; cin >> u >> v >> w; setmin(st[u / k][0].dt[u % k][v % k], w); } for (int lg = 1; lg <= __lg(n / k + 1); lg++) for (int i = 0; i + (1 << lg) - 1 < n / k; i++) st[i][lg] = st[i][lg - 1] * st[i + (1 << lg - 1)][lg - 1]; while (q--) { int u, v; cin >> u >> v; if (u == v) cout << 0 << endl; else if (u / k < v / k) { int x = v / k - u / k; node res = st[u / k][__lg(x)]; int t = x - (1 << __lg(x)), cur = u / k + (1 << __lg(x)); for (int i = __lg(t); i >= 0; i--) if (t & (1 << i)) { res = res * st[cur][i]; cur += 1 << i; } cout << (res.dt[u % k][v % k] == inf ? -1 : res.dt[u % k][v % k]) << endl; } else cout << -1 << endl; } } signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); if (fopen("in.txt", "r")) { freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); } // freopen("storagebox.inp", "r", stdin); // freopen("storagebox.out", "w", stdout); int tc = 1; // cin >> tc; while (tc--) solve(); }

Compilation message (stderr)

toll.cpp: In function 'void solve()':
toll.cpp:46:57: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   46 |             st[i][lg] = st[i][lg - 1] * st[i + (1 << lg - 1)][lg - 1];
      |                                                      ~~~^~~
toll.cpp: In function 'int main()':
toll.cpp:76:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   76 |         freopen("in.txt", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
toll.cpp:77:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   77 |         freopen("out.txt", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#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...