답안 #658711

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
658711 2022-11-14T13:19:15 Z Lobo From Hacks to Snitches (BOI21_watchmen) C++17
35 / 100
3011 ms 258976 KB
#include <bits/stdc++.h>
using namespace std;
// #define int long long
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define all(x) x.begin(),x.end()

const int inf = 1e9+10;
const int maxn = 3e5+10;
const int maxd = 5e6;

int n, m, k;
int szrt[maxn], idrt[maxn], thrt[maxn], isrt[maxn], nxrt[maxn], pvrt[maxn], dlol[maxn];
vector<int> g[maxn], d[maxn];
vector<pair<int,int>> withdist[maxd+1];

int32_t main() {
    // freopen("in.in", "r", stdin);

    cin >> n >> m;
    for(int i = 1; i <= m; i++) {
        int u,v; cin >> u >> v;
        g[u].pb(v);
        g[v].pb(u);
    }

    cin >> k;
    for(int i = 1; i <= k; i++) {
        cin >> szrt[i];
        for(int j = 0; j < szrt[i]; j++) {
            int v; cin >> v;
            idrt[v] = i;
            isrt[v] = 1;
            thrt[v] = j;
        }
    }

    szrt[0] = 1;
    for(int i = 1; i <= n; i++) {
        dlol[i] = inf;
        if(isrt[i]) {
            for(int j = 0; j < szrt[idrt[i]]; j++) {
                d[i].pb(inf);
            }
            int u = i;
            for(auto v : g[u]) {
                if(isrt[v] && idrt[u] == idrt[v] && thrt[v] == (thrt[u]+1)%szrt[idrt[u]]) {
                    nxrt[u] = v;
                }
                if(isrt[v] && idrt[u] == idrt[v] && thrt[u] == (thrt[v]+1)%szrt[idrt[u]]) {
                    pvrt[u] = v;
                }
            }
        }
        else {
            d[i].pb(inf);
        }

    }

    d[1][0] = 0;
    withdist[0].pb(mp(1,0));
    for(int distcur = 0; distcur <= maxd; distcur++) {
    while(withdist[distcur].size()) {
        auto X = withdist[distcur].back();
        withdist[distcur].pop_back();
        int u = X.fr;
        int t = X.sc;
        if(t != -1 && distcur != d[u][t]) continue;
        if(t == -1 && distcur != dlol[u]) continue;

        if(t == -1) {
            for(auto v : g[u]) {
                if(isrt[v]) {
                    int d1 = dlol[u]+1;
                    int t1 = d1%szrt[idrt[v]];
                    // Vou encontrar o guarda em v ou indo para v
                    // indo para v == u é depois de v e em t1 ele vai estar em u
                    if(t1 == thrt[v] || (thrt[u] == (thrt[v]+1)%szrt[idrt[u]] && t1 == thrt[u]) && idrt[u] == idrt[v]) continue;
                    if(d[v][t1] > d1) {
                        d[v][t1] = d1;
                        withdist[d[v][t1]].pb(mp(v,t1));
                    }

                    if(idrt[v] != idrt[u]) {
                        set<int> stt1;
                        d1 = dlol[u]+1;
                        while(true) {
                            int t1 = d1%szrt[idrt[v]];
                            if(stt1.count(t1)) break;
                            stt1.insert(t1);
                            // Vou encontrar o guarda em v ou indo para v
                            // indo para v == u é depois de v e em t1 ele vai estar em u
                            if(d[v][t1] > d1 && t1 != thrt[v]) {
                                d[v][t1] = d1;
                                withdist[d[v][t1]].pb(mp(v,t1));
                            }
                            d1+= szrt[idrt[u]];
                        }
                    }
                }
                else {
                    int d1 = dlol[u]+1;
                    int t1 = 0;
                    if(d[v][t1] > d1) {
                        d[v][t1] = d1;
                        withdist[d[v][t1]].pb(mp(v,t1));
                    }
                }
            }
            continue;
        }

        if(isrt[u]) {
            int v,d1,t1;
            v = nxrt[u];
            d1 = d[u][t]+1;
            t1 = d1%szrt[idrt[v]];
            // Vou encontrar o guarda em v ou indo para v
            // indo para v == u é depois de v e em t1 ele vai estar em u
            if(!(t1 == thrt[v] || (thrt[u] == (thrt[v]+1)%szrt[idrt[u]] && t1 == thrt[u] && idrt[u] == idrt[v])) && d[v][t1] > d1) {
                d[v][t1] = d1;
                withdist[d[v][t1]].pb(mp(v,t1));
            }

            v = pvrt[u];
            d1 = d[u][t]+1;
            t1 = d1%szrt[idrt[v]];
            // Vou encontrar o guarda em v ou indo para v
            // indo para v == u é depois de v e em t1 ele vai estar em u
            if(!(t1 == thrt[v] || (thrt[u] == (thrt[v]+1)%szrt[idrt[u]] && t1 == thrt[u] && idrt[u] == idrt[v])) && d[v][t1] > d1) {
                d[v][t1] = d1;
                withdist[d[v][t1]].pb(mp(v,t1));
            }

            if(dlol[u] > d[u][t]) {
                dlol[u] = d[u][t];
                withdist[dlol[u]].pb(mp(u,-1));
            }

            v = u;
            d1 = d[u][t]+1;
            t1 = d1%szrt[idrt[v]];
            if(!(t1 == thrt[v]) && d[v][t1] > d1) {
                d[v][t1] = d1;
                withdist[d[v][t1]].pb(mp(v,t1));
            }
        }
        else {
            for(auto v : g[u]) {
                if(isrt[v]) {
                    // d[u][t]+1+i = (thrt[v]+1) MOD
                    // i = trrt[v]-d[u][t] MOD
                    int i = ((thrt[v]-d[u][t])%szrt[idrt[v]]+szrt[idrt[v]])%szrt[idrt[v]];
                    int d1 = d[u][t]+1+i;
                    int t1 = d1%szrt[idrt[v]];
                    if(t1 == thrt[v]) continue;
                    if(d[v][t1] > d1) {
                        d[v][t1] = d1;
                        withdist[d[v][t1]].pb(mp(v,t1));
                    }
                    d1 = d[u][t]+1;
                    t1 = d1%szrt[idrt[v]];
                    if(t1 == thrt[v]) continue;
                    if(d[v][t1] > d1) {
                        d[v][t1] = d1;
                        withdist[d[v][t1]].pb(mp(v,t1));
                    }
                }
                else {
                    int d1 = d[u][t]+1;
                    int t1 = 0;
                    if(d[v][t1] > d1) {
                        d[v][t1] = d1;
                        withdist[d[v][t1]].pb(mp(v,t1));
                    }
                }
            }
        }
    }
    }

    if(d[n][0] == inf) cout << "impossible" << endl;
    else cout << d[n][0] << endl;
}

