Submission #441621

# Submission time Handle Problem Language Result Execution time Memory
441621 2021-07-05T15:15:05 Z koioi.org-koosaga Brackets (CPSPC17_brackets) C++17
100 / 100
140 ms 8324 KB
#include <bits/stdc++.h>
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
using namespace std;
using lint = long long;
using pi = pair<int, int>;
const int MAXN = 205;
const int mod = 1e9 + 7;

vector<pi> gph[MAXN];
vector<pi> rev[MAXN];

struct node{
	int s, e, state;
	lint dist;
	bool operator<(const node &n)const{
		return dist > n.dist;
	}
};

lint dp[205][205][5];
int c2i[256];
int n, m, s, t;

int main(){
	{
		string s = "<>()[]{}";
		for(int i = 0; i < 8; i++) c2i[s[i]] = i;
	}
	scanf("%d %d %d %d",&n,&m,&s,&t);
	memset(dp, 0x3f, sizeof(dp));
	for(int i = 0; i < m; i++){
		int x, y; char s[7];
		scanf("%d %d %s",&x,&y,s);
		x--; y--;
		int j = c2i[s[0]];
		gph[x].emplace_back(j, y);
		rev[y].emplace_back(j, x);
	}
	priority_queue<node> pq;
	auto enq = [&](int s, int e, int state, lint d){
		if(dp[s][e][state] > d){
			dp[s][e][state] = d;
			pq.push({s, e, state, d});
		}
	};
	for(int i = 0; i < n; i++){
		enq(i, i, 0, 0);
	}
	while(sz(pq)){
		auto x = pq.top(); pq.pop();
		if(dp[x.s][x.e][x.state] != x.dist) continue;
		if(x.state){
			for(auto &[i, v] : gph[x.e]){
				if(i == 2 * x.state - 1){
					enq(x.s, v, 0, x.dist + 1);
				}
			}
		}
		else{
			for(int i = 0; i < n; i++){
				enq(x.s, i, 0, x.dist + dp[x.e][i][0]);
				enq(i, x.e, 0, x.dist + dp[i][x.s][0]);
			}
			for(auto &[i, v] : rev[x.s]){
				if(i % 2 == 0) enq(v, x.e, i / 2 + 1, x.dist + 1);
			}
		}
	}
	lint ans = dp[s-1][t-1][0];
	if(ans > 1'000'000'000'000'000'000L) ans = -1;
	cout << ans << endl;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:28:38: warning: array subscript has type 'char' [-Wchar-subscripts]
   28 |   for(int i = 0; i < 8; i++) c2i[s[i]] = i;
      |                                      ^
Main.cpp:36:18: warning: array subscript has type 'char' [-Wchar-subscripts]
   36 |   int j = c2i[s[0]];
      |               ~~~^
Main.cpp:30:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   30 |  scanf("%d %d %d %d",&n,&m,&s,&t);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:34:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   34 |   scanf("%d %d %s",&x,&y,s);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1868 KB Output is correct
2 Correct 1 ms 1868 KB Output is correct
3 Correct 1 ms 1840 KB Output is correct
4 Correct 1 ms 1868 KB Output is correct
5 Correct 1 ms 1868 KB Output is correct
6 Correct 1 ms 1868 KB Output is correct
7 Correct 1 ms 1868 KB Output is correct
8 Correct 1 ms 1868 KB Output is correct
9 Correct 1 ms 1868 KB Output is correct
10 Correct 1 ms 1868 KB Output is correct
11 Correct 1 ms 1868 KB Output is correct
12 Correct 1 ms 1868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1868 KB Output is correct
2 Correct 1 ms 1868 KB Output is correct
3 Correct 1 ms 1840 KB Output is correct
4 Correct 1 ms 1868 KB Output is correct
5 Correct 1 ms 1868 KB Output is correct
6 Correct 1 ms 1868 KB Output is correct
7 Correct 1 ms 1868 KB Output is correct
8 Correct 1 ms 1868 KB Output is correct
9 Correct 1 ms 1868 KB Output is correct
10 Correct 1 ms 1868 KB Output is correct
11 Correct 1 ms 1868 KB Output is correct
12 Correct 1 ms 1868 KB Output is correct
13 Correct 1 ms 1868 KB Output is correct
14 Correct 2 ms 1868 KB Output is correct
15 Correct 2 ms 1868 KB Output is correct
16 Correct 1 ms 1868 KB Output is correct
17 Correct 2 ms 1868 KB Output is correct
18 Correct 1 ms 1868 KB Output is correct
19 Correct 1 ms 1868 KB Output is correct
20 Correct 2 ms 1868 KB Output is correct
21 Correct 2 ms 1868 KB Output is correct
22 Correct 2 ms 1868 KB Output is correct
23 Correct 1 ms 1868 KB Output is correct
24 Correct 2 ms 1868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1868 KB Output is correct
2 Correct 1 ms 1868 KB Output is correct
3 Correct 1 ms 1840 KB Output is correct
4 Correct 1 ms 1868 KB Output is correct
5 Correct 1 ms 1868 KB Output is correct
6 Correct 1 ms 1868 KB Output is correct
7 Correct 1 ms 1868 KB Output is correct
8 Correct 1 ms 1868 KB Output is correct
9 Correct 1 ms 1868 KB Output is correct
10 Correct 1 ms 1868 KB Output is correct
11 Correct 1 ms 1868 KB Output is correct
12 Correct 1 ms 1868 KB Output is correct
13 Correct 1 ms 1868 KB Output is correct
14 Correct 2 ms 1868 KB Output is correct
15 Correct 2 ms 1868 KB Output is correct
16 Correct 1 ms 1868 KB Output is correct
17 Correct 2 ms 1868 KB Output is correct
18 Correct 1 ms 1868 KB Output is correct
19 Correct 1 ms 1868 KB Output is correct
20 Correct 2 ms 1868 KB Output is correct
21 Correct 2 ms 1868 KB Output is correct
22 Correct 2 ms 1868 KB Output is correct
23 Correct 1 ms 1868 KB Output is correct
24 Correct 2 ms 1868 KB Output is correct
25 Correct 2 ms 1868 KB Output is correct
26 Correct 4 ms 2252 KB Output is correct
27 Correct 2 ms 1996 KB Output is correct
28 Correct 1 ms 1868 KB Output is correct
29 Correct 4 ms 2252 KB Output is correct
30 Correct 6 ms 2316 KB Output is correct
31 Correct 2 ms 1868 KB Output is correct
32 Correct 2 ms 1868 KB Output is correct
33 Correct 3 ms 2124 KB Output is correct
34 Correct 1 ms 1868 KB Output is correct
35 Correct 6 ms 2444 KB Output is correct
36 Correct 5 ms 2232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1868 KB Output is correct
2 Correct 1 ms 1868 KB Output is correct
3 Correct 1 ms 1840 KB Output is correct
4 Correct 1 ms 1868 KB Output is correct
5 Correct 1 ms 1868 KB Output is correct
6 Correct 1 ms 1868 KB Output is correct
7 Correct 1 ms 1868 KB Output is correct
8 Correct 1 ms 1868 KB Output is correct
9 Correct 1 ms 1868 KB Output is correct
10 Correct 1 ms 1868 KB Output is correct
11 Correct 1 ms 1868 KB Output is correct
12 Correct 1 ms 1868 KB Output is correct
13 Correct 1 ms 1868 KB Output is correct
14 Correct 2 ms 1868 KB Output is correct
15 Correct 2 ms 1868 KB Output is correct
16 Correct 1 ms 1868 KB Output is correct
17 Correct 2 ms 1868 KB Output is correct
18 Correct 1 ms 1868 KB Output is correct
19 Correct 1 ms 1868 KB Output is correct
20 Correct 2 ms 1868 KB Output is correct
21 Correct 2 ms 1868 KB Output is correct
22 Correct 2 ms 1868 KB Output is correct
23 Correct 1 ms 1868 KB Output is correct
24 Correct 2 ms 1868 KB Output is correct
25 Correct 2 ms 1868 KB Output is correct
26 Correct 4 ms 2252 KB Output is correct
27 Correct 2 ms 1996 KB Output is correct
28 Correct 1 ms 1868 KB Output is correct
29 Correct 4 ms 2252 KB Output is correct
30 Correct 6 ms 2316 KB Output is correct
31 Correct 2 ms 1868 KB Output is correct
32 Correct 2 ms 1868 KB Output is correct
33 Correct 3 ms 2124 KB Output is correct
34 Correct 1 ms 1868 KB Output is correct
35 Correct 6 ms 2444 KB Output is correct
36 Correct 5 ms 2232 KB Output is correct
37 Correct 3 ms 1868 KB Output is correct
38 Correct 16 ms 2892 KB Output is correct
39 Correct 2 ms 1996 KB Output is correct
40 Correct 2 ms 1868 KB Output is correct
41 Correct 17 ms 2888 KB Output is correct
42 Correct 1 ms 1868 KB Output is correct
43 Correct 2 ms 1868 KB Output is correct
44 Correct 2 ms 1972 KB Output is correct
45 Correct 17 ms 2892 KB Output is correct
46 Correct 17 ms 2892 KB Output is correct
47 Correct 3 ms 2124 KB Output is correct
48 Correct 19 ms 2892 KB Output is correct
49 Correct 1 ms 1868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 44 ms 3652 KB Output is correct
2 Correct 44 ms 3656 KB Output is correct
3 Correct 60 ms 3656 KB Output is correct
4 Correct 47 ms 3656 KB Output is correct
5 Correct 40 ms 3656 KB Output is correct
6 Correct 41 ms 3620 KB Output is correct
7 Correct 42 ms 3656 KB Output is correct
8 Correct 43 ms 3652 KB Output is correct
9 Correct 41 ms 3704 KB Output is correct
10 Correct 40 ms 3656 KB Output is correct
11 Correct 41 ms 3636 KB Output is correct
12 Correct 40 ms 3628 KB Output is correct
13 Correct 37 ms 3652 KB Output is correct
14 Correct 7 ms 2192 KB Output is correct
15 Correct 7 ms 2108 KB Output is correct
16 Correct 2 ms 1868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1868 KB Output is correct
2 Correct 1 ms 1868 KB Output is correct
3 Correct 1 ms 1840 KB Output is correct
4 Correct 1 ms 1868 KB Output is correct
5 Correct 1 ms 1868 KB Output is correct
6 Correct 1 ms 1868 KB Output is correct
7 Correct 1 ms 1868 KB Output is correct
8 Correct 1 ms 1868 KB Output is correct
9 Correct 1 ms 1868 KB Output is correct
10 Correct 1 ms 1868 KB Output is correct
11 Correct 1 ms 1868 KB Output is correct
12 Correct 1 ms 1868 KB Output is correct
13 Correct 1 ms 1868 KB Output is correct
14 Correct 2 ms 1868 KB Output is correct
15 Correct 2 ms 1868 KB Output is correct
16 Correct 1 ms 1868 KB Output is correct
17 Correct 2 ms 1868 KB Output is correct
18 Correct 1 ms 1868 KB Output is correct
19 Correct 1 ms 1868 KB Output is correct
20 Correct 2 ms 1868 KB Output is correct
21 Correct 2 ms 1868 KB Output is correct
22 Correct 2 ms 1868 KB Output is correct
23 Correct 1 ms 1868 KB Output is correct
24 Correct 2 ms 1868 KB Output is correct
25 Correct 2 ms 1868 KB Output is correct
26 Correct 4 ms 2252 KB Output is correct
27 Correct 2 ms 1996 KB Output is correct
28 Correct 1 ms 1868 KB Output is correct
29 Correct 4 ms 2252 KB Output is correct
30 Correct 6 ms 2316 KB Output is correct
31 Correct 2 ms 1868 KB Output is correct
32 Correct 2 ms 1868 KB Output is correct
33 Correct 3 ms 2124 KB Output is correct
34 Correct 1 ms 1868 KB Output is correct
35 Correct 6 ms 2444 KB Output is correct
36 Correct 5 ms 2232 KB Output is correct
37 Correct 3 ms 1868 KB Output is correct
38 Correct 16 ms 2892 KB Output is correct
39 Correct 2 ms 1996 KB Output is correct
40 Correct 2 ms 1868 KB Output is correct
41 Correct 17 ms 2888 KB Output is correct
42 Correct 1 ms 1868 KB Output is correct
43 Correct 2 ms 1868 KB Output is correct
44 Correct 2 ms 1972 KB Output is correct
45 Correct 17 ms 2892 KB Output is correct
46 Correct 17 ms 2892 KB Output is correct
47 Correct 3 ms 2124 KB Output is correct
48 Correct 19 ms 2892 KB Output is correct
49 Correct 1 ms 1868 KB Output is correct
50 Correct 44 ms 3652 KB Output is correct
51 Correct 44 ms 3656 KB Output is correct
52 Correct 60 ms 3656 KB Output is correct
53 Correct 47 ms 3656 KB Output is correct
54 Correct 40 ms 3656 KB Output is correct
55 Correct 41 ms 3620 KB Output is correct
56 Correct 42 ms 3656 KB Output is correct
57 Correct 43 ms 3652 KB Output is correct
58 Correct 41 ms 3704 KB Output is correct
59 Correct 40 ms 3656 KB Output is correct
60 Correct 41 ms 3636 KB Output is correct
61 Correct 40 ms 3628 KB Output is correct
62 Correct 37 ms 3652 KB Output is correct
63 Correct 7 ms 2192 KB Output is correct
64 Correct 7 ms 2108 KB Output is correct
65 Correct 2 ms 1868 KB Output is correct
66 Correct 3 ms 1996 KB Output is correct
67 Correct 128 ms 8256 KB Output is correct
68 Correct 140 ms 8248 KB Output is correct
69 Correct 113 ms 8256 KB Output is correct
70 Correct 5 ms 2124 KB Output is correct
71 Correct 7 ms 1868 KB Output is correct
72 Correct 86 ms 5188 KB Output is correct
73 Correct 87 ms 5188 KB Output is correct
74 Correct 13 ms 1868 KB Output is correct
75 Correct 95 ms 5224 KB Output is correct
76 Correct 5 ms 2124 KB Output is correct
77 Correct 3 ms 1868 KB Output is correct
78 Correct 8 ms 2124 KB Output is correct
79 Correct 102 ms 5188 KB Output is correct
80 Correct 116 ms 8324 KB Output is correct
81 Correct 16 ms 1868 KB Output is correct
82 Correct 6 ms 2448 KB Output is correct
83 Correct 89 ms 5188 KB Output is correct