답안 #291237

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
291237 2020-09-04T22:42:48 Z VROOM_VARUN Toll (BOI17_toll) C++14
100 / 100
872 ms 389496 KB
/*
ID: varunra2
LANG: C++
TASK: toll
*/

#include <bits/stdc++.h>
using namespace std;

#ifdef DEBUG
#include "lib/debug.h"
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define debug_arr(...) \
  cerr << "[" << #__VA_ARGS__ << "]:", debug_arr(__VA_ARGS__)
#pragma GCC diagnostic ignored "-Wsign-compare"
//#pragma GCC diagnostic ignored "-Wunused-parameter"
//#pragma GCC diagnostic ignored "-Wunused-variable"
#else
#define debug(...) 42
#endif

#define EPS 1e-9
#define IN(A, B, C) assert(B <= A && A <= C)
#define INF (int)1e9
#define MEM(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define MP make_pair
#define PB push_back
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define x first
#define y second

const double PI = acos(-1.0);
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef map<int, int> MPII;
typedef multiset<int> MSETI;
typedef set<int> SETI;
typedef set<string> SETS;
typedef vector<ll> VI;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef vector<VVI> VVVI;
typedef vector<VVVI> VVVVI;
typedef vector<string> VS;

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto& a : x)
#define sz(x) (int)(x).size()
typedef pair<int, int> pii;
typedef vector<int> vi;
#pragma GCC diagnostic ignored "-Wsign-compare"
// util functions

#define MAXN (int)5e4
#define MAXK (int)5
#define MLOG (int)20
int n, m, k, o;
VVVVI dp(MAXN, VVVI(MLOG, VVI(MAXK, VI(MAXK, INF))));

void init() {
  // rep(i, 0, MAXN) rep(j, 0, MLOG) rep(kk, 0, MAXK) rep(kkak, 0, MAXK)
      // dp[i][j][kk][kkak] = INF;
}

VVI floyd(VVI& a, VVI& b) {
  VVI ret(k, VI(k, INF));
  for (int i = 0; i < k; i++) {
    for (int j = 0; j < k; j++) {
      for (int kk = 0; kk < k; kk++) {
        ret[i][j] = min(ret[i][j], a[i][kk] + b[kk][j]);
      }
    }
  }
  return ret;
}

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);


  cin >> k >> n >> m >> o;

  init();


  for (int i = 0; i < m; i++) {
    int a, b, w;
    cin >> a >> b >> w;
    dp[a / k][0][a % k][b % k] = w;
  }

  for (int j = 1; j < 20; j++) {
    for (int i = 0; i < (n + k - 1) / k - (1 << j); i++) {
      dp[i][j] = floyd(dp[i][j - 1], dp[i + (1 << (j - 1))][j - 1]);
    }
  }

  for(int i = 0; i < o; i++) {
    int a, b;
    cin >> a >> b;
    VVI ret(k, VI(k, INF));
    for(int j = 0; j < k; j++) {
      ret[j][j] = 0;
    }
    int from, to;
    from = a/k;
    to = b/k;
    for(int i = MLOG - 1; i >= 0; i--) {
      if(from + (1 << i) <= to) {
        ret = floyd(ret, dp[from][i]);
        from += (1 << i);
      }
    }
    if(ret[a%k][b%k] == INF) cout << -1 << '\n';
    else cout << ret[a%k][b%k] << '\n';
  }

  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 864 ms 386936 KB Output is correct
