Submission #655705

# Submission time Handle Problem Language Result Execution time Memory
655705 2022-11-05T10:48:46 Z 600Mihnea From Hacks to Snitches (BOI21_watchmen) C++17
40 / 100
6000 ms 134960 KB
// pana la ora 1 
bool home = 0;
bool verbose = 1;

#include <iostream>
#include <cstdio>
#include <vector>
#include <cassert>
#include <queue>


using namespace std;

struct T {
  int v;
  int r;
};

const int N = 250000 + 7;
const int V = (int)1e7 + 7;
const int INF = (int)1e9 + 7;
int n;
int m;
int cnt_cycles;
int suml;
vector<pair<int, int>> edges;
vector<int> cycles[N];
int cycle_id[N];
int cycle_location[N];
int single_vertex_id[N];
vector<int> cycle_vertex_id[N];
int dp[V];
T what[V];
int y;
vector<int> g[N];

priority_queue<pair<int, int>> pq;

void relax(int id, int d) {
  assert(1 <= id && id <= y);
  if (d < dp[id]) {
    dp[id] = d;
    pq.push({ -dp[id], id });
  }
}

int main() {
  if (!home) {
    verbose = 0;
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
  }
  if (home) {
    verbose = 1;
  //  FILE* stream;
//freopen_s(&stream, "input.txt", "r", stdin);
  }

  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
    edges.push_back({ a, b });
  }

  bool debug = 1;
  cin >> cnt_cycles;
  debug &= (cnt_cycles == 21);

  for (int i = 1; i <= cnt_cycles; i++) {
    int l;
    cin >> l;
    suml += l;
    if (i == 1) {
      debug &= (l == 60);
    }
    else {
      debug &= (l == 3);
    }
    cycles[i].resize(l);
    for (int j = 0; j < (int)cycles[i].size(); j++) {
      cin >> cycles[i][j];
      assert(cycle_id[cycles[i][j]] == 0);
      cycle_id[cycles[i][j]] = i;
      cycle_location[cycles[i][j]] = j;
    }
    if (verbose) {
      cout << " ---> ";
      for (auto& v : cycles[i]) {
        cout << v << " ";
      }
      cout << "\n";
    }
  }
  for (int a = 1; a <= n; a++) {
    if (cycle_id[a] == 0) {
      y++;
      single_vertex_id[a] = y;
      what[y] = { a, 0 };
    }
    else {
      int id = cycle_id[a];
      cycle_vertex_id[a].resize((int) cycles[id].size());
      for (int j = 0; j < (int)cycles[id].size(); j++) {
        y++;
        cycle_vertex_id[a][j] = y;
        what[y] = { a, j };
      }
    }
  }
  if (debug) {
   // assert(0);
   // exit(0);
  }
  for (int i = 1; i <= y; i++) {
    dp[i] = INF;
  }
  if (verbose) {
    cout << "################################################################################### \n";
  }
  assert(cycle_id[1] == 0);
  assert(cycle_id[n] == 0);

  dp[single_vertex_id[1]] = 0;
  pq.push({ 0, single_vertex_id[1] });


  while (!pq.empty()) {
    int d = -pq.top().first;
    int idv = pq.top().second;
    pq.pop();
    if (dp[idv] != d) {
      continue;
    }
    int vertex = what[idv].v;
    int r = what[idv].r;
    if (cycle_id[vertex] == 0) {
      assert(r == 0);
      for (auto& vecin : g[vertex]) {
        if (cycle_id[vecin] == 0) {
          // vecin is a single vertex
          relax(single_vertex_id[vecin], d + 1);
        }
        else {
          // vecin is on a cycle
          int cycle_vecin = cycle_id[vecin];
          int l_vecin = cycles[cycle_vecin].size();
          for (int twait = 0; twait < l_vecin; twait++) {
            // la momentul d + 1 + twait
            if ((d + 1 + twait) % l_vecin == cycle_location[vecin]) {
              continue;
            }
            relax(cycle_vertex_id[vecin][(d + 1 + twait) % l_vecin], d + 1 + twait);
          }
        }
      }
    }
    else {
      int cycle = cycle_id[vertex];
      int l = cycles[cycle].size();

      /*
        wait a moment here
      */

      if ((d + 1) % l != cycle_location[vertex]) {
        relax(cycle_vertex_id[vertex][(d + 1) % l], d + 1);
      }

      /*
        don't wait a moment here
      */

      assert(0 <= r && r < l);
      assert(d % l == r);
      for (auto& vecin : g[vertex]) {
        if (cycle_id[vecin] == 0) {
          // vecin is a single vertex
          relax(single_vertex_id[vecin], d + 1);
        }
        else {
          // vecin is on a cycle
          int cycle_vecin = cycle_id[vecin];
          int l_vecin = cycles[cycle_vecin].size();
          if (cycle_vecin == cycle) {
            if (l_vecin!=l) {
              cout << "bad\n";
              exit(0);
            }
            // vecin is on the same cycle
            if ((d + 1) % l_vecin == cycle_location[vecin]) {
              continue;
            }
            if (d % l_vecin == cycle_location[vecin] && (d + 1) % l_vecin == cycle_location[vertex]) {
              continue;
            }

            relax(cycle_vertex_id[vecin][(d + 1) % l_vecin], d + 1);
            /*
            
            vertex -> vecin <- vecin2


            */
          }
          else {
            // vecin is on a different cycle
            for (int j = 0; j <= l_vecin; j++) {
              int twait = j * l;
              // la momentul d + 1 + twait
              if ((d + 1 + twait) % l_vecin == cycle_location[vecin]) {
                continue;
              }
              relax(cycle_vertex_id[vecin][(d + 1 + twait) % l_vecin], d + 1 + twait);
            }
          }
        }
      }
    }
  }

  int sol = dp[single_vertex_id[n]];
  if (sol == INF) {
    cout << "impossible\n";
    return 0;
  }
  cout << sol << "\n";
  return 0;
  /*for (auto& edge : edges) {
    int a = edge.first, b = edge.second;
    if (cycle_id[a] == 0 && cycle_id[b] == 0) {
      // just a normal edge
      addEdge(single_vertex_id[a], single_vertex_id[b], 1);
      continue;
    }
    if (cycle_id[a] != 0 && cycle_id[b] == 0) {
      swap(a, b);
    }
    if (cycle_id[a] == 0 && cycle_id[b] != 0) {

      continue;
    }
    assert(cycle_id[a] != 0 && cycle_id[b] != 0);
  }*/
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 86 ms 19916 KB Output is correct
2 Correct 69 ms 24200 KB Output is correct
3 Correct 71 ms 23984 KB Output is correct
4 Correct 90 ms 24284 KB Output is correct
5 Correct 12 ms 18188 KB Output is correct
6 Correct 89 ms 23944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 97 ms 19912 KB Output is correct
2 Correct 78 ms 24156 KB Output is correct
3 Correct 69 ms 23916 KB Output is correct
4 Correct 101 ms 24228 KB Output is correct
5 Correct 13 ms 18132 KB Output is correct
6 Correct 73 ms 23996 KB Output is correct
7 Correct 63 ms 23748 KB Output is correct
8 Correct 54 ms 23660 KB Output is correct
9 Correct 50 ms 23616 KB Output is correct
10 Correct 71 ms 23880 KB Output is correct
11 Correct 67 ms 23704 KB Output is correct
12 Correct 76 ms 23696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 97 ms 19912 KB Output is correct
2 Correct 78 ms 24156 KB Output is correct
3 Correct 69 ms 23916 KB Output is correct
4 Correct 101 ms 24228 KB Output is correct
5 Correct 13 ms 18132 KB Output is correct
6 Correct 73 ms 23996 KB Output is correct
7 Correct 63 ms 23748 KB Output is correct
8 Correct 54 ms 23660 KB Output is correct
9 Correct 50 ms 23616 KB Output is correct
10 Correct 71 ms 23880 KB Output is correct
11 Correct 67 ms 23704 KB Output is correct
12 Correct 76 ms 23696 KB Output is correct
13 Correct 87 ms 19912 KB Output is correct
14 Correct 61 ms 24128 KB Output is correct
15 Correct 62 ms 23924 KB Output is correct
16 Correct 88 ms 24312 KB Output is correct
17 Correct 12 ms 18132 KB Output is correct
18 Correct 71 ms 23956 KB Output is correct
19 Correct 56 ms 23664 KB Output is correct
20 Correct 54 ms 23660 KB Output is correct
21 Correct 51 ms 23616 KB Output is correct
22 Correct 85 ms 24008 KB Output is correct
23 Correct 77 ms 23612 KB Output is correct
24 Correct 86 ms 23644 KB Output is correct
25 Correct 1356 ms 90888 KB Output is correct
26 Correct 1329 ms 97476 KB Output is correct
27 Correct 1251 ms 90900 KB Output is correct
28 Correct 960 ms 94800 KB Output is correct
29 Execution timed out 6059 ms 85984 KB Time limit exceeded
30 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 97 ms 19912 KB Output is correct
2 Correct 78 ms 24156 KB Output is correct
3 Correct 69 ms 23916 KB Output is correct
4 Correct 101 ms 24228 KB Output is correct
5 Correct 13 ms 18132 KB Output is correct
6 Correct 73 ms 23996 KB Output is correct
7 Correct 63 ms 23748 KB Output is correct
8 Correct 54 ms 23660 KB Output is correct
9 Correct 50 ms 23616 KB Output is correct
10 Correct 71 ms 23880 KB Output is correct
11 Correct 67 ms 23704 KB Output is correct
12 Correct 76 ms 23696 KB Output is correct
13 Correct 87 ms 19912 KB Output is correct
14 Correct 61 ms 24128 KB Output is correct
15 Correct 62 ms 23924 KB Output is correct
16 Correct 88 ms 24312 KB Output is correct
17 Correct 12 ms 18132 KB Output is correct
18 Correct 71 ms 23956 KB Output is correct
19 Correct 56 ms 23664 KB Output is correct
20 Correct 54 ms 23660 KB Output is correct
21 Correct 51 ms 23616 KB Output is correct
22 Correct 85 ms 24008 KB Output is correct
23 Correct 77 ms 23612 KB Output is correct
24 Correct 86 ms 23644 KB Output is correct
25 Correct 1356 ms 90888 KB Output is correct
26 Correct 1329 ms 97476 KB Output is correct
27 Correct 1251 ms 90900 KB Output is correct
28 Correct 960 ms 94800 KB Output is correct
29 Execution timed out 6059 ms 85984 KB Time limit exceeded
30 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 86 ms 19916 KB Output is correct
2 Correct 69 ms 24200 KB Output is correct
3 Correct 71 ms 23984 KB Output is correct
4 Correct 90 ms 24284 KB Output is correct
5 Correct 12 ms 18188 KB Output is correct
6 Correct 89 ms 23944 KB Output is correct
7 Correct 97 ms 19912 KB Output is correct
8 Correct 78 ms 24156 KB Output is correct
9 Correct 69 ms 23916 KB Output is correct
10 Correct 101 ms 24228 KB Output is correct
11 Correct 13 ms 18132 KB Output is correct
12 Correct 73 ms 23996 KB Output is correct
13 Correct 63 ms 23748 KB Output is correct
14 Correct 54 ms 23660 KB Output is correct
15 Correct 50 ms 23616 KB Output is correct
16 Correct 71 ms 23880 KB Output is correct
17 Correct 67 ms 23704 KB Output is correct
18 Correct 76 ms 23696 KB Output is correct
19 Correct 9 ms 17876 KB Output is correct
20 Correct 11 ms 17872 KB Output is correct
21 Correct 11 ms 17964 KB Output is correct
22 Correct 87 ms 19944 KB Output is correct
23 Correct 85 ms 24120 KB Output is correct
24 Correct 75 ms 23952 KB Output is correct
25 Correct 96 ms 24256 KB Output is correct
26 Correct 12 ms 18140 KB Output is correct
27 Correct 66 ms 24000 KB Output is correct
28 Correct 62 ms 23744 KB Output is correct
29 Correct 56 ms 23616 KB Output is correct
30 Correct 58 ms 23612 KB Output is correct
31 Correct 88 ms 23892 KB Output is correct
32 Correct 63 ms 23792 KB Output is correct
33 Correct 64 ms 23712 KB Output is correct
34 Correct 1410 ms 91488 KB Output is correct
35 Correct 1520 ms 87808 KB Output is correct
36 Correct 1439 ms 87616 KB Output is correct
37 Correct 1378 ms 94080 KB Output is correct
38 Correct 1484 ms 128756 KB Output is correct
39 Correct 5056 ms 124620 KB Output is correct
40 Correct 3206 ms 124620 KB Output is correct
41 Correct 2005 ms 123540 KB Output is correct
42 Correct 1448 ms 128596 KB Output is correct
43 Correct 1497 ms 134748 KB Output is correct
44 Correct 1519 ms 134960 KB Output is correct
45 Correct 1543 ms 128564 KB Output is correct
46 Correct 1465 ms 128572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 86 ms 19916 KB Output is correct
2 Correct 69 ms 24200 KB Output is correct
3 Correct 71 ms 23984 KB Output is correct
4 Correct 90 ms 24284 KB Output is correct
5 Correct 12 ms 18188 KB Output is correct
6 Correct 89 ms 23944 KB Output is correct
7 Correct 97 ms 19912 KB Output is correct
8 Correct 78 ms 24156 KB Output is correct
9 Correct 69 ms 23916 KB Output is correct
10 Correct 101 ms 24228 KB Output is correct
11 Correct 13 ms 18132 KB Output is correct
12 Correct 73 ms 23996 KB Output is correct
13 Correct 63 ms 23748 KB Output is correct
14 Correct 54 ms 23660 KB Output is correct
15 Correct 50 ms 23616 KB Output is correct
16 Correct 71 ms 23880 KB Output is correct
17 Correct 67 ms 23704 KB Output is correct
18 Correct 76 ms 23696 KB Output is correct
19 Correct 87 ms 19912 KB Output is correct
20 Correct 61 ms 24128 KB Output is correct
21 Correct 62 ms 23924 KB Output is correct
22 Correct 88 ms 24312 KB Output is correct
23 Correct 12 ms 18132 KB Output is correct
24 Correct 71 ms 23956 KB Output is correct
25 Correct 56 ms 23664 KB Output is correct
26 Correct 54 ms 23660 KB Output is correct
27 Correct 51 ms 23616 KB Output is correct
28 Correct 85 ms 24008 KB Output is correct
29 Correct 77 ms 23612 KB Output is correct
30 Correct 86 ms 23644 KB Output is correct
31 Correct 1356 ms 90888 KB Output is correct
32 Correct 1329 ms 97476 KB Output is correct
33 Correct 1251 ms 90900 KB Output is correct
34 Correct 960 ms 94800 KB Output is correct
35 Execution timed out 6059 ms 85984 KB Time limit exceeded
36 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 86 ms 19916 KB Output is correct
2 Correct 69 ms 24200 KB Output is correct
3 Correct 71 ms 23984 KB Output is correct
4 Correct 90 ms 24284 KB Output is correct
5 Correct 12 ms 18188 KB Output is correct
6 Correct 89 ms 23944 KB Output is correct
7 Correct 97 ms 19912 KB Output is correct
8 Correct 78 ms 24156 KB Output is correct
9 Correct 69 ms 23916 KB Output is correct
10 Correct 101 ms 24228 KB Output is correct
11 Correct 13 ms 18132 KB Output is correct
12 Correct 73 ms 23996 KB Output is correct
13 Correct 63 ms 23748 KB Output is correct
14 Correct 54 ms 23660 KB Output is correct
15 Correct 50 ms 23616 KB Output is correct
16 Correct 71 ms 23880 KB Output is correct
17 Correct 67 ms 23704 KB Output is correct
18 Correct 76 ms 23696 KB Output is correct
19 Correct 87 ms 19912 KB Output is correct
20 Correct 61 ms 24128 KB Output is correct
21 Correct 62 ms 23924 KB Output is correct
22 Correct 88 ms 24312 KB Output is correct
23 Correct 12 ms 18132 KB Output is correct
24 Correct 71 ms 23956 KB Output is correct
25 Correct 56 ms 23664 KB Output is correct
26 Correct 54 ms 23660 KB Output is correct
27 Correct 51 ms 23616 KB Output is correct
28 Correct 85 ms 24008 KB Output is correct
29 Correct 77 ms 23612 KB Output is correct
30 Correct 86 ms 23644 KB Output is correct
31 Correct 1356 ms 90888 KB Output is correct
32 Correct 1329 ms 97476 KB Output is correct
33 Correct 1251 ms 90900 KB Output is correct
34 Correct 960 ms 94800 KB Output is correct
35 Execution timed out 6059 ms 85984 KB Time limit exceeded
36 Halted 0 ms 0 KB -