Submission #170912

# Submission time Handle Problem Language Result Execution time Memory
170912 2019-12-26T17:13:15 Z Rods Toll (BOI17_toll) C++14
0 / 100
3000 ms 88184 KB
#include <bits/stdc++.h>
#define endl '\n'
#define all(x) x.begin(),x.end()
#define sz(x) ((int)x.size())
#define x first
#define y second
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define ll long long
#define int ll
#define ld long double
#define ii pair<int, int>
#define iii pair<int, ii>
#define vi vector<int>
#define vii vector<ii>
using namespace std;
const int ms = 5e4+100;
const int inf = 1e15;
const int sigma = 8;
const int mlg = 23;
int n, m, k, t, q;

vii g[ms];
int dis[ms][sigma];
int adj[ms];
priority_queue<ii, vector<ii>, greater<ii>> pq;

int par[ms][mlg+1][sigma], lvl[ms][sigma];

void dfs(int v, int p, int i, int l = 0) { // chamar como dfs(root, root)
  lvl[v][i] = l;
  par[v][0][i] = p;
  for(int k = 1; k <= mlg; k++) {
    par[v][k][i] = par[par[v][k-1][i]][k-1][i];
  }
  for(auto u : g[v]) {
    dfs(u.x, v, l + 1);
  }
}

int lca(int a, int b, int p) {
  if(lvl[b][p] > lvl[a][p]) swap(a, b);
  for(int i = mlg; i >= 0; i--) {
    if(lvl[a][p] - (1 << i) >= lvl[b][p]) a = par[a][i][p];
  }
  if(a == b) return a;
  for(int i = mlg; i >= 0; i--) {
    if(par[a][i][p] != par[b][i][p]) a = par[a][i][p], b = par[b][i][p];
  }
  return par[a][0][p];
}

void dijkstra(int x, int p) {
  dis[x][p] = 0;
  pq.push(ii(0, x));
  while(!pq.empty()) {
    ii x = pq.top(); pq.pop();
    int u = x.second;
    if(x.first > dis[u][p]) continue;
    for(auto e : g[u]) {
      int v = e.first, w = e.second;
      if(dis[u][p]+w < dis[v][p]) {
        dis[v][p] = dis[u][p] + w;
        pq.push(ii(dis[v][p], v));
      }
    }
  }
}

void solve(){
  for(int i=0; i<ms; i++){
    for(int j=0; j<sigma; j++)
      dis[i][j] = inf;
  }
  cin>>k>>n>>m>>q;
  int a, b, c;
  for(int i=0; i<m; i++){
    cin>>a>>b>>c;
    adj[b]++;
    g[a].pb({b, c});
  }
  for(int i=0; i<n; i++){
    if(adj[i]==0){
      g[n+i%k].pb({i, 0});
    }
  }
  for(int i=n; i<n+k; i++){
    dfs(i, i, i%k);
  }
  for(int i=0; i<n; i++){
    if(dis[i][i%k]==inf){
      dijkstra(i, i%k);
    }
  }
  while(q--){
    cin>>a>>b;
    int ans = inf;
    bool f = 0;
    for(int i=0; i<k; i++){
      int lc = lca(a, b, i);
      if((lc%k!=i) || dis[a][i]==inf || dis[b][i]==inf){
        continue;
      }
      f = 1;
      ans = min(ans, dis[b][i] - dis[a][i]);
    }
    if(f) cout<<ans<<endl;
    else cout<<-1<<endl;
  }
}

int32_t main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
}
# Verdict Execution time Memory Grader output
1 Correct 139 ms 88184 KB Output is correct
2 Incorrect 6 ms 4600 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3031 ms 47068 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 4728 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 4728 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 139 ms 88184 KB Output is correct
2 Incorrect 6 ms 4600 KB Output isn't correct
3 Halted 0 ms 0 KB -