답안 #658709

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
658709 2022-11-14T13:15:12 Z Lobo From Hacks to Snitches (BOI21_watchmen) C++17
35 / 100
3427 ms 394232 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 = 1e7;

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,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(0,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.sc.fr;
        int t = X.sc.sc;
        int dist = X.fr;
        if(t != -1 && dist != d[u][t]) continue;
        if(t == -1 && dist != 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(d[v][t1],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(d[v][t1],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(d[v][t1],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(d[v][t1],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(d[v][t1],mp(v,t1)));
            }

            if(dlol[u] > d[u][t]) {
                dlol[u] = d[u][t];
                withdist[dlol[u]].pb(mp(dlol[u],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(d[v][t1],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(d[v][t1],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(d[v][t1],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(d[v][t1],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:82:97: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   82 |                     if(t1 == thrt[v] || (thrt[u] == (thrt[v]+1)%szrt[idrt[u]] && t1 == thrt[u]) && idrt[u] == idrt[v]) continue;
      |                                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 156 ms 250368 KB Output is correct
2 Correct 242 ms 260600 KB Output is correct
3 Correct 239 ms 259224 KB Output is correct
4 Correct 212 ms 259000 KB Output is correct
5 Correct 126 ms 249468 KB Output is correct
6 Correct 237 ms 258952 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 164 ms 250316 KB Output is correct
2 Correct 232 ms 260596 KB Output is correct
3 Correct 237 ms 259276 KB Output is correct
4 Correct 250 ms 259112 KB Output is correct
5 Correct 125 ms 249532 KB Output is correct
6 Correct 271 ms 258960 KB Output is correct
7 Correct 219 ms 259196 KB Output is correct
8 Correct 218 ms 259384 KB Output is correct
9 Correct 227 ms 259348 KB Output is correct
10 Correct 216 ms 258548 KB Output is correct
11 Correct 208 ms 257968 KB Output is correct
12 Correct 256 ms 258920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 164 ms 250316 KB Output is correct
2 Correct 232 ms 260596 KB Output is correct
3 Correct 237 ms 259276 KB Output is correct
4 Correct 250 ms 259112 KB Output is correct
5 Correct 125 ms 249532 KB Output is correct
6 Correct 271 ms 258960 KB Output is correct
7 Correct 219 ms 259196 KB Output is correct
8 Correct 218 ms 259384 KB Output is correct
9 Correct 227 ms 259348 KB Output is correct
10 Correct 216 ms 258548 KB Output is correct
11 Correct 208 ms 257968 KB Output is correct
12 Correct 256 ms 258920 KB Output is correct
13 Correct 160 ms 250360 KB Output is correct
14 Correct 246 ms 260544 KB Output is correct
15 Correct 225 ms 259168 KB Output is correct
16 Correct 226 ms 258988 KB Output is correct
17 Correct 127 ms 249420 KB Output is correct
18 Correct 233 ms 258976 KB Output is correct
19 Correct 224 ms 259116 KB Output is correct
20 Correct 262 ms 259208 KB Output is correct
21 Correct 233 ms 259404 KB Output is correct
22 Correct 213 ms 258516 KB Output is correct
23 Correct 208 ms 258072 KB Output is correct
24 Correct 229 ms 258812 KB Output is correct
25 Correct 2877 ms 307116 KB Output is correct
26 Correct 2739 ms 313676 KB Output is correct
27 Correct 2661 ms 308996 KB Output is correct
28 Correct 2347 ms 313188 KB Output is correct
29 Correct 2603 ms 300532 KB Output is correct
30 Correct 2666 ms 304320 KB Output is correct
31 Correct 2741 ms 311632 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 164 ms 250316 KB Output is correct
2 Correct 232 ms 260596 KB Output is correct
3 Correct 237 ms 259276 KB Output is correct
4 Correct 250 ms 259112 KB Output is correct
5 Correct 125 ms 249532 KB Output is correct
6 Correct 271 ms 258960 KB Output is correct
7 Correct 219 ms 259196 KB Output is correct
8 Correct 218 ms 259384 KB Output is correct
9 Correct 227 ms 259348 KB Output is correct
10 Correct 216 ms 258548 KB Output is correct
11 Correct 208 ms 257968 KB Output is correct
12 Correct 256 ms 258920 KB Output is correct
13 Correct 160 ms 250360 KB Output is correct
14 Correct 246 ms 260544 KB Output is correct
15 Correct 225 ms 259168 KB Output is correct
16 Correct 226 ms 258988 KB Output is correct
17 Correct 127 ms 249420 KB Output is correct
18 Correct 233 ms 258976 KB Output is correct
19 Correct 224 ms 259116 KB Output is correct
20 Correct 262 ms 259208 KB Output is correct
21 Correct 233 ms 259404 KB Output is correct
22 Correct 213 ms 258516 KB Output is correct
23 Correct 208 ms 258072 KB Output is correct
24 Correct 229 ms 258812 KB Output is correct
25 Correct 2877 ms 307116 KB Output is correct
26 Correct 2739 ms 313676 KB Output is correct
27 Correct 2661 ms 308996 KB Output is correct
28 Correct 2347 ms 313188 KB Output is correct
29 Correct 2603 ms 300532 KB Output is correct
30 Correct 2666 ms 304320 KB Output is correct
31 Correct 2741 ms 311632 KB Output is correct
32 Correct 161 ms 250316 KB Output is correct
33 Correct 246 ms 260512 KB Output is correct
34 Correct 224 ms 259392 KB Output is correct
35 Correct 236 ms 259008 KB Output is correct
36 Correct 127 ms 249580 KB Output is correct
37 Correct 226 ms 259056 KB Output is correct
38 Correct 260 ms 259172 KB Output is correct
39 Correct 222 ms 259240 KB Output is correct
40 Correct 219 ms 259412 KB Output is correct
41 Correct 264 ms 258560 KB Output is correct
42 Correct 226 ms 258124 KB Output is correct
43 Correct 219 ms 258900 KB Output is correct
44 Correct 2729 ms 307156 KB Output is correct
45 Correct 2958 ms 313508 KB Output is correct
46 Correct 2606 ms 308928 KB Output is correct
47 Correct 2444 ms 313016 KB Output is correct
48 Correct 2698 ms 300552 KB Output is correct
49 Correct 2649 ms 304176 KB Output is correct
50 Correct 2835 ms 311844 KB Output is correct
51 Correct 2959 ms 354720 KB Output is correct
52 Correct 2829 ms 394232 KB Output is correct
53 Correct 2743 ms 356280 KB Output is correct
54 Correct 2283 ms 311300 KB Output is correct
55 Correct 3427 ms 351704 KB Output is correct
56 Correct 2774 ms 331144 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 156 ms 250368 KB Output is correct
2 Correct 242 ms 260600 KB Output is correct
3 Correct 239 ms 259224 KB Output is correct
4 Correct 212 ms 259000 KB Output is correct
5 Correct 126 ms 249468 KB Output is correct
6 Correct 237 ms 258952 KB Output is correct
7 Correct 164 ms 250316 KB Output is correct
8 Correct 232 ms 260596 KB Output is correct
9 Correct 237 ms 259276 KB Output is correct
10 Correct 250 ms 259112 KB Output is correct
11 Correct 125 ms 249532 KB Output is correct
12 Correct 271 ms 258960 KB Output is correct
13 Correct 219 ms 259196 KB Output is correct
14 Correct 218 ms 259384 KB Output is correct
15 Correct 227 ms 259348 KB Output is correct
16 Correct 216 ms 258548 KB Output is correct
17 Correct 208 ms 257968 KB Output is correct
18 Correct 256 ms 258920 KB Output is correct
19 Correct 140 ms 249196 KB Output is correct
20 Correct 156 ms 249168 KB Output is correct
21 Correct 129 ms 249164 KB Output is correct
22 Correct 160 ms 250356 KB Output is correct
23 Correct 251 ms 260596 KB Output is correct
24 Correct 232 ms 259156 KB Output is correct
25 Correct 214 ms 258988 KB Output is correct
26 Correct 131 ms 249484 KB Output is correct
27 Correct 255 ms 259012 KB Output is correct
28 Correct 219 ms 259192 KB Output is correct
29 Correct 233 ms 259264 KB Output is correct
30 Correct 265 ms 259496 KB Output is correct
31 Correct 221 ms 258476 KB Output is correct
32 Correct 212 ms 258112 KB Output is correct
33 Correct 226 ms 258832 KB Output is correct
34 Correct 2718 ms 304512 KB Output is correct
35 Correct 2914 ms 299136 KB Output is correct
36 Correct 2860 ms 301476 KB Output is correct
37 Incorrect 2575 ms 304132 KB Output isn't correct
38 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 156 ms 250368 KB Output is correct
2 Correct 242 ms 260600 KB Output is correct
3 Correct 239 ms 259224 KB Output is correct
4 Correct 212 ms 259000 KB Output is correct
5 Correct 126 ms 249468 KB Output is correct
6 Correct 237 ms 258952 KB Output is correct
7 Correct 164 ms 250316 KB Output is correct
8 Correct 232 ms 260596 KB Output is correct
9 Correct 237 ms 259276 KB Output is correct
10 Correct 250 ms 259112 KB Output is correct
11 Correct 125 ms 249532 KB Output is correct
12 Correct 271 ms 258960 KB Output is correct
13 Correct 219 ms 259196 KB Output is correct
14 Correct 218 ms 259384 KB Output is correct
15 Correct 227 ms 259348 KB Output is correct
16 Correct 216 ms 258548 KB Output is correct
17 Correct 208 ms 257968 KB Output is correct
18 Correct 256 ms 258920 KB Output is correct
19 Correct 160 ms 250360 KB Output is correct
20 Correct 246 ms 260544 KB Output is correct
21 Correct 225 ms 259168 KB Output is correct
22 Correct 226 ms 258988 KB Output is correct
23 Correct 127 ms 249420 KB Output is correct
24 Correct 233 ms 258976 KB Output is correct
25 Correct 224 ms 259116 KB Output is correct
26 Correct 262 ms 259208 KB Output is correct
27 Correct 233 ms 259404 KB Output is correct
28 Correct 213 ms 258516 KB Output is correct
29 Correct 208 ms 258072 KB Output is correct
30 Correct 229 ms 258812 KB Output is correct
31 Correct 2877 ms 307116 KB Output is correct
32 Correct 2739 ms 313676 KB Output is correct
33 Correct 2661 ms 308996 KB Output is correct
34 Correct 2347 ms 313188 KB Output is correct
35 Correct 2603 ms 300532 KB Output is correct
36 Correct 2666 ms 304320 KB Output is correct
37 Correct 2741 ms 311632 KB Output is correct
38 Correct 140 ms 249196 KB Output is correct
39 Correct 156 ms 249168 KB Output is correct
40 Correct 129 ms 249164 KB Output is correct
41 Correct 160 ms 250356 KB Output is correct
42 Correct 251 ms 260596 KB Output is correct
43 Correct 232 ms 259156 KB Output is correct
44 Correct 214 ms 258988 KB Output is correct
45 Correct 131 ms 249484 KB Output is correct
46 Correct 255 ms 259012 KB Output is correct
47 Correct 219 ms 259192 KB Output is correct
48 Correct 233 ms 259264 KB Output is correct
49 Correct 265 ms 259496 KB Output is correct
50 Correct 221 ms 258476 KB Output is correct
51 Correct 212 ms 258112 KB Output is correct
52 Correct 226 ms 258832 KB Output is correct
53 Correct 2718 ms 304512 KB Output is correct
54 Correct 2914 ms 299136 KB Output is correct
55 Correct 2860 ms 301476 KB Output is correct
56 Incorrect 2575 ms 304132 KB Output isn't correct
57 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 156 ms 250368 KB Output is correct
2 Correct 242 ms 260600 KB Output is correct
3 Correct 239 ms 259224 KB Output is correct
4 Correct 212 ms 259000 KB Output is correct
5 Correct 126 ms 249468 KB Output is correct
6 Correct 237 ms 258952 KB Output is correct
7 Correct 164 ms 250316 KB Output is correct
8 Correct 232 ms 260596 KB Output is correct
9 Correct 237 ms 259276 KB Output is correct
10 Correct 250 ms 259112 KB Output is correct
11 Correct 125 ms 249532 KB Output is correct
12 Correct 271 ms 258960 KB Output is correct
13 Correct 219 ms 259196 KB Output is correct
14 Correct 218 ms 259384 KB Output is correct
15 Correct 227 ms 259348 KB Output is correct
16 Correct 216 ms 258548 KB Output is correct
17 Correct 208 ms 257968 KB Output is correct
18 Correct 256 ms 258920 KB Output is correct
19 Correct 160 ms 250360 KB Output is correct
20 Correct 246 ms 260544 KB Output is correct
21 Correct 225 ms 259168 KB Output is correct
22 Correct 226 ms 258988 KB Output is correct
23 Correct 127 ms 249420 KB Output is correct
24 Correct 233 ms 258976 KB Output is correct
25 Correct 224 ms 259116 KB Output is correct
26 Correct 262 ms 259208 KB Output is correct
27 Correct 233 ms 259404 KB Output is correct
28 Correct 213 ms 258516 KB Output is correct
29 Correct 208 ms 258072 KB Output is correct
30 Correct 229 ms 258812 KB Output is correct
31 Correct 2877 ms 307116 KB Output is correct
32 Correct 2739 ms 313676 KB Output is correct
33 Correct 2661 ms 308996 KB Output is correct
34 Correct 2347 ms 313188 KB Output is correct
35 Correct 2603 ms 300532 KB Output is correct
36 Correct 2666 ms 304320 KB Output is correct
37 Correct 2741 ms 311632 KB Output is correct
38 Correct 161 ms 250316 KB Output is correct
39 Correct 246 ms 260512 KB Output is correct
40 Correct 224 ms 259392 KB Output is correct
41 Correct 236 ms 259008 KB Output is correct
42 Correct 127 ms 249580 KB Output is correct
43 Correct 226 ms 259056 KB Output is correct
44 Correct 260 ms 259172 KB Output is correct
45 Correct 222 ms 259240 KB Output is correct
46 Correct 219 ms 259412 KB Output is correct
47 Correct 264 ms 258560 KB Output is correct
48 Correct 226 ms 258124 KB Output is correct
49 Correct 219 ms 258900 KB Output is correct
50 Correct 2729 ms 307156 KB Output is correct
51 Correct 2958 ms 313508 KB Output is correct
52 Correct 2606 ms 308928 KB Output is correct
53 Correct 2444 ms 313016 KB Output is correct
54 Correct 2698 ms 300552 KB Output is correct
55 Correct 2649 ms 304176 KB Output is correct
56 Correct 2835 ms 311844 KB Output is correct
57 Correct 2959 ms 354720 KB Output is correct
58 Correct 2829 ms 394232 KB Output is correct
59 Correct 2743 ms 356280 KB Output is correct
60 Correct 2283 ms 311300 KB Output is correct
61 Correct 3427 ms 351704 KB Output is correct
62 Correct 2774 ms 331144 KB Output is correct
63 Correct 140 ms 249196 KB Output is correct
64 Correct 156 ms 249168 KB Output is correct
65 Correct 129 ms 249164 KB Output is correct
66 Correct 160 ms 250356 KB Output is correct
67 Correct 251 ms 260596 KB Output is correct
68 Correct 232 ms 259156 KB Output is correct
69 Correct 214 ms 258988 KB Output is correct
70 Correct 131 ms 249484 KB Output is correct
71 Correct 255 ms 259012 KB Output is correct
72 Correct 219 ms 259192 KB Output is correct
73 Correct 233 ms 259264 KB Output is correct
74 Correct 265 ms 259496 KB Output is correct
75 Correct 221 ms 258476 KB Output is correct
76 Correct 212 ms 258112 KB Output is correct
77 Correct 226 ms 258832 KB Output is correct
78 Correct 2718 ms 304512 KB Output is correct
79 Correct 2914 ms 299136 KB Output is correct
80 Correct 2860 ms 301476 KB Output is correct
81 Incorrect 2575 ms 304132 KB Output isn't correct
82 Halted 0 ms 0 KB -