Submission #817693

# Submission time Handle Problem Language Result Execution time Memory
817693 2023-08-09T15:05:15 Z MohamedAhmed04 From Hacks to Snitches (BOI21_watchmen) C++17
35 / 100
4610 ms 297504 KB
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>

using namespace std ;

const int inf = 1e9 ;
const int MAX = 3e5 + 10 ;
const int K = 30 ;

int arr[MAX] ;
int n , m , k ;

vector< vector<int> >adj(MAX) ;

vector<int>v[MAX] ;
int watch[MAX] , cycle_sz[MAX] , id[MAX] ;

int dist[MAX][K] ;

int calc[MAX][205] ;

bool check(int node , int child , int tim)
{
	assert(watch[child] != 0) ;
	int x = watch[node] ;
	int tim_node = 2e9 ;
	if(watch[node])
	{
		int cur = tim % cycle_sz[x] ;
		tim_node = tim + (id[node] - cur + cycle_sz[x]) % cycle_sz[x] ;
	}
	int tim_child = -2e9 ;
	x = watch[child] ;
	int cur = tim % cycle_sz[x] ;
	tim_child = tim + (id[child] - cur + cycle_sz[x]) % cycle_sz[x] ;
	return (tim_child < tim_node-1 || (tim_child == tim_node - 1 && watch[child] != watch[node])) ;
}

void solve(int src)
{
	for(int i = 1 ; i <= n ; ++i)
		dist[i][0] = dist[i][1] = inf ;
	priority_queue< array<int , 3> , vector< array<int , 3> > , greater< array<int , 3> > >q ;
	dist[src][0] = 0 ;
	q.push({0 , src , 0}) ;
	while(!q.empty())
	{
		array<int , 3>a = q.top() ;
		q.pop() ;
		int node = a[1] , cost = a[0] , t = a[2] ;
		if(dist[node][t] < cost)
			continue ;
		for(auto &child : adj[node])
		{
			int child2 = child ;
			int cost2 = cost + 1 ;
			if((!watch[child2]))
			{
				if(cost2 < dist[child2][0])
				{
					dist[child2][0] = cost2 ;
					q.push({cost2 , child2 , 0}) ;
				}
				continue ;
			}
			int x = watch[child2] ;
			if(check(node , child2 , cost)) //handle waiting for some time
			{
				int cur = cost % cycle_sz[x] ;
				cost2 = cost + (id[child2] - cur + cycle_sz[x]) % cycle_sz[x] + 1 ; 
				if(cost2 < dist[child2][1])
				{
					dist[child2][1] = cost2 ;
					q.push({cost2 , child2 , 1}) ;
				}
			}
			cost2 = cost + 1 ;
			//handle corridor 
			if(v[x][(cost2 - 1) % cycle_sz[x]] == child2 && v[x][cost2 % cycle_sz[x]] == node)
				continue ;
			if(v[x][cost2 % cycle_sz[x]] != child2) //go without waiting
			{
				if(cost2 < dist[child2][t])
				{
					dist[child2][t] = cost2 ;
					q.push({cost2 , child2 , t}) ;
				}
			}
		}
	}
}

void solve2(int src)
{
	for(int i = 1 ; i <= n ; ++i)
	{
		for(int j = 0 ; j < K ; ++j)
			dist[i][j] = inf ;
	}
	queue< pair<int , int> >q ;
	dist[src][0] = 0 ;
	q.push({src , 0}) ;
	while(!q.empty())
	{
		pair<int , int>p = q.front() ;
		q.pop() ;
		int node = p.first , wait = p.second ;
		int cost = dist[node][wait] , x = watch[node] ;
		if(node == n)
			return ;
		if((!x) || (v[x][calc[cost + 1][cycle_sz[x]]] != node))
		{
			if(wait+1 < K && dist[node][wait+1] == inf)
			{
				dist[node][wait+1] = cost + 1 ;
				q.push({node , wait+1}) ;
			}
		}
		for(auto &child : adj[node])
		{
			int child2 = child ;
			int cost2 = cost + 1 ;
			x = watch[child] ;
			if(x > 0 && v[x][calc[cost2 - 1][cycle_sz[x]]] == child2 && v[x][calc[cost2][cycle_sz[x]]] == node)
				continue ;
			if(x > 0 && v[x][calc[cost2][cycle_sz[x]]] == child2)
				continue ;
			if(dist[child2][wait] == inf)
			{
				dist[child2][wait] = cost2 ;
				q.push({child2 , wait}) ;
			}
		}
	}
}

