Submission #410340

#TimeUsernameProblemLanguageResultExecution timeMemory
410340600MihneaCrocodile's Underground City (IOI11_crocodile)C++17
89 / 100
2084 ms73880 KiB
#include <bits/stdc++.h>
#include "crocodile.h"

using namespace std;

typedef long long ll;

mt19937 rng((long long) (new char));

const int N = 100000 + 7;
const int INF = (int) 1e9 + 7;
int n, m, k, best[N];
bool inside[N];
vector<pair<int, int>> lol[N];
vector<pair<int, int>> g[N];
queue<int> q;

int lt[N], step;

void push(int i, int value) {
  for (auto &it : g[i]) {
    lol[it.first].push_back({min(INF, value + it.second), i});
    sort(lol[it.first].begin(), lol[it.first].end());
    step++;
    vector<pair<int, int>> lol2;
    for (auto &k : lol[it.first]) {
      if (lt[k.second] != step) {
        lol2.push_back(k);
        lt[k.second] = step;
        if ((int) lol2.size() == 2) {
          break;
        }
      }
    }
    lol[it.first] = lol2;
  }
  best[i] = value;
  if (!inside[i]) {
    inside[i] = 1;
    q.push(i);
  }
}

int get_second(int a) {
  if ((int) lol[a].size() < 2) {
    return INF;
  }
  return lol[a][1].first;
}

int travel_plan(int nn, int mm, int r[][2], int len[], int kk, int ch[]) {
  n = nn;
  m = mm;
  k = kk;
  for (int i = 0; i < n; i++) {
    g[i].clear();
    lol[i].clear();
    best[i] = INF;
  }
  for (int i = 0; i < m; i++) {
    g[r[i][0]].push_back({r[i][1], len[i]});
    g[r[i][1]].push_back({r[i][0], len[i]});
    lol[r[i][0]].push_back({INF, r[i][1]});
    lol[r[i][1]].push_back({INF, r[i][0]});
  }
  for (int i = 0; i < n; i++) {
    shuffle(g[i].begin(), g[i].end(), rng);
  }
  shuffle(ch, ch + k, rng);
  for (int i = 0; i < k; i++) {
    push(ch[i], 0);
  }
  int cnt = 0;
  while (!q.empty()) {
    int a = q.front();
    inside[a] = 0;
    q.pop();
    for (auto &it : g[a]) {
      int node = it.first, relax = get_second(node);
      if (relax < best[node]) {
        push(node, relax);
      }
    }
  }
  if (best[0] == INF) {
    best[0] = -1;
  }
  return best[0];
}

Compilation message (stderr)

crocodile.cpp: In function 'int travel_plan(int, int, int (*)[2], int*, int, int*)':
crocodile.cpp:73:7: warning: unused variable 'cnt' [-Wunused-variable]
   73 |   int cnt = 0;
      |       ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...