Compilation message

watchmen.cpp: In function 'int32_t main()':
watchmen.cpp:81:97: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   81 |                     if(t1 == thrt[v] || (thrt[u] == (thrt[v]+1)%szrt[idrt[u]] && t1 == thrt[u]) && idrt[u] == idrt[v]) continue;
      |                                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 116 ms 132984 KB Output is correct
2 Correct 195 ms 143168 KB Output is correct
3 Correct 208 ms 141736 KB Output is correct
4 Correct 188 ms 141340 KB Output is correct
5 Correct 84 ms 132064 KB Output is correct
6 Correct 200 ms 141356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 123 ms 132832 KB Output is correct
2 Correct 187 ms 143120 KB Output is correct
3 Correct 219 ms 141732 KB Output is correct
4 Correct 179 ms 141352 KB Output is correct
5 Correct 85 ms 131948 KB Output is correct
6 Correct 204 ms 141408 KB Output is correct
7 Correct 177 ms 141744 KB Output is correct
8 Correct 183 ms 141708 KB Output is correct
9 Correct 215 ms 141968 KB Output is correct
10 Correct 171 ms 140704 KB Output is correct
11 Correct 176 ms 140272 KB Output is correct
12 Correct 214 ms 141260 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 123 ms 132832 KB Output is correct
2 Correct 187 ms 143120 KB Output is correct
3 Correct 219 ms 141732 KB Output is correct
4 Correct 179 ms 141352 KB Output is correct
5 Correct 85 ms 131948 KB Output is correct
6 Correct 204 ms 141408 KB Output is correct
7 Correct 177 ms 141744 KB Output is correct
8 Correct 183 ms 141708 KB Output is correct
9 Correct 215 ms 141968 KB Output is correct
10 Correct 171 ms 140704 KB Output is correct
11 Correct 176 ms 140272 KB Output is correct
12 Correct 214 ms 141260 KB Output is correct
13 Correct 117 ms 132828 KB Output is correct
14 Correct 190 ms 143164 KB Output is correct
15 Correct 193 ms 141624 KB Output is correct
16 Correct 178 ms 141256 KB Output is correct
17 Correct 85 ms 131988 KB Output is correct
18 Correct 182 ms 141388 KB Output is correct
19 Correct 184 ms 141676 KB Output is correct
20 Correct 204 ms 141748 KB Output is correct
21 Correct 183 ms 142024 KB Output is correct
22 Correct 184 ms 140752 KB Output is correct
23 Correct 167 ms 140212 KB Output is correct
24 Correct 190 ms 141312 KB Output is correct
25 Correct 2855 ms 189428 KB Output is correct
26 Correct 2500 ms 195536 KB Output is correct
27 Correct 2360 ms 191272 KB Output is correct
28 Correct 2130 ms 195544 KB Output is correct
29 Correct 2572 ms 182488 KB Output is correct
30 Correct 2435 ms 185384 KB Output is correct
31 Correct 2626 ms 193668 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 123 ms 132832 KB Output is correct
2 Correct 187 ms 143120 KB Output is correct
3 Correct 219 ms 141732 KB Output is correct
4 Correct 179 ms 141352 KB Output is correct
5 Correct 85 ms 131948 KB Output is correct
6 Correct 204 ms 141408 KB Output is correct
7 Correct 177 ms 141744 KB Output is correct
8 Correct 183 ms 141708 KB Output is correct
9 Correct 215 ms 141968 KB Output is correct
10 Correct 171 ms 140704 KB Output is correct
11 Correct 176 ms 140272 KB Output is correct
12 Correct 214 ms 141260 KB Output is correct
13 Correct 117 ms 132828 KB Output is correct
14 Correct 190 ms 143164 KB Output is correct
15 Correct 193 ms 141624 KB Output is correct
16 Correct 178 ms 141256 KB Output is correct
17 Correct 85 ms 131988 KB Output is correct
18 Correct 182 ms 141388 KB Output is correct
19 Correct 184 ms 141676 KB Output is correct
20 Correct 204 ms 141748 KB Output is correct
21 Correct 183 ms 142024 KB Output is correct
22 Correct 184 ms 140752 KB Output is correct
23 Correct 167 ms 140212 KB Output is correct
24 Correct 190 ms 141312 KB Output is correct
25 Correct 2855 ms 189428 KB Output is correct
26 Correct 2500 ms 195536 KB Output is correct
27 Correct 2360 ms 191272 KB Output is correct
28 Correct 2130 ms 195544 KB Output is correct
29 Correct 2572 ms 182488 KB Output is correct
30 Correct 2435 ms 185384 KB Output is correct
31 Correct 2626 ms 193668 KB Output is correct
32 Correct 121 ms 132888 KB Output is correct
33 Correct 185 ms 143060 KB Output is correct
34 Correct 186 ms 141616 KB Output is correct
35 Correct 176 ms 141300 KB Output is correct
36 Correct 87 ms 131988 KB Output is correct
37 Correct 174 ms 141428 KB Output is correct
38 Correct 173 ms 141692 KB Output is correct
39 Correct 224 ms 141796 KB Output is correct
40 Correct 176 ms 141976 KB Output is correct
41 Correct 184 ms 140756 KB Output is correct
42 Correct 174 ms 140260 KB Output is correct
43 Correct 172 ms 141260 KB Output is correct
44 Correct 2617 ms 189388 KB Output is correct
45 Correct 2558 ms 195476 KB Output is correct
46 Correct 2449 ms 191296 KB Output is correct
47 Correct 2188 ms 195584 KB Output is correct
48 Correct 2454 ms 182592 KB Output is correct
49 Correct 2430 ms 185416 KB Output is correct
50 Correct 2672 ms 193652 KB Output is correct
51 Correct 3011 ms 226364 KB Output is correct
52 Correct 2814 ms 258976 KB Output is correct
53 Correct 2603 ms 227000 KB Output is correct
54 Correct 2205 ms 191984 KB Output is correct
55 Correct 2637 ms 224900 KB Output is correct
56 Correct 2713 ms 205152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 116 ms 132984 KB Output is correct
2 Correct 195 ms 143168 KB Output is correct
3 Correct 208 ms 141736 KB Output is correct
4 Correct 188 ms 141340 KB Output is correct
5 Correct 84 ms 132064 KB Output is correct
6 Correct 200 ms 141356 KB Output is correct
7 Correct 123 ms 132832 KB Output is correct
8 Correct 187 ms 143120 KB Output is correct
9 Correct 219 ms 141732 KB Output is correct
10 Correct 179 ms 141352 KB Output is correct
11 Correct 85 ms 131948 KB Output is correct
12 Correct 204 ms 141408 KB Output is correct
13 Correct 177 ms 141744 KB Output is correct
14 Correct 183 ms 141708 KB Output is correct
15 Correct 215 ms 141968 KB Output is correct
16 Correct 171 ms 140704 KB Output is correct
17 Correct 176 ms 140272 KB Output is correct
18 Correct 214 ms 141260 KB Output is correct
19 Correct 88 ms 131708 KB Output is correct
20 Correct 86 ms 131816 KB Output is correct
21 Correct 85 ms 131788 KB Output is correct
22 Correct 118 ms 132816 KB Output is correct
23 Correct 215 ms 143168 KB Output is correct
24 Correct 182 ms 141800 KB Output is correct
25 Correct 197 ms 141300 KB Output is correct
26 Correct 87 ms 132044 KB Output is correct
27 Correct 187 ms 141428 KB Output is correct
28 Correct 200 ms 141772 KB Output is correct
29 Correct 216 ms 141728 KB Output is correct
30 Correct 193 ms 141900 KB Output is correct
31 Correct 176 ms 140768 KB Output is correct
32 Correct 203 ms 140328 KB Output is correct
33 Correct 179 ms 141368 KB Output is correct
34 Correct 2788 ms 186548 KB Output is correct
35 Correct 2798 ms 181412 KB Output is correct
36 Correct 2651 ms 183576 KB Output is correct
37 Incorrect 2419 ms 186444 KB Output isn't correct
38 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 116 ms 132984 KB Output is correct
2 Correct 195 ms 143168 KB Output is correct
3 Correct 208 ms 141736 KB Output is correct
4 Correct 188 ms 141340 KB Output is correct
5 Correct 84 ms 132064 KB Output is correct
6 Correct 200 ms 141356 KB Output is correct
7 Correct 123 ms 132832 KB Output is correct
8 Correct 187 ms 143120 KB Output is correct
9 Correct 219 ms 141732 KB Output is correct
10 Correct 179 ms 141352 KB Output is correct
11 Correct 85 ms 131948 KB Output is correct
12 Correct 204 ms 141408 KB Output is correct
13 Correct 177 ms 141744 KB Output is correct
14 Correct 183 ms 141708 KB Output is correct
15 Correct 215 ms 141968 KB Output is correct
16 Correct 171 ms 140704 KB Output is correct
17 Correct 176 ms 140272 KB Output is correct
18 Correct 214 ms 141260 KB Output is correct
19 Correct 117 ms 132828 KB Output is correct
20 Correct 190 ms 143164 KB Output is correct
21 Correct 193 ms 141624 KB Output is correct
22 Correct 178 ms 141256 KB Output is correct
23 Correct 85 ms 131988 KB Output is correct
24 Correct 182 ms 141388 KB Output is correct
25 Correct 184 ms 141676 KB Output is correct
26 Correct 204 ms 141748 KB Output is correct
27 Correct 183 ms 142024 KB Output is correct
28 Correct 184 ms 140752 KB Output is correct
29 Correct 167 ms 140212 KB Output is correct
30 Correct 190 ms 141312 KB Output is correct
31 Correct 2855 ms 189428 KB Output is correct
32 Correct 2500 ms 195536 KB Output is correct
33 Correct 2360 ms 191272 KB Output is correct
34 Correct 2130 ms 195544 KB Output is correct
35 Correct 2572 ms 182488 KB Output is correct
36 Correct 2435 ms 185384 KB Output is correct
37 Correct 2626 ms 193668 KB Output is correct
38 Correct 88 ms 131708 KB Output is correct
39 Correct 86 ms 131816 KB Output is correct
40 Correct 85 ms 131788 KB Output is correct
41 Correct 118 ms 132816 KB Output is correct
42 Correct 215 ms 143168 KB Output is correct
43 Correct 182 ms 141800 KB Output is correct
44 Correct 197 ms 141300 KB Output is correct
45 Correct 87 ms 132044 KB Output is correct
46 Correct 187 ms 141428 KB Output is correct
47 Correct 200 ms 141772 KB Output is correct
48 Correct 216 ms 141728 KB Output is correct
49 Correct 193 ms 141900 KB Output is correct
50 Correct 176 ms 140768 KB Output is correct
51 Correct 203 ms 140328 KB Output is correct
52 Correct 179 ms 141368 KB Output is correct
53 Correct 2788 ms 186548 KB Output is correct
54 Correct 2798 ms 181412 KB Output is correct
55 Correct 2651 ms 183576 KB Output is correct
56 Incorrect 2419 ms 186444 KB Output isn't correct
57 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 116 ms 132984 KB Output is correct
2 Correct 195 ms 143168 KB Output is correct
3 Correct 208 ms 141736 KB Output is correct
4 Correct 188 ms 141340 KB Output is correct
5 Correct 84 ms 132064 KB Output is correct
6 Correct 200 ms 141356 KB Output is correct
7 Correct 123 ms 132832 KB Output is correct
8 Correct 187 ms 143120 KB Output is correct
9 Correct 219 ms 141732 KB Output is correct
10 Correct 179 ms 141352 KB Output is correct
11 Correct 85 ms 131948 KB Output is correct
12 Correct 204 ms 141408 KB Output is correct
13 Correct 177 ms 141744 KB Output is correct
14 Correct 183 ms 141708 KB Output is correct
15 Correct 215 ms 141968 KB Output is correct
16 Correct 171 ms 140704 KB Output is correct
17 Correct 176 ms 140272 KB Output is correct
18 Correct 214 ms 141260 KB Output is correct
19 Correct 117 ms 132828 KB Output is correct
20 Correct 190 ms 143164 KB Output is correct
21 Correct 193 ms 141624 KB Output is correct
22 Correct 178 ms 141256 KB Output is correct
23 Correct 85 ms 131988 KB Output is correct
24 Correct 182 ms 141388 KB Output is correct
25 Correct 184 ms 141676 KB Output is correct
26 Correct 204 ms 141748 KB Output is correct
27 Correct 183 ms 142024 KB Output is correct
28 Correct 184 ms 140752 KB Output is correct
29 Correct 167 ms 140212 KB Output is correct
30 Correct 190 ms 141312 KB Output is correct
31 Correct 2855 ms 189428 KB Output is correct
32 Correct 2500 ms 195536 KB Output is correct
33 Correct 2360 ms 191272 KB Output is correct
34 Correct 2130 ms 195544 KB Output is correct
35 Correct 2572 ms 182488 KB Output is correct
36 Correct 2435 ms 185384 KB Output is correct
37 Correct 2626 ms 193668 KB Output is correct
38 Correct 121 ms 132888 KB Output is correct
39 Correct 185 ms 143060 KB Output is correct
40 Correct 186 ms 141616 KB Output is correct
41 Correct 176 ms 141300 KB Output is correct
42 Correct 87 ms 131988 KB Output is correct
43 Correct 174 ms 141428 KB Output is correct
44 Correct 173 ms 141692 KB Output is correct
45 Correct 224 ms 141796 KB Output is correct
46 Correct 176 ms 141976 KB Output is correct
47 Correct 184 ms 140756 KB Output is correct
48 Correct 174 ms 140260 KB Output is correct
49 Correct 172 ms 141260 KB Output is correct
50 Correct 2617 ms 189388 KB Output is correct
51 Correct 2558 ms 195476 KB Output is correct
52 Correct 2449 ms 191296 KB Output is correct
53 Correct 2188 ms 195584 KB Output is correct
54 Correct 2454 ms 182592 KB Output is correct
55 Correct 2430 ms 185416 KB Output is correct
56 Correct 2672 ms 193652 KB Output is correct
57 Correct 3011 ms 226364 KB Output is correct
58 Correct 2814 ms 258976 KB Output is correct
59 Correct 2603 ms 227000 KB Output is correct
60 Correct 2205 ms 191984 KB Output is correct
61 Correct 2637 ms 224900 KB Output is correct
62 Correct 2713 ms 205152 KB Output is correct
63 Correct 88 ms 131708 KB Output is correct
64 Correct 86 ms 131816 KB Output is correct
65 Correct 85 ms 131788 KB Output is correct
66 Correct 118 ms 132816 KB Output is correct
67 Correct 215 ms 143168 KB Output is correct
68 Correct 182 ms 141800 KB Output is correct
69 Correct 197 ms 141300 KB Output is correct
70 Correct 87 ms 132044 KB Output is correct
71 Correct 187 ms 141428 KB Output is correct
72 Correct 200 ms 141772 KB Output is correct
73 Correct 216 ms 141728 KB Output is correct
74 Correct 193 ms 141900 KB Output is correct
75 Correct 176 ms 140768 KB Output is correct
76 Correct 203 ms 140328 KB Output is correct
77 Correct 179 ms 141368 KB Output is correct
78 Correct 2788 ms 186548 KB Output is correct
79 Correct 2798 ms 181412 KB Output is correct
80 Correct 2651 ms 183576 KB Output is correct
81 Incorrect 2419 ms 186444 KB Output isn't correct
82 Halted 0 ms 0 KB -