int main()
{
	ios_base::sync_with_stdio(0) ;
	cin.tie(0) ;
	cin>>n>>m ;
	for(int i = 0 ; i < m ; ++i)
	{
		int x , y ;
		cin>>x>>y ;
		adj[x].push_back(y) ;
		adj[y].push_back(x) ;
	}
	cin>>k ;
	for(int i = 1 ; i <= k ; ++i)
	{
		cin>>cycle_sz[i] ;
		v[i].resize(cycle_sz[i]) ;
		for(int j = 0 ; j < cycle_sz[i] ; ++j)
		{
			cin>>v[i][j] ;
			watch[v[i][j]] = i ;
			id[v[i][j]] = j ;
		}
	}
	bool flag = true ;
	for(int i = 1 ; i <= n ; ++i)
	{
		for(auto &child : adj[i])
			flag &= (watch[i] == 0 || watch[child] == 0 || watch[i] == watch[child]) ;
	}
	if(flag) //no corridor connects two two different routes
	{
		solve(1) ;
		if(dist[n][0] == inf)
			cout<<"impossible\n" ;
		else
			cout<<dist[n][0]<<"\n" ;
	}
	else
	{
		for(int j = 1 ; j < 205 ; ++j)
		{
			calc[0][j] = 0 ;
			for(int i = 1 ; i < n + 1000 ; ++i)
			{
				calc[i][j] = calc[i-1][j] + 1 ;
				if(calc[i][j] == j)
					calc[i][j] = 0 ;
			}
		}
		solve2(1) ;
		int ans = inf ;
		for(int i = 0 ; i < K ; ++i)
			ans = min(ans , dist[n][i]) ;
		if(ans == inf)
			cout<<"impossible\n" ;
		else
			cout<<ans<<"\n" ;
	}
	return 0 ;
}		
# Verdict Execution time Memory Grader output
1 Correct 17 ms 15380 KB Output is correct
2 Correct 50 ms 29792 KB Output is correct
3 Correct 47 ms 28512 KB Output is correct
4 Correct 50 ms 28664 KB Output is correct
5 Correct 9 ms 14420 KB Output is correct
6 Correct 52 ms 28596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 15372 KB Output is correct
2 Correct 50 ms 29732 KB Output is correct
3 Correct 51 ms 28560 KB Output is correct
4 Correct 54 ms 28572 KB Output is correct
5 Correct 8 ms 14428 KB Output is correct
6 Correct 47 ms 28456 KB Output is correct
7 Correct 48 ms 28516 KB Output is correct
8 Correct 46 ms 28456 KB Output is correct
9 Correct 54 ms 28424 KB Output is correct
10 Correct 47 ms 28728 KB Output is correct
11 Correct 49 ms 28592 KB Output is correct
12 Correct 70 ms 28424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 15372 KB Output is correct
2 Correct 50 ms 29732 KB Output is correct
3 Correct 51 ms 28560 KB Output is correct
4 Correct 54 ms 28572 KB Output is correct
5 Correct 8 ms 14428 KB Output is correct
6 Correct 47 ms 28456 KB Output is correct
7 Correct 48 ms 28516 KB Output is correct
8 Correct 46 ms 28456 KB Output is correct
9 Correct 54 ms 28424 KB Output is correct
10 Correct 47 ms 28728 KB Output is correct
11 Correct 49 ms 28592 KB Output is correct
12 Correct 70 ms 28424 KB Output is correct
13 Correct 21 ms 15372 KB Output is correct
14 Correct 54 ms 29856 KB Output is correct
15 Correct 48 ms 28548 KB Output is correct
16 Correct 52 ms 28584 KB Output is correct
17 Correct 7 ms 14420 KB Output is correct
18 Correct 47 ms 28492 KB Output is correct
19 Correct 47 ms 28452 KB Output is correct
20 Correct 48 ms 28500 KB Output is correct
21 Correct 45 ms 28408 KB Output is correct
22 Correct 57 ms 28752 KB Output is correct
23 Correct 50 ms 28576 KB Output is correct
24 Correct 57 ms 28496 KB Output is correct
25 Correct 1071 ms 88700 KB Output is correct
26 Correct 882 ms 95016 KB Output is correct
27 Correct 856 ms 88500 KB Output is correct
28 Correct 768 ms 93408 KB Output is correct
29 Correct 1047 ms 82276 KB Output is correct
30 Correct 948 ms 85736 KB Output is correct
31 Correct 1141 ms 95584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 15372 KB Output is correct
2 Correct 50 ms 29732 KB Output is correct
3 Correct 51 ms 28560 KB Output is correct
4 Correct 54 ms 28572 KB Output is correct
5 Correct 8 ms 14428 KB Output is correct
6 Correct 47 ms 28456 KB Output is correct
7 Correct 48 ms 28516 KB Output is correct
8 Correct 46 ms 28456 KB Output is correct
9 Correct 54 ms 28424 KB Output is correct
10 Correct 47 ms 28728 KB Output is correct
11 Correct 49 ms 28592 KB Output is correct
12 Correct 70 ms 28424 KB Output is correct
13 Correct 21 ms 15372 KB Output is correct
14 Correct 54 ms 29856 KB Output is correct
15 Correct 48 ms 28548 KB Output is correct
16 Correct 52 ms 28584 KB Output is correct
17 Correct 7 ms 14420 KB Output is correct
18 Correct 47 ms 28492 KB Output is correct
19 Correct 47 ms 28452 KB Output is correct
20 Correct 48 ms 28500 KB Output is correct
21 Correct 45 ms 28408 KB Output is correct
22 Correct 57 ms 28752 KB Output is correct
23 Correct 50 ms 28576 KB Output is correct
24 Correct 57 ms 28496 KB Output is correct
25 Correct 1071 ms 88700 KB Output is correct
26 Correct 882 ms 95016 KB Output is correct
27 Correct 856 ms 88500 KB Output is correct
28 Correct 768 ms 93408 KB Output is correct
29 Correct 1047 ms 82276 KB Output is correct
30 Correct 948 ms 85736 KB Output is correct
31 Correct 1141 ms 95584 KB Output is correct
32 Correct 18 ms 15444 KB Output is correct
33 Correct 52 ms 29808 KB Output is correct
34 Correct 48 ms 28512 KB Output is correct
35 Correct 52 ms 28588 KB Output is correct
36 Correct 7 ms 14420 KB Output is correct
37 Correct 55 ms 28528 KB Output is correct
38 Correct 53 ms 28636 KB Output is correct
39 Correct 47 ms 28492 KB Output is correct
40 Correct 53 ms 28428 KB Output is correct
41 Correct 65 ms 28748 KB Output is correct
42 Correct 48 ms 28552 KB Output is correct
43 Correct 49 ms 28436 KB Output is correct
44 Correct 1067 ms 88644 KB Output is correct
45 Correct 903 ms 95048 KB Output is correct
46 Correct 859 ms 88460 KB Output is correct
47 Correct 785 ms 93272 KB Output is correct
48 Correct 972 ms 82268 KB Output is correct
49 Correct 968 ms 85824 KB Output is correct
50 Correct 1175 ms 95512 KB Output is correct
51 Correct 1049 ms 89604 KB Output is correct
52 Correct 913 ms 96016 KB Output is correct
53 Correct 850 ms 88288 KB Output is correct
54 Correct 750 ms 81948 KB Output is correct
55 Correct 1099 ms 88128 KB Output is correct
56 Correct 1108 ms 82072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 15380 KB Output is correct
2 Correct 50 ms 29792 KB Output is correct
3 Correct 47 ms 28512 KB Output is correct
4 Correct 50 ms 28664 KB Output is correct
5 Correct 9 ms 14420 KB Output is correct
6 Correct 52 ms 28596 KB Output is correct
7 Correct 19 ms 15372 KB Output is correct
8 Correct 50 ms 29732 KB Output is correct
9 Correct 51 ms 28560 KB Output is correct
10 Correct 54 ms 28572 KB Output is correct
11 Correct 8 ms 14428 KB Output is correct
12 Correct 47 ms 28456 KB Output is correct
13 Correct 48 ms 28516 KB Output is correct
14 Correct 46 ms 28456 KB Output is correct
15 Correct 54 ms 28424 KB Output is correct
16 Correct 47 ms 28728 KB Output is correct
17 Correct 49 ms 28592 KB Output is correct
18 Correct 70 ms 28424 KB Output is correct
19 Correct 7 ms 14420 KB Output is correct
20 Correct 9 ms 14420 KB Output is correct
21 Correct 7 ms 15188 KB Output is correct
22 Correct 17 ms 15424 KB Output is correct
23 Correct 51 ms 29780 KB Output is correct
24 Correct 50 ms 28512 KB Output is correct
25 Correct 51 ms 28660 KB Output is correct
26 Correct 7 ms 14420 KB Output is correct
27 Correct 49 ms 28448 KB Output is correct
28 Correct 48 ms 28524 KB Output is correct
29 Correct 60 ms 28460 KB Output is correct
30 Correct 92 ms 28448 KB Output is correct
31 Correct 55 ms 28700 KB Output is correct
32 Correct 51 ms 28492 KB Output is correct
33 Correct 52 ms 28536 KB Output is correct
34 Correct 3611 ms 291356 KB Output is correct
35 Correct 4404 ms 290616 KB Output is correct
36 Correct 4610 ms 290884 KB Output is correct
37 Incorrect 2530 ms 297504 KB Output isn't correct
38 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 17 ms 15380 KB Output is correct
2 Correct 50 ms 29792 KB Output is correct
3 Correct 47 ms 28512 KB Output is correct
4 Correct 50 ms 28664 KB Output is correct
5 Correct 9 ms 14420 KB Output is correct
6 Correct 52 ms 28596 KB Output is correct
7 Correct 19 ms 15372 KB Output is correct
8 Correct 50 ms 29732 KB Output is correct
9 Correct 51 ms 28560 KB Output is correct
10 Correct 54 ms 28572 KB Output is correct
11 Correct 8 ms 14428 KB Output is correct
12 Correct 47 ms 28456 KB Output is correct
13 Correct 48 ms 28516 KB Output is correct
14 Correct 46 ms 28456 KB Output is correct
15 Correct 54 ms 28424 KB Output is correct
16 Correct 47 ms 28728 KB Output is correct
17 Correct 49 ms 28592 KB Output is correct
18 Correct 70 ms 28424 KB Output is correct
19 Correct 21 ms 15372 KB Output is correct
20 Correct 54 ms 29856 KB Output is correct
21 Correct 48 ms 28548 KB Output is correct
22 Correct 52 ms 28584 KB Output is correct
23 Correct 7 ms 14420 KB Output is correct
24 Correct 47 ms 28492 KB Output is correct
25 Correct 47 ms 28452 KB Output is correct
26 Correct 48 ms 28500 KB Output is correct
27 Correct 45 ms 28408 KB Output is correct
28 Correct 57 ms 28752 KB Output is correct
29 Correct 50 ms 28576 KB Output is correct
30 Correct 57 ms 28496 KB Output is correct
31 Correct 1071 ms 88700 KB Output is correct
32 Correct 882 ms 95016 KB Output is correct
33 Correct 856 ms 88500 KB Output is correct
34 Correct 768 ms 93408 KB Output is correct
35 Correct 1047 ms 82276 KB Output is correct
36 Correct 948 ms 85736 KB Output is correct
37 Correct 1141 ms 95584 KB Output is correct
38 Correct 7 ms 14420 KB Output is correct
39 Correct 9 ms 14420 KB Output is correct
40 Correct 7 ms 15188 KB Output is correct
41 Correct 17 ms 15424 KB Output is correct
42 Correct 51 ms 29780 KB Output is correct
43 Correct 50 ms 28512 KB Output is correct
44 Correct 51 ms 28660 KB Output is correct
45 Correct 7 ms 14420 KB Output is correct
46 Correct 49 ms 28448 KB Output is correct
47 Correct 48 ms 28524 KB Output is correct
48 Correct 60 ms 28460 KB Output is correct
49 Correct 92 ms 28448 KB Output is correct
50 Correct 55 ms 28700 KB Output is correct
51 Correct 51 ms 28492 KB Output is correct
52 Correct 52 ms 28536 KB Output is correct
53 Correct 3611 ms 291356 KB Output is correct
54 Correct 4404 ms 290616 KB Output is correct
55 Correct 4610 ms 290884 KB Output is correct
56 Incorrect 2530 ms 297504 KB Output isn't correct
57 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 17 ms 15380 KB Output is correct
2 Correct 50 ms 29792 KB Output is correct
3 Correct 47 ms 28512 KB Output is correct
4 Correct 50 ms 28664 KB Output is correct
5 Correct 9 ms 14420 KB Output is correct
6 Correct 52 ms 28596 KB Output is correct
7 Correct 19 ms 15372 KB Output is correct
8 Correct 50 ms 29732 KB Output is correct
9 Correct 51 ms 28560 KB Output is correct
10 Correct 54 ms 28572 KB Output is correct
11 Correct 8 ms 14428 KB Output is correct
12 Correct 47 ms 28456 KB Output is correct
13 Correct 48 ms 28516 KB Output is correct
14 Correct 46 ms 28456 KB Output is correct
15 Correct 54 ms 28424 KB Output is correct
16 Correct 47 ms 28728 KB Output is correct
17 Correct 49 ms 28592 KB Output is correct
18 Correct 70 ms 28424 KB Output is correct
19 Correct 21 ms 15372 KB Output is correct
20 Correct 54 ms 29856 KB Output is correct
21 Correct 48 ms 28548 KB Output is correct
22 Correct 52 ms 28584 KB Output is correct
23 Correct 7 ms 14420 KB Output is correct
24 Correct 47 ms 28492 KB Output is correct
25 Correct 47 ms 28452 KB Output is correct
26 Correct 48 ms 28500 KB Output is correct
27 Correct 45 ms 28408 KB Output is correct
28 Correct 57 ms 28752 KB Output is correct
29 Correct 50 ms 28576 KB Output is correct
30 Correct 57 ms 28496 KB Output is correct
31 Correct 1071 ms 88700 KB Output is correct
32 Correct 882 ms 95016 KB Output is correct
33 Correct 856 ms 88500 KB Output is correct
34 Correct 768 ms 93408 KB Output is correct
35 Correct 1047 ms 82276 KB Output is correct
36 Correct 948 ms 85736 KB Output is correct
37 Correct 1141 ms 95584 KB Output is correct
38 Correct 18 ms 15444 KB Output is correct
39 Correct 52 ms 29808 KB Output is correct
40 Correct 48 ms 28512 KB Output is correct
41 Correct 52 ms 28588 KB Output is correct
42 Correct 7 ms 14420 KB Output is correct
43 Correct 55 ms 28528 KB Output is correct
44 Correct 53 ms 28636 KB Output is correct
45 Correct 47 ms 28492 KB Output is correct
46 Correct 53 ms 28428 KB Output is correct
47 Correct 65 ms 28748 KB Output is correct
48 Correct 48 ms 28552 KB Output is correct
49 Correct 49 ms 28436 KB Output is correct
50 Correct 1067 ms 88644 KB Output is correct
51 Correct 903 ms 95048 KB Output is correct
52 Correct 859 ms 88460 KB Output is correct
53 Correct 785 ms 93272 KB Output is correct
54 Correct 972 ms 82268 KB Output is correct
55 Correct 968 ms 85824 KB Output is correct
56 Correct 1175 ms 95512 KB Output is correct
57 Correct 1049 ms 89604 KB Output is correct
58 Correct 913 ms 96016 KB Output is correct
59 Correct 850 ms 88288 KB Output is correct
60 Correct 750 ms 81948 KB Output is correct
61 Correct 1099 ms 88128 KB Output is correct
62 Correct 1108 ms 82072 KB Output is correct
63 Correct 7 ms 14420 KB Output is correct
64 Correct 9 ms 14420 KB Output is correct
65 Correct 7 ms 15188 KB Output is correct
66 Correct 17 ms 15424 KB Output is correct
67 Correct 51 ms 29780 KB Output is correct
68 Correct 50 ms 28512 KB Output is correct
69 Correct 51 ms 28660 KB Output is correct
70 Correct 7 ms 14420 KB Output is correct
71 Correct 49 ms 28448 KB Output is correct
72 Correct 48 ms 28524 KB Output is correct
73 Correct 60 ms 28460 KB Output is correct
74 Correct 92 ms 28448 KB Output is correct
75 Correct 55 ms 28700 KB Output is correct
76 Correct 51 ms 28492 KB Output is correct
77 Correct 52 ms 28536 KB Output is correct
78 Correct 3611 ms 291356 KB Output is correct
79 Correct 4404 ms 290616 KB Output is correct
80 Correct 4610 ms 290884 KB Output is correct
81 Incorrect 2530 ms 297504 KB Output isn't correct
82 Halted 0 ms 0 KB -