답안 #658699

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
658699 2022-11-14T12:45:24 Z Lobo From Hacks to Snitches (BOI21_watchmen) C++17
35 / 100
3452 ms 98188 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;

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];

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;
    priority_queue<pair<int,pair<int,int>>, vector<pair<int,pair<int,int>>>, greater<pair<int,pair<int,int>>>> pq;
    pq.push(mp(0,mp(1,0)));
    while(pq.size()) {
        int u = pq.top().sc.fr;
        int t = pq.top().sc.sc;
        int dist = pq.top().fr;
        pq.pop();

        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;
                        pq.push(mp(d[v][t1],mp(v,t1)));
                    }

                    // set<int> stt1;
                    // d1 = dlol[u]+1;
                    // while(true) {
                    //     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])) continue;
                    //     if(d[v][t1] > d1) {
                    //         d[v][t1] = d1;
                    //         pq.push(mp(d[v][t1],mp(v,t1)));
                    //     }
                    // }
                }
                else {
                    int d1 = dlol[u]+1;
                    int t1 = 0;
                    if(d[v][t1] > d1) {
                        d[v][t1] = d1;
                        pq.push(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;
                pq.push(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;
                pq.push(mp(d[v][t1],mp(v,t1)));
            }

            if(dlol[u] > d[u][t]) {
                dlol[u] = d[u][t];
                pq.push(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;
                pq.push(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;
                        pq.push(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;
                        pq.push(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;
                        pq.push(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:80:97: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   80 |                     if(t1 == thrt[v] || (thrt[u] == (thrt[v]+1)%szrt[idrt[u]] && t1 == thrt[u]) && idrt[u] == idrt[v]) continue;
      |                                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 15948 KB Output is correct
2 Correct 171 ms 23644 KB Output is correct
3 Correct 114 ms 23176 KB Output is correct
4 Correct 109 ms 23140 KB Output is correct
5 Correct 9 ms 14420 KB Output is correct
6 Correct 121 ms 23016 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 15936 KB Output is correct
2 Correct 136 ms 23764 KB Output is correct
3 Correct 103 ms 23104 KB Output is correct
4 Correct 111 ms 23144 KB Output is correct
5 Correct 10 ms 14500 KB Output is correct
6 Correct 104 ms 23012 KB Output is correct
7 Correct 104 ms 23032 KB Output is correct
8 Correct 123 ms 23036 KB Output is correct
9 Correct 114 ms 22992 KB Output is correct
10 Correct 102 ms 23276 KB Output is correct
11 Correct 106 ms 23044 KB Output is correct
12 Correct 133 ms 23068 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 15936 KB Output is correct
2 Correct 136 ms 23764 KB Output is correct
3 Correct 103 ms 23104 KB Output is correct
4 Correct 111 ms 23144 KB Output is correct
5 Correct 10 ms 14500 KB Output is correct
6 Correct 104 ms 23012 KB Output is correct
7 Correct 104 ms 23032 KB Output is correct
8 Correct 123 ms 23036 KB Output is correct
9 Correct 114 ms 22992 KB Output is correct
10 Correct 102 ms 23276 KB Output is correct
11 Correct 106 ms 23044 KB Output is correct
12 Correct 133 ms 23068 KB Output is correct
13 Correct 42 ms 15912 KB Output is correct
14 Correct 114 ms 23668 KB Output is correct
15 Correct 114 ms 23188 KB Output is correct
16 Correct 128 ms 23184 KB Output is correct
17 Correct 10 ms 14420 KB Output is correct
18 Correct 113 ms 23060 KB Output is correct
19 Correct 117 ms 22960 KB Output is correct
20 Correct 107 ms 23040 KB Output is correct
21 Correct 107 ms 23044 KB Output is correct
22 Correct 115 ms 23224 KB Output is correct
23 Correct 100 ms 23048 KB Output is correct
24 Correct 104 ms 23052 KB Output is correct
25 Correct 2687 ms 69160 KB Output is correct
26 Correct 2592 ms 74732 KB Output is correct
27 Correct 2660 ms 70404 KB Output is correct
28 Correct 2241 ms 74004 KB Output is correct
29 Correct 2608 ms 65336 KB Output is correct
30 Correct 2579 ms 70440 KB Output is correct
31 Correct 2809 ms 76568 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 15936 KB Output is correct
2 Correct 136 ms 23764 KB Output is correct
3 Correct 103 ms 23104 KB Output is correct
4 Correct 111 ms 23144 KB Output is correct
5 Correct 10 ms 14500 KB Output is correct
6 Correct 104 ms 23012 KB Output is correct
7 Correct 104 ms 23032 KB Output is correct
8 Correct 123 ms 23036 KB Output is correct
9 Correct 114 ms 22992 KB Output is correct
10 Correct 102 ms 23276 KB Output is correct
11 Correct 106 ms 23044 KB Output is correct
12 Correct 133 ms 23068 KB Output is correct
13 Correct 42 ms 15912 KB Output is correct
14 Correct 114 ms 23668 KB Output is correct
15 Correct 114 ms 23188 KB Output is correct
16 Correct 128 ms 23184 KB Output is correct
17 Correct 10 ms 14420 KB Output is correct
18 Correct 113 ms 23060 KB Output is correct
19 Correct 117 ms 22960 KB Output is correct
20 Correct 107 ms 23040 KB Output is correct
21 Correct 107 ms 23044 KB Output is correct
22 Correct 115 ms 23224 KB Output is correct
23 Correct 100 ms 23048 KB Output is correct
24 Correct 104 ms 23052 KB Output is correct
25 Correct 2687 ms 69160 KB Output is correct
26 Correct 2592 ms 74732 KB Output is correct
27 Correct 2660 ms 70404 KB Output is correct
28 Correct 2241 ms 74004 KB Output is correct
29 Correct 2608 ms 65336 KB Output is correct
30 Correct 2579 ms 70440 KB Output is correct
31 Correct 2809 ms 76568 KB Output is correct
32 Correct 43 ms 15904 KB Output is correct
33 Correct 111 ms 23676 KB Output is correct
34 Correct 148 ms 23184 KB Output is correct
35 Correct 128 ms 23168 KB Output is correct
36 Correct 10 ms 14420 KB Output is correct
37 Correct 108 ms 23000 KB Output is correct
38 Correct 112 ms 23036 KB Output is correct
39 Correct 116 ms 22972 KB Output is correct
40 Correct 144 ms 23044 KB Output is correct
41 Correct 109 ms 23416 KB Output is correct
42 Correct 117 ms 23108 KB Output is correct
43 Correct 129 ms 23072 KB Output is correct
44 Correct 2750 ms 69368 KB Output is correct
45 Correct 2759 ms 74972 KB Output is correct
46 Correct 2478 ms 68044 KB Output is correct
47 Correct 2277 ms 76140 KB Output is correct
48 Correct 2509 ms 62552 KB Output is correct
49 Correct 2509 ms 68244 KB Output is correct
50 Correct 2815 ms 74964 KB Output is correct
51 Correct 3206 ms 83548 KB Output is correct
52 Correct 3452 ms 98188 KB Output is correct
53 Correct 3088 ms 82068 KB Output is correct
54 Correct 2275 ms 71536 KB Output is correct
55 Correct 3030 ms 83688 KB Output is correct
56 Correct 3017 ms 74324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 15948 KB Output is correct
2 Correct 171 ms 23644 KB Output is correct
3 Correct 114 ms 23176 KB Output is correct
4 Correct 109 ms 23140 KB Output is correct
5 Correct 9 ms 14420 KB Output is correct
6 Correct 121 ms 23016 KB Output is correct
7 Correct 41 ms 15936 KB Output is correct
8 Correct 136 ms 23764 KB Output is correct
9 Correct 103 ms 23104 KB Output is correct
10 Correct 111 ms 23144 KB Output is correct
11 Correct 10 ms 14500 KB Output is correct
12 Correct 104 ms 23012 KB Output is correct
13 Correct 104 ms 23032 KB Output is correct
14 Correct 123 ms 23036 KB Output is correct
15 Correct 114 ms 22992 KB Output is correct
16 Correct 102 ms 23276 KB Output is correct
17 Correct 106 ms 23044 KB Output is correct
18 Correct 133 ms 23068 KB Output is correct
19 Correct 8 ms 14420 KB Output is correct
20 Correct 7 ms 14420 KB Output is correct
21 Correct 8 ms 14400 KB Output is correct
22 Correct 42 ms 15948 KB Output is correct
23 Correct 123 ms 23624 KB Output is correct
24 Correct 122 ms 23116 KB Output is correct
25 Correct 124 ms 23080 KB Output is correct
26 Correct 10 ms 14488 KB Output is correct
27 Correct 120 ms 22972 KB Output is correct
28 Correct 135 ms 22976 KB Output is correct
29 Correct 115 ms 22980 KB Output is correct
30 Correct 97 ms 22976 KB Output is correct
31 Correct 106 ms 23304 KB Output is correct
32 Correct 105 ms 23116 KB Output is correct
33 Correct 123 ms 23000 KB Output is correct
34 Correct 2671 ms 66484 KB Output is correct
35 Correct 2800 ms 65368 KB Output is correct
36 Correct 2815 ms 63364 KB Output is correct
37 Incorrect 2611 ms 68224 KB Output isn't correct
38 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 15948 KB Output is correct
2 Correct 171 ms 23644 KB Output is correct
3 Correct 114 ms 23176 KB Output is correct
4 Correct 109 ms 23140 KB Output is correct
5 Correct 9 ms 14420 KB Output is correct
6 Correct 121 ms 23016 KB Output is correct
7 Correct 41 ms 15936 KB Output is correct
8 Correct 136 ms 23764 KB Output is correct
9 Correct 103 ms 23104 KB Output is correct
10 Correct 111 ms 23144 KB Output is correct
11 Correct 10 ms 14500 KB Output is correct
12 Correct 104 ms 23012 KB Output is correct
13 Correct 104 ms 23032 KB Output is correct
14 Correct 123 ms 23036 KB Output is correct
15 Correct 114 ms 22992 KB Output is correct
16 Correct 102 ms 23276 KB Output is correct
17 Correct 106 ms 23044 KB Output is correct
18 Correct 133 ms 23068 KB Output is correct
19 Correct 42 ms 15912 KB Output is correct
20 Correct 114 ms 23668 KB Output is correct
21 Correct 114 ms 23188 KB Output is correct
22 Correct 128 ms 23184 KB Output is correct
23 Correct 10 ms 14420 KB Output is correct
24 Correct 113 ms 23060 KB Output is correct
25 Correct 117 ms 22960 KB Output is correct
26 Correct 107 ms 23040 KB Output is correct
27 Correct 107 ms 23044 KB Output is correct
28 Correct 115 ms 23224 KB Output is correct
29 Correct 100 ms 23048 KB Output is correct
30 Correct 104 ms 23052 KB Output is correct
31 Correct 2687 ms 69160 KB Output is correct
32 Correct 2592 ms 74732 KB Output is correct
33 Correct 2660 ms 70404 KB Output is correct
34 Correct 2241 ms 74004 KB Output is correct
35 Correct 2608 ms 65336 KB Output is correct
36 Correct 2579 ms 70440 KB Output is correct
37 Correct 2809 ms 76568 KB Output is correct
38 Correct 8 ms 14420 KB Output is correct
39 Correct 7 ms 14420 KB Output is correct
40 Correct 8 ms 14400 KB Output is correct
41 Correct 42 ms 15948 KB Output is correct
42 Correct 123 ms 23624 KB Output is correct
43 Correct 122 ms 23116 KB Output is correct
44 Correct 124 ms 23080 KB Output is correct
45 Correct 10 ms 14488 KB Output is correct
46 Correct 120 ms 22972 KB Output is correct
47 Correct 135 ms 22976 KB Output is correct
48 Correct 115 ms 22980 KB Output is correct
49 Correct 97 ms 22976 KB Output is correct
50 Correct 106 ms 23304 KB Output is correct
51 Correct 105 ms 23116 KB Output is correct
52 Correct 123 ms 23000 KB Output is correct
53 Correct 2671 ms 66484 KB Output is correct
54 Correct 2800 ms 65368 KB Output is correct
55 Correct 2815 ms 63364 KB Output is correct
56 Incorrect 2611 ms 68224 KB Output isn't correct
57 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 15948 KB Output is correct
2 Correct 171 ms 23644 KB Output is correct
3 Correct 114 ms 23176 KB Output is correct
4 Correct 109 ms 23140 KB Output is correct
5 Correct 9 ms 14420 KB Output is correct
6 Correct 121 ms 23016 KB Output is correct
7 Correct 41 ms 15936 KB Output is correct
8 Correct 136 ms 23764 KB Output is correct
9 Correct 103 ms 23104 KB Output is correct
10 Correct 111 ms 23144 KB Output is correct
11 Correct 10 ms 14500 KB Output is correct
12 Correct 104 ms 23012 KB Output is correct
13 Correct 104 ms 23032 KB Output is correct
14 Correct 123 ms 23036 KB Output is correct
15 Correct 114 ms 22992 KB Output is correct
16 Correct 102 ms 23276 KB Output is correct
17 Correct 106 ms 23044 KB Output is correct
18 Correct 133 ms 23068 KB Output is correct
19 Correct 42 ms 15912 KB Output is correct
20 Correct 114 ms 23668 KB Output is correct
21 Correct 114 ms 23188 KB Output is correct
22 Correct 128 ms 23184 KB Output is correct
23 Correct 10 ms 14420 KB Output is correct
24 Correct 113 ms 23060 KB Output is correct
25 Correct 117 ms 22960 KB Output is correct
26 Correct 107 ms 23040 KB Output is correct
27 Correct 107 ms 23044 KB Output is correct
28 Correct 115 ms 23224 KB Output is correct
29 Correct 100 ms 23048 KB Output is correct
30 Correct 104 ms 23052 KB Output is correct
31 Correct 2687 ms 69160 KB Output is correct
32 Correct 2592 ms 74732 KB Output is correct
33 Correct 2660 ms 70404 KB Output is correct
34 Correct 2241 ms 74004 KB Output is correct
35 Correct 2608 ms 65336 KB Output is correct
36 Correct 2579 ms 70440 KB Output is correct
37 Correct 2809 ms 76568 KB Output is correct
38 Correct 43 ms 15904 KB Output is correct
39 Correct 111 ms 23676 KB Output is correct
40 Correct 148 ms 23184 KB Output is correct
41 Correct 128 ms 23168 KB Output is correct
42 Correct 10 ms 14420 KB Output is correct
43 Correct 108 ms 23000 KB Output is correct
44 Correct 112 ms 23036 KB Output is correct
45 Correct 116 ms 22972 KB Output is correct
46 Correct 144 ms 23044 KB Output is correct
47 Correct 109 ms 23416 KB Output is correct
48 Correct 117 ms 23108 KB Output is correct
49 Correct 129 ms 23072 KB Output is correct
50 Correct 2750 ms 69368 KB Output is correct
51 Correct 2759 ms 74972 KB Output is correct
52 Correct 2478 ms 68044 KB Output is correct
53 Correct 2277 ms 76140 KB Output is correct
54 Correct 2509 ms 62552 KB Output is correct
55 Correct 2509 ms 68244 KB Output is correct
56 Correct 2815 ms 74964 KB Output is correct
57 Correct 3206 ms 83548 KB Output is correct
58 Correct 3452 ms 98188 KB Output is correct
59 Correct 3088 ms 82068 KB Output is correct
60 Correct 2275 ms 71536 KB Output is correct
61 Correct 3030 ms 83688 KB Output is correct
62 Correct 3017 ms 74324 KB Output is correct
63 Correct 8 ms 14420 KB Output is correct
64 Correct 7 ms 14420 KB Output is correct
65 Correct 8 ms 14400 KB Output is correct
66 Correct 42 ms 15948 KB Output is correct
67 Correct 123 ms 23624 KB Output is correct
68 Correct 122 ms 23116 KB Output is correct
69 Correct 124 ms 23080 KB Output is correct
70 Correct 10 ms 14488 KB Output is correct
71 Correct 120 ms 22972 KB Output is correct
72 Correct 135 ms 22976 KB Output is correct
73 Correct 115 ms 22980 KB Output is correct
74 Correct 97 ms 22976 KB Output is correct
75 Correct 106 ms 23304 KB Output is correct
76 Correct 105 ms 23116 KB Output is correct
77 Correct 123 ms 23000 KB Output is correct
78 Correct 2671 ms 66484 KB Output is correct
79 Correct 2800 ms 65368 KB Output is correct
80 Correct 2815 ms 63364 KB Output is correct
81 Incorrect 2611 ms 68224 KB Output isn't correct
82 Halted 0 ms 0 KB -