2 Correct 460 ms 385912 KB Output is correct
3 Correct 477 ms 386040 KB Output is correct
4 Correct 473 ms 385792 KB Output is correct
5 Correct 465 ms 385912 KB Output is correct
6 Correct 466 ms 385912 KB Output is correct
7 Correct 496 ms 386040 KB Output is correct
8 Correct 868 ms 386936 KB Output is correct
9 Correct 860 ms 387068 KB Output is correct
10 Correct 833 ms 386276 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 768 ms 387832 KB Output is correct
2 Correct 456 ms 385912 KB Output is correct
3 Correct 460 ms 385948 KB Output is correct
4 Correct 461 ms 385924 KB Output is correct
5 Correct 462 ms 385884 KB Output is correct
6 Correct 465 ms 385912 KB Output is correct
7 Correct 474 ms 386040 KB Output is correct
8 Correct 478 ms 386168 KB Output is correct
9 Correct 858 ms 386936 KB Output is correct
10 Correct 722 ms 388344 KB Output is correct
11 Correct 772 ms 387704 KB Output is correct
12 Correct 693 ms 387448 KB Output is correct
13 Correct 576 ms 388344 KB Output is correct
14 Correct 591 ms 387448 KB Output is correct
15 Correct 553 ms 387176 KB Output is correct
16 Correct 545 ms 387192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 456 ms 385912 KB Output is correct
2 Correct 458 ms 386032 KB Output is correct
3 Correct 462 ms 385912 KB Output is correct
4 Correct 458 ms 385912 KB Output is correct
5 Correct 458 ms 386040 KB Output is correct
6 Correct 461 ms 385912 KB Output is correct
7 Correct 464 ms 385980 KB Output is correct
8 Correct 465 ms 385912 KB Output is correct
9 Correct 464 ms 386168 KB Output is correct
10 Correct 827 ms 386940 KB Output is correct
11 Correct 722 ms 387576 KB Output is correct
12 Correct 689 ms 388132 KB Output is correct
13 Correct 709 ms 388372 KB Output is correct
14 Correct 681 ms 388024 KB Output is correct
15 Correct 542 ms 387064 KB Output is correct
16 Correct 539 ms 387064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 456 ms 385912 KB Output is correct
2 Correct 458 ms 386032 KB Output is correct
3 Correct 462 ms 385912 KB Output is correct
4 Correct 458 ms 385912 KB Output is correct
5 Correct 458 ms 386040 KB Output is correct
6 Correct 461 ms 385912 KB Output is correct
7 Correct 464 ms 385980 KB Output is correct
8 Correct 465 ms 385912 KB Output is correct
9 Correct 464 ms 386168 KB Output is correct
10 Correct 827 ms 386940 KB Output is correct
11 Correct 722 ms 387576 KB Output is correct
12 Correct 689 ms 388132 KB Output is correct
13 Correct 709 ms 388372 KB Output is correct
14 Correct 681 ms 388024 KB Output is correct
15 Correct 542 ms 387064 KB Output is correct
16 Correct 539 ms 387064 KB Output is correct
17 Correct 728 ms 387768 KB Output is correct
18 Correct 462 ms 385860 KB Output is correct
19 Correct 458 ms 385912 KB Output is correct
20 Correct 456 ms 385912 KB Output is correct
21 Correct 459 ms 385912 KB Output is correct
22 Correct 461 ms 385872 KB Output is correct
23 Correct 470 ms 386168 KB Output is correct
24 Correct 466 ms 385992 KB Output is correct
25 Correct 468 ms 386168 KB Output is correct
26 Correct 464 ms 386040 KB Output is correct
27 Correct 832 ms 386936 KB Output is correct
28 Correct 698 ms 388236 KB Output is correct
29 Correct 710 ms 388604 KB Output is correct
30 Correct 692 ms 387960 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 864 ms 386936 KB Output is correct
2 Correct 460 ms 385912 KB Output is correct
3 Correct 477 ms 386040 KB Output is correct
4 Correct 473 ms 385792 KB Output is correct
5 Correct 465 ms 385912 KB Output is correct
6 Correct 466 ms 385912 KB Output is correct
7 Correct 496 ms 386040 KB Output is correct
8 Correct 868 ms 386936 KB Output is correct
9 Correct 860 ms 387068 KB Output is correct
10 Correct 833 ms 386276 KB Output is correct
11 Correct 768 ms 387832 KB Output is correct
12 Correct 456 ms 385912 KB Output is correct
13 Correct 460 ms 385948 KB Output is correct
14 Correct 461 ms 385924 KB Output is correct
15 Correct 462 ms 385884 KB Output is correct
16 Correct 465 ms 385912 KB Output is correct
17 Correct 474 ms 386040 KB Output is correct
18 Correct 478 ms 386168 KB Output is correct
19 Correct 858 ms 386936 KB Output is correct
20 Correct 722 ms 388344 KB Output is correct
21 Correct 772 ms 387704 KB Output is correct
22 Correct 693 ms 387448 KB Output is correct
23 Correct 576 ms 388344 KB Output is correct
24 Correct 591 ms 387448 KB Output is correct
25 Correct 553 ms 387176 KB Output is correct
26 Correct 545 ms 387192 KB Output is correct
27 Correct 456 ms 385912 KB Output is correct
28 Correct 458 ms 386032 KB Output is correct
29 Correct 462 ms 385912 KB Output is correct
30 Correct 458 ms 385912 KB Output is correct
31 Correct 458 ms 386040 KB Output is correct
32 Correct 461 ms 385912 KB Output is correct
33 Correct 464 ms 385980 KB Output is correct
34 Correct 465 ms 385912 KB Output is correct
35 Correct 464 ms 386168 KB Output is correct
36 Correct 827 ms 386940 KB Output is correct
37 Correct 722 ms 387576 KB Output is correct
38 Correct 689 ms 388132 KB Output is correct
39 Correct 709 ms 388372 KB Output is correct
40 Correct 681 ms 388024 KB Output is correct
41 Correct 542 ms 387064 KB Output is correct
42 Correct 539 ms 387064 KB Output is correct
43 Correct 728 ms 387768 KB Output is correct
44 Correct 462 ms 385860 KB Output is correct
45 Correct 458 ms 385912 KB Output is correct
46 Correct 456 ms 385912 KB Output is correct
47 Correct 459 ms 385912 KB Output is correct
48 Correct 461 ms 385872 KB Output is correct
49 Correct 470 ms 386168 KB Output is correct
50 Correct 466 ms 385992 KB Output is correct
51 Correct 468 ms 386168 KB Output is correct
52 Correct 464 ms 386040 KB Output is correct
53 Correct 832 ms 386936 KB Output is correct
54 Correct 698 ms 388236 KB Output is correct
55 Correct 710 ms 388604 KB Output is correct
56 Correct 692 ms 387960 KB Output is correct
57 Correct 751 ms 389496 KB Output is correct
58 Correct 872 ms 387064 KB Output is correct
59 Correct 754 ms 387832 KB Output is correct