답안 #429511

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
429511 2021-06-16T04:39:15 Z 반딧불(#7598) Brackets (CPSPC17_brackets) C++17
100 / 100
168 ms 8512 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

struct dat{
    int s, e, m; ll d;
    dat(){}
    dat(int s, int e, int m, ll d): s(s), e(e), m(m), d(d){}

    bool operator<(const dat &r)const{
        return d>r.d;
    }
};

int n, m, s, t;
int idx[128];
vector<int> link[202][8];
vector<int> revLink[202][8];
ll dist[202][202][5];
bool chk[202][202][5];

priority_queue<dat> pq;

int main(){
    idx['('] = 0, idx['['] = 1, idx['{'] = 2, idx['<'] = 3;
    idx[')'] = 4, idx[']'] = 5, idx['}'] = 6, idx['>'] = 7;
    scanf("%d %d %d %d", &n, &m, &s, &t);
    for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) for(int k=0; k<5; k++) dist[i][j][k] = 1e18;
    for(int i=1; i<=m; i++){
        int x, y; char c;
        scanf("%d %d %c", &x, &y, &c);
        link[x][idx[c]].push_back(y);
        revLink[y][idx[c]].push_back(x);
    }
    for(int i=1; i<=n; i++){
        for(int j=0; j<8; j++){
            sort(link[i][j].begin(), link[i][j].end());
            link[i][j].erase(unique(link[i][j].begin(), link[i][j].end()), link[i][j].end());

            sort(revLink[i][j].begin(), revLink[i][j].end());
            revLink[i][j].erase(unique(revLink[i][j].begin(), revLink[i][j].end()), revLink[i][j].end());

            for(auto x: link[i][j]){
                if(j>=4) continue;
                dist[i][x][j+1] = 1;
                pq.push(dat(i, x, j+1, 1));
            }
        }
    }

    for(int i=1; i<=n; i++){
        dist[i][i][0] = 0;
        pq.push(dat(i, i, 0, 0));
    }

    while(!pq.empty()){
        dat tmp = pq.top(); pq.pop();
        if(chk[tmp.s][tmp.e][tmp.m]) continue;
        chk[tmp.s][tmp.e][tmp.m] = 1;

        if(tmp.s == s && tmp.e == t && tmp.m == 0){
            printf("%lld\n", tmp.d);
            return 0;
        }

        if(tmp.m){
            tmp.m--;
            for(auto v: link[tmp.e][tmp.m+4]){
                ll nd = tmp.d + 1;
                if(nd >= dist[tmp.s][v][0]) continue;
                dist[tmp.s][v][0] = nd;
                pq.push(dat(tmp.s, v, 0, nd));
            }
            continue;
        }

        for(int i=1; i<=n; i++){
            if(chk[tmp.s][i][0]) continue;
            ll nd = tmp.d + dist[tmp.e][i][0];
            if(nd >= dist[tmp.s][i][0]) continue;
            dist[tmp.s][i][0] = nd;
            pq.push(dat(tmp.s, i, 0, nd));
        }

        for(int i=1; i<=n; i++){
            if(chk[i][tmp.e][0]) continue;
            ll nd = tmp.d + dist[i][tmp.s][0];
            if(nd >= dist[i][tmp.e][0]) continue;
            dist[i][tmp.e][0] = nd;
            pq.push(dat(i, tmp.e, 0, nd));
        }

        for(int k=0; k<4; k++){
            for(auto v: revLink[tmp.s][k]){
                ll nd = tmp.d + 1;
                if(nd >= dist[v][tmp.e][k+1]) continue;
                dist[v][tmp.e][k+1] = nd;
                pq.push(dat(v, tmp.e, k+1, nd));
            }
        }
    }

    printf("-1");
    return 0;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:34:21: warning: array subscript has type 'char' [-Wchar-subscripts]
   34 |         link[x][idx[c]].push_back(y);
      |                     ^
Main.cpp:35:24: warning: array subscript has type 'char' [-Wchar-subscripts]
   35 |         revLink[y][idx[c]].push_back(x);
      |                        ^
Main.cpp:29:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   29 |     scanf("%d %d %d %d", &n, &m, &s, &t);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:33:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   33 |         scanf("%d %d %c", &x, &y, &c);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 460 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 1 ms 460 KB Output is correct
16 Correct 1 ms 460 KB Output is correct
17 Correct 1 ms 460 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 460 KB Output is correct
20 Correct 1 ms 460 KB Output is correct
21 Correct 1 ms 460 KB Output is correct
22 Correct 1 ms 460 KB Output is correct
23 Correct 1 ms 460 KB Output is correct
24 Correct 1 ms 460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 460 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 1 ms 460 KB Output is correct
16 Correct 1 ms 460 KB Output is correct
17 Correct 1 ms 460 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 460 KB Output is correct
20 Correct 1 ms 460 KB Output is correct
21 Correct 1 ms 460 KB Output is correct
22 Correct 1 ms 460 KB Output is correct
23 Correct 1 ms 460 KB Output is correct
24 Correct 1 ms 460 KB Output is correct
25 Correct 1 ms 588 KB Output is correct
26 Correct 5 ms 972 KB Output is correct
27 Correct 2 ms 772 KB Output is correct
28 Correct 1 ms 716 KB Output is correct
29 Correct 4 ms 1036 KB Output is correct
30 Correct 5 ms 1040 KB Output is correct
31 Correct 2 ms 716 KB Output is correct
32 Correct 1 ms 716 KB Output is correct
33 Correct 3 ms 844 KB Output is correct
34 Correct 1 ms 716 KB Output is correct
35 Correct 3 ms 1220 KB Output is correct
36 Correct 4 ms 972 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 460 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 1 ms 460 KB Output is correct
16 Correct 1 ms 460 KB Output is correct
17 Correct 1 ms 460 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 460 KB Output is correct
20 Correct 1 ms 460 KB Output is correct
21 Correct 1 ms 460 KB Output is correct
22 Correct 1 ms 460 KB Output is correct
23 Correct 1 ms 460 KB Output is correct
24 Correct 1 ms 460 KB Output is correct
25 Correct 1 ms 588 KB Output is correct
26 Correct 5 ms 972 KB Output is correct
27 Correct 2 ms 772 KB Output is correct
28 Correct 1 ms 716 KB Output is correct
29 Correct 4 ms 1036 KB Output is correct
30 Correct 5 ms 1040 KB Output is correct
31 Correct 2 ms 716 KB Output is correct
32 Correct 1 ms 716 KB Output is correct
33 Correct 3 ms 844 KB Output is correct
34 Correct 1 ms 716 KB Output is correct
35 Correct 3 ms 1220 KB Output is correct
36 Correct 4 ms 972 KB Output is correct
37 Correct 4 ms 1228 KB Output is correct
38 Correct 10 ms 2124 KB Output is correct
39 Correct 3 ms 1228 KB Output is correct
40 Correct 1 ms 1228 KB Output is correct
41 Correct 10 ms 2184 KB Output is correct
42 Correct 2 ms 1228 KB Output is correct
43 Correct 2 ms 1228 KB Output is correct
44 Correct 3 ms 1228 KB Output is correct
45 Correct 15 ms 2252 KB Output is correct
46 Correct 21 ms 2124 KB Output is correct
47 Correct 4 ms 1356 KB Output is correct
48 Correct 24 ms 2124 KB Output is correct
49 Correct 1 ms 972 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 45 ms 3832 KB Output is correct
2 Correct 48 ms 3908 KB Output is correct
3 Correct 57 ms 3908 KB Output is correct
4 Correct 51 ms 3780 KB Output is correct
5 Correct 46 ms 3828 KB Output is correct
6 Correct 53 ms 3840 KB Output is correct
7 Correct 45 ms 3832 KB Output is correct
8 Correct 55 ms 3832 KB Output is correct
9 Correct 54 ms 3848 KB Output is correct
10 Correct 47 ms 3788 KB Output is correct
11 Correct 44 ms 3780 KB Output is correct
12 Correct 50 ms 3828 KB Output is correct
13 Correct 46 ms 3780 KB Output is correct
14 Correct 7 ms 2380 KB Output is correct
15 Correct 10 ms 2304 KB Output is correct
16 Correct 2 ms 2176 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 460 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 1 ms 460 KB Output is correct
16 Correct 1 ms 460 KB Output is correct
17 Correct 1 ms 460 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 460 KB Output is correct
20 Correct 1 ms 460 KB Output is correct
21 Correct 1 ms 460 KB Output is correct
22 Correct 1 ms 460 KB Output is correct
23 Correct 1 ms 460 KB Output is correct
24 Correct 1 ms 460 KB Output is correct
25 Correct 1 ms 588 KB Output is correct
26 Correct 5 ms 972 KB Output is correct
27 Correct 2 ms 772 KB Output is correct
28 Correct 1 ms 716 KB Output is correct
29 Correct 4 ms 1036 KB Output is correct
30 Correct 5 ms 1040 KB Output is correct
31 Correct 2 ms 716 KB Output is correct
32 Correct 1 ms 716 KB Output is correct
33 Correct 3 ms 844 KB Output is correct
34 Correct 1 ms 716 KB Output is correct
35 Correct 3 ms 1220 KB Output is correct
36 Correct 4 ms 972 KB Output is correct
37 Correct 4 ms 1228 KB Output is correct
38 Correct 10 ms 2124 KB Output is correct
39 Correct 3 ms 1228 KB Output is correct
40 Correct 1 ms 1228 KB Output is correct
41 Correct 10 ms 2184 KB Output is correct
42 Correct 2 ms 1228 KB Output is correct
43 Correct 2 ms 1228 KB Output is correct
44 Correct 3 ms 1228 KB Output is correct
45 Correct 15 ms 2252 KB Output is correct
46 Correct 21 ms 2124 KB Output is correct
47 Correct 4 ms 1356 KB Output is correct
48 Correct 24 ms 2124 KB Output is correct
49 Correct 1 ms 972 KB Output is correct
50 Correct 45 ms 3832 KB Output is correct
51 Correct 48 ms 3908 KB Output is correct
52 Correct 57 ms 3908 KB Output is correct
53 Correct 51 ms 3780 KB Output is correct
54 Correct 46 ms 3828 KB Output is correct
55 Correct 53 ms 3840 KB Output is correct
56 Correct 45 ms 3832 KB Output is correct
57 Correct 55 ms 3832 KB Output is correct
58 Correct 54 ms 3848 KB Output is correct
59 Correct 47 ms 3788 KB Output is correct
60 Correct 44 ms 3780 KB Output is correct
61 Correct 50 ms 3828 KB Output is correct
62 Correct 46 ms 3780 KB Output is correct
63 Correct 7 ms 2380 KB Output is correct
64 Correct 10 ms 2304 KB Output is correct
65 Correct 2 ms 2176 KB Output is correct
66 Correct 3 ms 2124 KB Output is correct
67 Correct 149 ms 8440 KB Output is correct
68 Correct 168 ms 8512 KB Output is correct
69 Correct 140 ms 8508 KB Output is correct
70 Correct 6 ms 2380 KB Output is correct
71 Correct 11 ms 2124 KB Output is correct
72 Correct 108 ms 5440 KB Output is correct
73 Correct 123 ms 5348 KB Output is correct
74 Correct 16 ms 2124 KB Output is correct
75 Correct 137 ms 5440 KB Output is correct
76 Correct 9 ms 2252 KB Output is correct
77 Correct 4 ms 1740 KB Output is correct
78 Correct 11 ms 2252 KB Output is correct
79 Correct 117 ms 5312 KB Output is correct
80 Correct 149 ms 8508 KB Output is correct
81 Correct 17 ms 2124 KB Output is correct
82 Correct 8 ms 1424 KB Output is correct
83 Correct 84 ms 5388 KB Output is correct