Submission #760306

# Submission time Handle Problem Language Result Execution time Memory
760306 2023-06-17T12:13:30 Z mgl_diamond Toll (BOI17_toll) C++17
100 / 100
71 ms 10108 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
 
#define foru(i, l, r) for(int i=(l); i<=(r); ++i)
#define ford(i, l, r) for(int i=(l; i>=(r); --i)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)x.size()
#define fi first
#define se second
 
const int INF = 1e9;
const int N = 50005;
const int LOG = 17;
using dt = pair<ii, int>;
 
int k, n, m, o, ret[N];
int f[5][N], g[5][N];
vector<ii> go[N], back[N];
vector<dt> querys;
 
void divide(int lx, int rx, vector<dt> &ask) {
  if (ask.empty()) return;
 
  int mx = (lx+rx)/2;
  int st = lx*k, en = min(n, (rx+1)*k);
  
  for(int t=0; t<k; ++t) {
    for(int i=st; i<en; ++i) {
      f[t][i] = g[t][i] = INF;
    }
  }
 
  int l = mx*k, r = min(n, l+k);  
 
  for(int m=l; m<r; ++m) {
    f[m-l][m] = g[m-l][m] = 0;
 
    for(int i=m; i>st; --i) {
      for(ii x : back[i]) {
        f[m-l][x.fi] = min(f[m-l][x.fi], f[m-l][i] + x.se);
      }
    }
 
    for(int i=m; i<en-1; ++i) {
      for(ii x : go[i]) {
        g[m-l][x.fi] = min(g[m-l][x.fi], g[m-l][i] + x.se);
      }
    }
  }
 
  vector<dt> _left, _right;
  for(auto x : ask) {
    if (x.fi.se/k < mx) _left.emplace_back(x);
    else if (x.fi.fi/k > mx) _right.emplace_back(x);
    else {
      for(int m=l; m<r; ++m) {
        ret[x.se] = min(ret[x.se], f[m-l][x.fi.fi] + g[m-l][x.fi.se]);
      }
    }
  }
 
  divide(lx, mx-1, _left);
  divide(mx+1, rx, _right);
}
 
void solve() {
  cin >> k >> n >> m >> o;
 
  for(int i=1; i<=m; ++i) {
    int u, v, t;
    cin >> u >> v >> t;
 
    go[u].emplace_back(v, t);
    back[v].emplace_back(u, t);
  }
 
  for(int i=1; i<=o; ++i) {
    int u, v;
    cin >> u >> v;
 
    querys.push_back({{u, v}, i});
  }
 
  memset(ret, 0x3f, sizeof(ret));
  divide(0, (n-1)/k, querys);
 
  for(int i=1; i<=o; ++i) {
    if (ret[i] < INF) cout << ret[i] << "\n";
    else cout << "-1\n";
  }
}
 
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);
 
  if (fopen("input.inp", "r")) {
    freopen("input.inp", "r", stdin);
    freopen("input.out", "w", stdout);
  }
 
  solve();
}

Compilation message

