답안 #655707

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
655707 2022-11-05T10:51:17 Z 600Mihnea From Hacks to Snitches (BOI21_watchmen) C++17
50 / 100
6000 ms 210724 KB
// pana la ora 1
bool home = 0;
bool verbose = 1;

#include <iostream>
#include <cstdio>
#include <vector>
#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) {
  if (d < dp[id]) {
    dp[id] = d;
    pq.push({ -dp[id], id });
  }
}

int main() {

    verbose = 0;
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);


  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 });
  }

  cin >> cnt_cycles;

  for (int i = 1; i <= cnt_cycles; i++) {
    int l;
    cin >> l;
    suml += l;
    cycles[i].resize(l);
    for (int j = 0; j < (int)cycles[i].size(); j++) {
      cin >> cycles[i][j];
      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 };
      }
    }
  }
  for (int i = 1; i <= y; i++) {
    dp[i] = INF;
  }
  if (verbose) {
    cout << "################################################################################### \n";
  }

  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) {
      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
      */

      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) {
            // 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;
}

Compilation message

watchmen.cpp: In function 'int main()':
watchmen.cpp:117:9: warning: unused variable 'r' [-Wunused-variable]
  117 |     int r = what[idv].r;
      |         ^
# 결과 실행 시간 메모리 Grader output
1 Correct 73 ms 20252 KB Output is correct
2 Correct 62 ms 24468 KB Output is correct
3 Correct 58 ms 24232 KB Output is correct
4 Correct 96 ms 24560 KB Output is correct
5 Correct 13 ms 18124 KB Output is correct
6 Correct 66 ms 24212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 76 ms 20172 KB Output is correct
2 Correct 77 ms 24392 KB Output is correct
3 Correct 62 ms 24180 KB Output is correct
4 Correct 87 ms 24500 KB Output is correct
5 Correct 12 ms 18264 KB Output is correct
6 Correct 69 ms 24236 KB Output is correct
7 Correct 82 ms 24060 KB Output is correct
8 Correct 64 ms 23872 KB Output is correct
9 Correct 53 ms 23916 KB Output is correct
10 Correct 76 ms 24224 KB Output is correct
11 Correct 96 ms 23868 KB Output is correct
12 Correct 71 ms 23988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 76 ms 20172 KB Output is correct
2 Correct 77 ms 24392 KB Output is correct
3 Correct 62 ms 24180 KB Output is correct
4 Correct 87 ms 24500 KB Output is correct
5 Correct 12 ms 18264 KB Output is correct
6 Correct 69 ms 24236 KB Output is correct
7 Correct 82 ms 24060 KB Output is correct
8 Correct 64 ms 23872 KB Output is correct
9 Correct 53 ms 23916 KB Output is correct
10 Correct 76 ms 24224 KB Output is correct
11 Correct 96 ms 23868 KB Output is correct
12 Correct 71 ms 23988 KB Output is correct
13 Correct 74 ms 20176 KB Output is correct
14 Correct 61 ms 24424 KB Output is correct
15 Correct 71 ms 24156 KB Output is correct
16 Correct 122 ms 24484 KB Output is correct
17 Correct 11 ms 18204 KB Output is correct
18 Correct 67 ms 24244 KB Output is correct
19 Correct 57 ms 23920 KB Output is correct
20 Correct 64 ms 23948 KB Output is correct
21 Correct 73 ms 23856 KB Output is correct
22 Correct 67 ms 24200 KB Output is correct
23 Correct 70 ms 24112 KB Output is correct
24 Correct 58 ms 23996 KB Output is correct
25 Correct 1384 ms 90992 KB Output is correct
26 Correct 1297 ms 97492 KB Output is correct
27 Correct 1211 ms 90972 KB Output is correct
28 Correct 1025 ms 95104 KB Output is correct
29 Correct 5180 ms 86036 KB Output is correct
30 Correct 5379 ms 128216 KB Output is correct
31 Correct 1610 ms 136520 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 76 ms 20172 KB Output is correct
2 Correct 77 ms 24392 KB Output is correct
3 Correct 62 ms 24180 KB Output is correct
4 Correct 87 ms 24500 KB Output is correct
5 Correct 12 ms 18264 KB Output is correct
6 Correct 69 ms 24236 KB Output is correct
7 Correct 82 ms 24060 KB Output is correct
8 Correct 64 ms 23872 KB Output is correct
9 Correct 53 ms 23916 KB Output is correct
10 Correct 76 ms 24224 KB Output is correct
11 Correct 96 ms 23868 KB Output is correct
12 Correct 71 ms 23988 KB Output is correct
13 Correct 74 ms 20176 KB Output is correct
14 Correct 61 ms 24424 KB Output is correct
15 Correct 71 ms 24156 KB Output is correct
16 Correct 122 ms 24484 KB Output is correct
17 Correct 11 ms 18204 KB Output is correct
18 Correct 67 ms 24244 KB Output is correct
19 Correct 57 ms 23920 KB Output is correct
20 Correct 64 ms 23948 KB Output is correct
21 Correct 73 ms 23856 KB Output is correct
22 Correct 67 ms 24200 KB Output is correct
23 Correct 70 ms 24112 KB Output is correct
24 Correct 58 ms 23996 KB Output is correct
25 Correct 1384 ms 90992 KB Output is correct
26 Correct 1297 ms 97492 KB Output is correct
27 Correct 1211 ms 90972 KB Output is correct
28 Correct 1025 ms 95104 KB Output is correct
29 Correct 5180 ms 86036 KB Output is correct
30 Correct 5379 ms 128216 KB Output is correct
31 Correct 1610 ms 136520 KB Output is correct
32 Correct 73 ms 20552 KB Output is correct
33 Correct 77 ms 25388 KB Output is correct
34 Correct 62 ms 25060 KB Output is correct
35 Correct 99 ms 25460 KB Output is correct
36 Correct 12 ms 18132 KB Output is correct
37 Correct 60 ms 25136 KB Output is correct
38 Correct 61 ms 24816 KB Output is correct
39 Correct 76 ms 24764 KB Output is correct
40 Correct 68 ms 24800 KB Output is correct
41 Correct 83 ms 25100 KB Output is correct
42 Correct 65 ms 24812 KB Output is correct
43 Correct 69 ms 24812 KB Output is correct
44 Correct 1464 ms 129488 KB Output is correct
45 Correct 1434 ms 135796 KB Output is correct
46 Correct 1306 ms 129424 KB Output is correct
47 Correct 1154 ms 133372 KB Output is correct
48 Correct 5573 ms 124764 KB Output is correct
49 Correct 5685 ms 128280 KB Output is correct
50 Correct 1482 ms 136624 KB Output is correct
51 Correct 2166 ms 164924 KB Output is correct
52 Correct 2581 ms 195008 KB Output is correct
53 Correct 2336 ms 181136 KB Output is correct
54 Correct 1037 ms 125156 KB Output is correct
55 Execution timed out 6044 ms 210724 KB Time limit exceeded
56 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 73 ms 20252 KB Output is correct
2 Correct 62 ms 24468 KB Output is correct
3 Correct 58 ms 24232 KB Output is correct
4 Correct 96 ms 24560 KB Output is correct
5 Correct 13 ms 18124 KB Output is correct
6 Correct 66 ms 24212 KB Output is correct
7 Correct 76 ms 20172 KB Output is correct
8 Correct 77 ms 24392 KB Output is correct
9 Correct 62 ms 24180 KB Output is correct
10 Correct 87 ms 24500 KB Output is correct
11 Correct 12 ms 18264 KB Output is correct
12 Correct 69 ms 24236 KB Output is correct
13 Correct 82 ms 24060 KB Output is correct
14 Correct 64 ms 23872 KB Output is correct
15 Correct 53 ms 23916 KB Output is correct
16 Correct 76 ms 24224 KB Output is correct
17 Correct 96 ms 23868 KB Output is correct
18 Correct 71 ms 23988 KB Output is correct
19 Correct 9 ms 17876 KB Output is correct
20 Correct 10 ms 17952 KB Output is correct
21 Correct 12 ms 17876 KB Output is correct
22 Correct 77 ms 20420 KB Output is correct
23 Correct 66 ms 24828 KB Output is correct
24 Correct 72 ms 24612 KB Output is correct
25 Correct 90 ms 24852 KB Output is correct
26 Correct 12 ms 18132 KB Output is correct
27 Correct 63 ms 24548 KB Output is correct
28 Correct 66 ms 24296 KB Output is correct
29 Correct 61 ms 24284 KB Output is correct
30 Correct 56 ms 24288 KB Output is correct
31 Correct 83 ms 24612 KB Output is correct
32 Correct 67 ms 24296 KB Output is correct
33 Correct 68 ms 24320 KB Output is correct
34 Correct 1369 ms 91588 KB Output is correct
35 Correct 1446 ms 87840 KB Output is correct
36 Correct 1504 ms 87768 KB Output is correct
37 Correct 1418 ms 94068 KB Output is correct
38 Correct 1498 ms 90536 KB Output is correct
39 Correct 3894 ms 86404 KB Output is correct
40 Correct 2817 ms 86788 KB Output is correct
41 Correct 1783 ms 85308 KB Output is correct
42 Correct 1372 ms 90644 KB Output is correct
43 Correct 1425 ms 96656 KB Output is correct
44 Correct 1442 ms 96728 KB Output is correct
45 Correct 1478 ms 90492 KB Output is correct
46 Correct 1387 ms 90572 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 73 ms 20252 KB Output is correct
2 Correct 62 ms 24468 KB Output is correct
3 Correct 58 ms 24232 KB Output is correct
4 Correct 96 ms 24560 KB Output is correct
5 Correct 13 ms 18124 KB Output is correct
6 Correct 66 ms 24212 KB Output is correct
7 Correct 76 ms 20172 KB Output is correct
8 Correct 77 ms 24392 KB Output is correct
9 Correct 62 ms 24180 KB Output is correct
10 Correct 87 ms 24500 KB Output is correct
11 Correct 12 ms 18264 KB Output is correct
12 Correct 69 ms 24236 KB Output is correct
13 Correct 82 ms 24060 KB Output is correct
14 Correct 64 ms 23872 KB Output is correct
15 Correct 53 ms 23916 KB Output is correct
16 Correct 76 ms 24224 KB Output is correct
17 Correct 96 ms 23868 KB Output is correct
18 Correct 71 ms 23988 KB Output is correct
19 Correct 74 ms 20176 KB Output is correct
20 Correct 61 ms 24424 KB Output is correct
21 Correct 71 ms 24156 KB Output is correct
22 Correct 122 ms 24484 KB Output is correct
23 Correct 11 ms 18204 KB Output is correct
24 Correct 67 ms 24244 KB Output is correct
25 Correct 57 ms 23920 KB Output is correct
26 Correct 64 ms 23948 KB Output is correct
27 Correct 73 ms 23856 KB Output is correct
28 Correct 67 ms 24200 KB Output is correct
29 Correct 70 ms 24112 KB Output is correct
30 Correct 58 ms 23996 KB Output is correct
31 Correct 1384 ms 90992 KB Output is correct
32 Correct 1297 ms 97492 KB Output is correct
33 Correct 1211 ms 90972 KB Output is correct
34 Correct 1025 ms 95104 KB Output is correct
35 Correct 5180 ms 86036 KB Output is correct
36 Correct 5379 ms 128216 KB Output is correct
37 Correct 1610 ms 136520 KB Output is correct
38 Correct 9 ms 17876 KB Output is correct
39 Correct 10 ms 17952 KB Output is correct
40 Correct 12 ms 17876 KB Output is correct
41 Correct 77 ms 20420 KB Output is correct
42 Correct 66 ms 24828 KB Output is correct
43 Correct 72 ms 24612 KB Output is correct
44 Correct 90 ms 24852 KB Output is correct
45 Correct 12 ms 18132 KB Output is correct
46 Correct 63 ms 24548 KB Output is correct
47 Correct 66 ms 24296 KB Output is correct
48 Correct 61 ms 24284 KB Output is correct
49 Correct 56 ms 24288 KB Output is correct
50 Correct 83 ms 24612 KB Output is correct
51 Correct 67 ms 24296 KB Output is correct
52 Correct 68 ms 24320 KB Output is correct
53 Correct 1369 ms 91588 KB Output is correct
54 Correct 1446 ms 87840 KB Output is correct
55 Correct 1504 ms 87768 KB Output is correct
56 Correct 1418 ms 94068 KB Output is correct
57 Correct 1498 ms 90536 KB Output is correct
58 Correct 3894 ms 86404 KB Output is correct
59 Correct 2817 ms 86788 KB Output is correct
60 Correct 1783 ms 85308 KB Output is correct
61 Correct 1372 ms 90644 KB Output is correct
62 Correct 1425 ms 96656 KB Output is correct
63 Correct 1442 ms 96728 KB Output is correct
64 Correct 1478 ms 90492 KB Output is correct
65 Correct 1387 ms 90572 KB Output is correct
66 Correct 9 ms 17876 KB Output is correct
67 Correct 12 ms 18004 KB Output is correct
68 Correct 10 ms 17948 KB Output is correct
69 Correct 74 ms 20520 KB Output is correct
70 Correct 70 ms 25368 KB Output is correct
71 Correct 63 ms 25124 KB Output is correct
72 Correct 88 ms 25436 KB Output is correct
73 Correct 12 ms 18132 KB Output is correct
74 Correct 66 ms 25156 KB Output is correct
75 Correct 57 ms 24800 KB Output is correct
76 Correct 61 ms 24860 KB Output is correct
77 Correct 60 ms 24792 KB Output is correct
78 Correct 80 ms 25020 KB Output is correct
79 Correct 71 ms 24812 KB Output is correct
80 Correct 59 ms 24896 KB Output is correct
81 Correct 1525 ms 129476 KB Output is correct
82 Correct 1433 ms 135868 KB Output is correct
83 Correct 1312 ms 129436 KB Output is correct
84 Correct 1041 ms 133488 KB Output is correct
85 Correct 5127 ms 124956 KB Output is correct
86 Correct 5770 ms 128204 KB Output is correct
87 Correct 1500 ms 136444 KB Output is correct
88 Correct 1426 ms 129856 KB Output is correct
89 Correct 1477 ms 126212 KB Output is correct
90 Correct 1508 ms 126204 KB Output is correct
91 Correct 1319 ms 132436 KB Output is correct
92 Correct 1430 ms 128620 KB Output is correct
93 Correct 3496 ms 124516 KB Output is correct
94 Correct 2629 ms 124564 KB Output is correct
95 Correct 1765 ms 123340 KB Output is correct
96 Correct 1445 ms 128672 KB Output is correct
97 Correct 1532 ms 133932 KB Output is correct
98 Correct 1521 ms 134052 KB Output is correct
99 Correct 1563 ms 128332 KB Output is correct
100 Correct 1429 ms 128540 KB Output is correct
101 Correct 1499 ms 130464 KB Output is correct
102 Correct 1369 ms 133204 KB Output is correct
103 Correct 1477 ms 129376 KB Output is correct
104 Execution timed out 6018 ms 129128 KB Time limit exceeded
105 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 73 ms 20252 KB Output is correct
2 Correct 62 ms 24468 KB Output is correct
3 Correct 58 ms 24232 KB Output is correct
4 Correct 96 ms 24560 KB Output is correct
5 Correct 13 ms 18124 KB Output is correct
6 Correct 66 ms 24212 KB Output is correct
7 Correct 76 ms 20172 KB Output is correct
8 Correct 77 ms 24392 KB Output is correct
9 Correct 62 ms 24180 KB Output is correct
10 Correct 87 ms 24500 KB Output is correct
11 Correct 12 ms 18264 KB Output is correct
12 Correct 69 ms 24236 KB Output is correct
13 Correct 82 ms 24060 KB Output is correct
14 Correct 64 ms 23872 KB Output is correct
15 Correct 53 ms 23916 KB Output is correct
16 Correct 76 ms 24224 KB Output is correct
17 Correct 96 ms 23868 KB Output is correct
18 Correct 71 ms 23988 KB Output is correct
19 Correct 74 ms 20176 KB Output is correct
20 Correct 61 ms 24424 KB Output is correct
21 Correct 71 ms 24156 KB Output is correct
22 Correct 122 ms 24484 KB Output is correct
23 Correct 11 ms 18204 KB Output is correct
24 Correct 67 ms 24244 KB Output is correct
25 Correct 57 ms 23920 KB Output is correct
26 Correct 64 ms 23948 KB Output is correct
27 Correct 73 ms 23856 KB Output is correct
28 Correct 67 ms 24200 KB Output is correct
29 Correct 70 ms 24112 KB Output is correct
30 Correct 58 ms 23996 KB Output is correct
31 Correct 1384 ms 90992 KB Output is correct
32 Correct 1297 ms 97492 KB Output is correct
33 Correct 1211 ms 90972 KB Output is correct
34 Correct 1025 ms 95104 KB Output is correct
35 Correct 5180 ms 86036 KB Output is correct
36 Correct 5379 ms 128216 KB Output is correct
37 Correct 1610 ms 136520 KB Output is correct
38 Correct 73 ms 20552 KB Output is correct
39 Correct 77 ms 25388 KB Output is correct
40 Correct 62 ms 25060 KB Output is correct
41 Correct 99 ms 25460 KB Output is correct
42 Correct 12 ms 18132 KB Output is correct
43 Correct 60 ms 25136 KB Output is correct
44 Correct 61 ms 24816 KB Output is correct
45 Correct 76 ms 24764 KB Output is correct
46 Correct 68 ms 24800 KB Output is correct
47 Correct 83 ms 25100 KB Output is correct
48 Correct 65 ms 24812 KB Output is correct
49 Correct 69 ms 24812 KB Output is correct
50 Correct 1464 ms 129488 KB Output is correct
51 Correct 1434 ms 135796 KB Output is correct
52 Correct 1306 ms 129424 KB Output is correct
53 Correct 1154 ms 133372 KB Output is correct
54 Correct 5573 ms 124764 KB Output is correct
55 Correct 5685 ms 128280 KB Output is correct
56 Correct 1482 ms 136624 KB Output is correct
57 Correct 2166 ms 164924 KB Output is correct
58 Correct 2581 ms 195008 KB Output is correct
59 Correct 2336 ms 181136 KB Output is correct
60 Correct 1037 ms 125156 KB Output is correct
61 Execution timed out 6044 ms 210724 KB Time limit exceeded
62 Halted 0 ms 0 KB -