Submission #291237

#TimeUsernameProblemLanguageResultExecution timeMemory
291237VROOM_VARUNToll (BOI17_toll)C++14
100 / 100
872 ms389496 KiB
/*
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;
}
#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...