toll.cpp: In function 'int main()':
toll.cpp:100:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  100 |     freopen("input.inp", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:101:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  101 |     freopen("input.out", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 21 ms 6656 KB Output is correct
2 Correct 2 ms 2792 KB Output is correct
3 Correct 1 ms 2772 KB Output is correct
4 Correct 1 ms 2772 KB Output is correct
5 Correct 2 ms 2900 KB Output is correct
6 Correct 2 ms 2900 KB Output is correct
7 Correct 2 ms 2900 KB Output is correct
8 Correct 18 ms 6668 KB Output is correct
9 Correct 18 ms 6516 KB Output is correct
10 Correct 4 ms 3480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 7072 KB Output is correct
2 Correct 1 ms 2772 KB Output is correct
3 Correct 1 ms 2772 KB Output is correct
4 Correct 1 ms 2772 KB Output is correct
5 Correct 2 ms 2900 KB Output is correct
6 Correct 2 ms 2900 KB Output is correct
7 Correct 3 ms 3224 KB Output is correct
8 Correct 4 ms 3256 KB Output is correct
9 Correct 30 ms 6676 KB Output is correct
10 Correct 46 ms 9216 KB Output is correct
11 Correct 32 ms 7084 KB Output is correct
12 Correct 25 ms 7212 KB Output is correct
13 Correct 50 ms 9528 KB Output is correct
14 Correct 28 ms 6868 KB Output is correct
15 Correct 25 ms 6804 KB Output is correct
16 Correct 26 ms 6724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2772 KB Output is correct
2 Correct 2 ms 2772 KB Output is correct
3 Correct 1 ms 2772 KB Output is correct
4 Correct 1 ms 2900 KB Output is correct
5 Correct 1 ms 2900 KB Output is correct
6 Correct 2 ms 2936 KB Output is correct
7 Correct 2 ms 2972 KB Output is correct
8 Correct 3 ms 3156 KB Output is correct
9 Correct 2 ms 3028 KB Output is correct
10 Correct 16 ms 6396 KB Output is correct
11 Correct 30 ms 6688 KB Output is correct
12 Correct 45 ms 8964 KB Output is correct
13 Correct 49 ms 9420 KB Output is correct
14 Correct 38 ms 8148 KB Output is correct
15 Correct 27 ms 6608 KB Output is correct
16 Correct 27 ms 6632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2772 KB Output is correct
2 Correct 2 ms 2772 KB Output is correct
3 Correct 1 ms 2772 KB Output is correct
4 Correct 1 ms 2900 KB Output is correct
5 Correct 1 ms 2900 KB Output is correct
6 Correct 2 ms 2936 KB Output is correct
7 Correct 2 ms 2972 KB Output is correct
8 Correct 3 ms 3156 KB Output is correct
9 Correct 2 ms 3028 KB Output is correct
10 Correct 16 ms 6396 KB Output is correct
11 Correct 30 ms 6688 KB Output is correct
12 Correct 45 ms 8964 KB Output is correct
13 Correct 49 ms 9420 KB Output is correct
14 Correct 38 ms 8148 KB Output is correct
15 Correct 27 ms 6608 KB Output is correct
16 Correct 27 ms 6632 KB Output is correct
17 Correct 32 ms 6856 KB Output is correct
18 Correct 1 ms 2772 KB Output is correct
19 Correct 1 ms 2772 KB Output is correct
20 Correct 1 ms 2772 KB Output is correct
21 Correct 1 ms 2900 KB Output is correct
22 Correct 1 ms 2900 KB Output is correct
23 Correct 2 ms 3028 KB Output is correct
24 Correct 4 ms 3028 KB Output is correct
25 Correct 3 ms 3156 KB Output is correct
26 Correct 3 ms 3028 KB Output is correct
27 Correct 17 ms 6484 KB Output is correct
28 Correct 46 ms 8980 KB Output is correct
29 Correct 53 ms 9440 KB Output is correct
30 Correct 40 ms 8316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 6656 KB Output is correct
2 Correct 2 ms 2792 KB Output is correct
3 Correct 1 ms 2772 KB Output is correct
4 Correct 1 ms 2772 KB Output is correct
5 Correct 2 ms 2900 KB Output is correct
6 Correct 2 ms 2900 KB Output is correct
7 Correct 2 ms 2900 KB Output is correct
8 Correct 18 ms 6668 KB Output is correct
9 Correct 18 ms 6516 KB Output is correct
10 Correct 4 ms 3480 KB Output is correct
11 Correct 29 ms 7072 KB Output is correct
12 Correct 1 ms 2772 KB Output is correct
13 Correct 1 ms 2772 KB Output is correct
14 Correct 1 ms 2772 KB Output is correct
15 Correct 2 ms 2900 KB Output is correct
16 Correct 2 ms 2900 KB Output is correct
17 Correct 3 ms 3224 KB Output is correct
18 Correct 4 ms 3256 KB Output is correct
19 Correct 30 ms 6676 KB Output is correct
20 Correct 46 ms 9216 KB Output is correct
21 Correct 32 ms 7084 KB Output is correct
22 Correct 25 ms 7212 KB Output is correct
23 Correct 50 ms 9528 KB Output is correct
24 Correct 28 ms 6868 KB Output is correct
25 Correct 25 ms 6804 KB Output is correct
26 Correct 26 ms 6724 KB Output is correct
27 Correct 1 ms 2772 KB Output is correct
28 Correct 2 ms 2772 KB Output is correct
29 Correct 1 ms 2772 KB Output is correct
30 Correct 1 ms 2900 KB Output is correct
31 Correct 1 ms 2900 KB Output is correct
32 Correct 2 ms 2936 KB Output is correct
33 Correct 2 ms 2972 KB Output is correct
34 Correct 3 ms 3156 KB Output is correct
35 Correct 2 ms 3028 KB Output is correct
36 Correct 16 ms 6396 KB Output is correct
37 Correct 30 ms 6688 KB Output is correct
38 Correct 45 ms 8964 KB Output is correct
39 Correct 49 ms 9420 KB Output is correct
40 Correct 38 ms 8148 KB Output is correct
41 Correct 27 ms 6608 KB Output is correct
42 Correct 27 ms 6632 KB Output is correct
43 Correct 32 ms 6856 KB Output is correct
44 Correct 1 ms 2772 KB Output is correct
45 Correct 1 ms 2772 KB Output is correct
46 Correct 1 ms 2772 KB Output is correct
47 Correct 1 ms 2900 KB Output is correct
48 Correct 1 ms 2900 KB Output is correct
49 Correct 2 ms 3028 KB Output is correct
50 Correct 4 ms 3028 KB Output is correct
51 Correct 3 ms 3156 KB Output is correct
52 Correct 3 ms 3028 KB Output is correct
53 Correct 17 ms 6484 KB Output is correct
54 Correct 46 ms 8980 KB Output is correct
55 Correct 53 ms 9440 KB Output is correct
56 Correct 40 ms 8316 KB Output is correct
57 Correct 71 ms 10108 KB Output is correct
58 Correct 22 ms 6676 KB Output is correct
59 Correct 39 ms 7064 KB Output is correct