Submission #1115909

#TimeUsernameProblemLanguageResultExecution timeMemory
1115909nathan4690Toll (BOI17_toll)C++17
100 / 100
142 ms14408 KiB
#include <bits/stdc++.h> #define ll long long #define ld long double #define el cout << '\n' #define f1(i,n) for(int i=1;i<=n;i++) #define __file_name "" using namespace std; const ll maxn = 5e4+5, inf=1e18; struct Matrix{ int r, c; vector<vector<ll>> data; Matrix(){}; Matrix(int r, int c): r(r), c(c), data(r, vector<ll>(c, 1e9)){}; Matrix(vector<vector<ll>> a): r(a.size()), c(a.empty() ? 0 : a[0].size()), data(a){}; Matrix operator*(const Matrix &rhs) const{ assert(c == rhs.r); Matrix res(r, rhs.c); // cout << (*this) << "TIMES\n" << rhs;el; for(int i=0;i<r;i++){ for(int j=0;j<rhs.c;j++){ for(int k=0;k<c;k++){ res.data[i][j] = min(res.data[i][j], data[i][k] + rhs.data[k][j]); } } } return res; } Matrix operator^(const long long &e) const{ assert(r == c); Matrix res(r,c), base(*this); for(int i=0;i<r;i++) res.data[i][i] = 1; long long _e = e; while(_e){ if(_e & 1) res = res * base; base = base * base; _e >>= 1; } return res; } int get(int _r, int _c){ // 1-indexed return data[_r-1][_c-1]; } friend ostream& operator<<(ostream& ouf, const Matrix &rhs){ for(int i=0;i<rhs.r;i++){ for(int j=0;j<rhs.c;j++){ ouf << rhs.data[i][j] << ' '; } ouf << '\n'; } return ouf; } }; Matrix st[4*maxn]; int k, n, m, o, id[maxn]; void build(int idx, int l, int r, bool flag = true){ if(l == r){ if(flag){ id[l] = idx; st[idx] = Matrix(k, k); // f1(i,k) f1(j,k) st[idx].data[i-1][j-1] = 1e9; } return; } int mid = (l + r) / 2; build(idx*2, l, mid, flag); build(idx*2+1, mid+1, r, flag); st[idx] = st[idx*2] * st[idx*2+1]; } Matrix get(int idx, int l, int r, int u, int v){ if(v < l || r < u) { Matrix res(k, k); f1(i, k) f1(j, k){ if(i == j) res.data[i-1][j-1] = 0; } return res; } if(u <= l && r <= v) return st[idx]; int mid = (l + r) / 2; return get(idx*2, l, mid, u, v) * get(idx*2+1, mid+1, r, u, v); } int main(){ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); if(fopen(__file_name ".inp", "r")){ freopen(__file_name ".inp","r",stdin); freopen(__file_name ".out","w",stdout); } // code here cin >> k >> n >> m >> o; build(1, 0, n / k); f1(i,m){ int a, b, t; cin >> a >> b >> t; int bl = a/k; // cout << id[bl] << ' ' << a % k << ' ' << b % k << ' ' << t;el; st[id[bl]].data[a % k][b % k] = min((int)st[id[bl]].data[a % k][b % k], t); } build(1, 0, n / k, false); // cout << "BUILD COMPLETE\n\n"; // for(int i=0;i<=n/k;i++) cout << st[id[i]] << "\n\n"; while(o--){ int a, b; cin >> a >> b; if(a / k == b / k){ if(a == b) cout << 0; else cout << -1; el; continue; } int bla = a / k, blb = b / k; Matrix res = Matrix(1, k); for(int i=0;i<k;i++) res.data[0][i] = 1e9; res.data[0][a % k] = 0; // for(int b=bla;b<blb;b++) { // res = res * st[id[b]]; // cout << b << ' ' << id[b];el; // for(int i=0;i<k;i++) cout << res.data[0][i] << ' ';el; // } // for(int i=0;i<k;i++) cout << get(1,0,n/k,bla,blb-1).data[0][i] << ' ';el; res = res * get(1,0,n/k,bla,blb-1); // for(int i=0;i<k;i++) cout << get(1,0,n/k,bla,blb-1).data[0][i] << ' ';el; if(res.data[0][b % k] >= 1e9) cout << -1; else cout << res.data[0][b % k]; el; } return 0; }

Compilation message (stderr)

toll.cpp: In function 'int main()':
toll.cpp:92:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   92 |         freopen(__file_name ".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:93:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   93 |         freopen(__file_name ".out","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...