답안 #817661

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
817661 2023-08-09T14:48:16 Z MohamedAhmed04 From Hacks to Snitches (BOI21_watchmen) C++14
35 / 100
6000 ms 269796 KB
#include <bits/stdc++.h>

using namespace std ;

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

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

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((!x) || (v[x][(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][(cost2 - 1) % cycle_sz[x]] == child2 && v[x][cost2 % cycle_sz[x]] == node)
				continue ;
			if(x > 0 && v[x][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
	{
		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 ;
}		
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 15864 KB Output is correct
2 Correct 91 ms 98384 KB Output is correct
3 Correct 78 ms 90308 KB Output is correct
4 Correct 87 ms 90444 KB Output is correct
5 Correct 8 ms 14548 KB Output is correct
6 Correct 75 ms 90292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 15812 KB Output is correct
2 Correct 79 ms 98444 KB Output is correct
3 Correct 79 ms 90276 KB Output is correct
4 Correct 92 ms 90356 KB Output is correct
5 Correct 7 ms 14548 KB Output is correct
6 Correct 83 ms 90216 KB Output is correct
7 Correct 86 ms 90396 KB Output is correct
8 Correct 79 ms 90284 KB Output is correct
9 Correct 73 ms 90268 KB Output is correct
10 Correct 78 ms 90388 KB Output is correct
11 Correct 75 ms 90392 KB Output is correct
12 Correct 81 ms 90212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 15812 KB Output is correct
2 Correct 79 ms 98444 KB Output is correct
3 Correct 79 ms 90276 KB Output is correct
4 Correct 92 ms 90356 KB Output is correct
5 Correct 7 ms 14548 KB Output is correct
6 Correct 83 ms 90216 KB Output is correct
7 Correct 86 ms 90396 KB Output is correct
8 Correct 79 ms 90284 KB Output is correct
9 Correct 73 ms 90268 KB Output is correct
10 Correct 78 ms 90388 KB Output is correct
11 Correct 75 ms 90392 KB Output is correct
12 Correct 81 ms 90212 KB Output is correct
13 Correct 18 ms 15872 KB Output is correct
14 Correct 117 ms 98472 KB Output is correct
15 Correct 76 ms 90320 KB Output is correct
16 Correct 81 ms 90432 KB Output is correct
17 Correct 7 ms 14548 KB Output is correct
18 Correct 87 ms 90236 KB Output is correct
19 Correct 77 ms 90212 KB Output is correct
20 Correct 80 ms 90276 KB Output is correct
21 Correct 81 ms 90224 KB Output is correct
22 Correct 83 ms 90476 KB Output is correct
23 Correct 77 ms 90340 KB Output is correct
24 Correct 77 ms 90188 KB Output is correct
25 Correct 1222 ms 262492 KB Output is correct
26 Correct 1034 ms 268924 KB Output is correct
27 Correct 959 ms 262264 KB Output is correct
28 Correct 891 ms 267064 KB Output is correct
29 Correct 1119 ms 256056 KB Output is correct
30 Correct 1193 ms 259528 KB Output is correct
31 Correct 1197 ms 269244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 15812 KB Output is correct
2 Correct 79 ms 98444 KB Output is correct
3 Correct 79 ms 90276 KB Output is correct
4 Correct 92 ms 90356 KB Output is correct
5 Correct 7 ms 14548 KB Output is correct
6 Correct 83 ms 90216 KB Output is correct
7 Correct 86 ms 90396 KB Output is correct
8 Correct 79 ms 90284 KB Output is correct
9 Correct 73 ms 90268 KB Output is correct
10 Correct 78 ms 90388 KB Output is correct
11 Correct 75 ms 90392 KB Output is correct
12 Correct 81 ms 90212 KB Output is correct
13 Correct 18 ms 15872 KB Output is correct
14 Correct 117 ms 98472 KB Output is correct
15 Correct 76 ms 90320 KB Output is correct
16 Correct 81 ms 90432 KB Output is correct
17 Correct 7 ms 14548 KB Output is correct
18 Correct 87 ms 90236 KB Output is correct
19 Correct 77 ms 90212 KB Output is correct
20 Correct 80 ms 90276 KB Output is correct
21 Correct 81 ms 90224 KB Output is correct
22 Correct 83 ms 90476 KB Output is correct
23 Correct 77 ms 90340 KB Output is correct
24 Correct 77 ms 90188 KB Output is correct
25 Correct 1222 ms 262492 KB Output is correct
26 Correct 1034 ms 268924 KB Output is correct
27 Correct 959 ms 262264 KB Output is correct
28 Correct 891 ms 267064 KB Output is correct
29 Correct 1119 ms 256056 KB Output is correct
30 Correct 1193 ms 259528 KB Output is correct
31 Correct 1197 ms 269244 KB Output is correct
32 Correct 18 ms 15828 KB Output is correct
33 Correct 82 ms 98348 KB Output is correct
34 Correct 75 ms 90312 KB Output is correct
35 Correct 101 ms 90336 KB Output is correct
36 Correct 7 ms 14548 KB Output is correct
37 Correct 76 ms 90296 KB Output is correct
38 Correct 81 ms 90288 KB Output is correct
39 Correct 75 ms 90260 KB Output is correct
40 Correct 79 ms 90244 KB Output is correct
41 Correct 74 ms 90364 KB Output is correct
42 Correct 81 ms 90372 KB Output is correct
43 Correct 80 ms 90188 KB Output is correct
44 Correct 1373 ms 262352 KB Output is correct
45 Correct 1216 ms 268840 KB Output is correct
46 Correct 1236 ms 262188 KB Output is correct
47 Correct 964 ms 267020 KB Output is correct
48 Correct 1335 ms 256124 KB Output is correct
49 Correct 1316 ms 259524 KB Output is correct
50 Correct 1223 ms 269320 KB Output is correct
51 Correct 1170 ms 263256 KB Output is correct
52 Correct 1014 ms 269796 KB Output is correct
53 Correct 1041 ms 262084 KB Output is correct
54 Correct 818 ms 255788 KB Output is correct
55 Correct 1176 ms 261836 KB Output is correct
56 Correct 1152 ms 255964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 15864 KB Output is correct
2 Correct 91 ms 98384 KB Output is correct
3 Correct 78 ms 90308 KB Output is correct
4 Correct 87 ms 90444 KB Output is correct
5 Correct 8 ms 14548 KB Output is correct
6 Correct 75 ms 90292 KB Output is correct
7 Correct 18 ms 15812 KB Output is correct
8 Correct 79 ms 98444 KB Output is correct
9 Correct 79 ms 90276 KB Output is correct
10 Correct 92 ms 90356 KB Output is correct
11 Correct 7 ms 14548 KB Output is correct
12 Correct 83 ms 90216 KB Output is correct
13 Correct 86 ms 90396 KB Output is correct
14 Correct 79 ms 90284 KB Output is correct
15 Correct 73 ms 90268 KB Output is correct
16 Correct 78 ms 90388 KB Output is correct
17 Correct 75 ms 90392 KB Output is correct
18 Correct 81 ms 90212 KB Output is correct
19 Correct 8 ms 14436 KB Output is correct
20 Correct 7 ms 14420 KB Output is correct
21 Correct 7 ms 14420 KB Output is correct
22 Correct 18 ms 15828 KB Output is correct
23 Correct 84 ms 98552 KB Output is correct
24 Correct 86 ms 90348 KB Output is correct
25 Correct 91 ms 90572 KB Output is correct
26 Correct 8 ms 14548 KB Output is correct
27 Correct 85 ms 90532 KB Output is correct
28 Correct 78 ms 90316 KB Output is correct
29 Correct 82 ms 90416 KB Output is correct
30 Correct 74 ms 90308 KB Output is correct
31 Correct 82 ms 90560 KB Output is correct
32 Correct 81 ms 90456 KB Output is correct
33 Correct 91 ms 90396 KB Output is correct
34 Execution timed out 6040 ms 264352 KB Time limit exceeded
35 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 15864 KB Output is correct
2 Correct 91 ms 98384 KB Output is correct
3 Correct 78 ms 90308 KB Output is correct
4 Correct 87 ms 90444 KB Output is correct
5 Correct 8 ms 14548 KB Output is correct
6 Correct 75 ms 90292 KB Output is correct
7 Correct 18 ms 15812 KB Output is correct
8 Correct 79 ms 98444 KB Output is correct
9 Correct 79 ms 90276 KB Output is correct
10 Correct 92 ms 90356 KB Output is correct
11 Correct 7 ms 14548 KB Output is correct
12 Correct 83 ms 90216 KB Output is correct
13 Correct 86 ms 90396 KB Output is correct
14 Correct 79 ms 90284 KB Output is correct
15 Correct 73 ms 90268 KB Output is correct
16 Correct 78 ms 90388 KB Output is correct
17 Correct 75 ms 90392 KB Output is correct
18 Correct 81 ms 90212 KB Output is correct
19 Correct 18 ms 15872 KB Output is correct
20 Correct 117 ms 98472 KB Output is correct
21 Correct 76 ms 90320 KB Output is correct
22 Correct 81 ms 90432 KB Output is correct
23 Correct 7 ms 14548 KB Output is correct
24 Correct 87 ms 90236 KB Output is correct
25 Correct 77 ms 90212 KB Output is correct
26 Correct 80 ms 90276 KB Output is correct
27 Correct 81 ms 90224 KB Output is correct
28 Correct 83 ms 90476 KB Output is correct
29 Correct 77 ms 90340 KB Output is correct
30 Correct 77 ms 90188 KB Output is correct
31 Correct 1222 ms 262492 KB Output is correct
32 Correct 1034 ms 268924 KB Output is correct
33 Correct 959 ms 262264 KB Output is correct
34 Correct 891 ms 267064 KB Output is correct
35 Correct 1119 ms 256056 KB Output is correct
36 Correct 1193 ms 259528 KB Output is correct
37 Correct 1197 ms 269244 KB Output is correct
38 Correct 8 ms 14436 KB Output is correct
39 Correct 7 ms 14420 KB Output is correct
40 Correct 7 ms 14420 KB Output is correct
41 Correct 18 ms 15828 KB Output is correct
42 Correct 84 ms 98552 KB Output is correct
43 Correct 86 ms 90348 KB Output is correct
44 Correct 91 ms 90572 KB Output is correct
45 Correct 8 ms 14548 KB Output is correct
46 Correct 85 ms 90532 KB Output is correct
47 Correct 78 ms 90316 KB Output is correct
48 Correct 82 ms 90416 KB Output is correct
49 Correct 74 ms 90308 KB Output is correct
50 Correct 82 ms 90560 KB Output is correct
51 Correct 81 ms 90456 KB Output is correct
52 Correct 91 ms 90396 KB Output is correct
53 Execution timed out 6040 ms 264352 KB Time limit exceeded
54 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 15864 KB Output is correct
2 Correct 91 ms 98384 KB Output is correct
3 Correct 78 ms 90308 KB Output is correct
4 Correct 87 ms 90444 KB Output is correct
5 Correct 8 ms 14548 KB Output is correct
6 Correct 75 ms 90292 KB Output is correct
7 Correct 18 ms 15812 KB Output is correct
8 Correct 79 ms 98444 KB Output is correct
9 Correct 79 ms 90276 KB Output is correct
10 Correct 92 ms 90356 KB Output is correct
11 Correct 7 ms 14548 KB Output is correct
12 Correct 83 ms 90216 KB Output is correct
13 Correct 86 ms 90396 KB Output is correct
14 Correct 79 ms 90284 KB Output is correct
15 Correct 73 ms 90268 KB Output is correct
16 Correct 78 ms 90388 KB Output is correct
17 Correct 75 ms 90392 KB Output is correct
18 Correct 81 ms 90212 KB Output is correct
19 Correct 18 ms 15872 KB Output is correct
20 Correct 117 ms 98472 KB Output is correct
21 Correct 76 ms 90320 KB Output is correct
22 Correct 81 ms 90432 KB Output is correct
23 Correct 7 ms 14548 KB Output is correct
24 Correct 87 ms 90236 KB Output is correct
25 Correct 77 ms 90212 KB Output is correct
26 Correct 80 ms 90276 KB Output is correct
27 Correct 81 ms 90224 KB Output is correct
28 Correct 83 ms 90476 KB Output is correct
29 Correct 77 ms 90340 KB Output is correct
30 Correct 77 ms 90188 KB Output is correct
31 Correct 1222 ms 262492 KB Output is correct
32 Correct 1034 ms 268924 KB Output is correct
33 Correct 959 ms 262264 KB Output is correct
34 Correct 891 ms 267064 KB Output is correct
35 Correct 1119 ms 256056 KB Output is correct
36 Correct 1193 ms 259528 KB Output is correct
37 Correct 1197 ms 269244 KB Output is correct
38 Correct 18 ms 15828 KB Output is correct
39 Correct 82 ms 98348 KB Output is correct
40 Correct 75 ms 90312 KB Output is correct
41 Correct 101 ms 90336 KB Output is correct
42 Correct 7 ms 14548 KB Output is correct
43 Correct 76 ms 90296 KB Output is correct
44 Correct 81 ms 90288 KB Output is correct
45 Correct 75 ms 90260 KB Output is correct
46 Correct 79 ms 90244 KB Output is correct
47 Correct 74 ms 90364 KB Output is correct
48 Correct 81 ms 90372 KB Output is correct
49 Correct 80 ms 90188 KB Output is correct
50 Correct 1373 ms 262352 KB Output is correct
51 Correct 1216 ms 268840 KB Output is correct
52 Correct 1236 ms 262188 KB Output is correct
53 Correct 964 ms 267020 KB Output is correct
54 Correct 1335 ms 256124 KB Output is correct
55 Correct 1316 ms 259524 KB Output is correct
56 Correct 1223 ms 269320 KB Output is correct
57 Correct 1170 ms 263256 KB Output is correct
58 Correct 1014 ms 269796 KB Output is correct
59 Correct 1041 ms 262084 KB Output is correct
60 Correct 818 ms 255788 KB Output is correct
61 Correct 1176 ms 261836 KB Output is correct
62 Correct 1152 ms 255964 KB Output is correct
63 Correct 8 ms 14436 KB Output is correct
64 Correct 7 ms 14420 KB Output is correct
65 Correct 7 ms 14420 KB Output is correct
66 Correct 18 ms 15828 KB Output is correct
67 Correct 84 ms 98552 KB Output is correct
68 Correct 86 ms 90348 KB Output is correct
69 Correct 91 ms 90572 KB Output is correct
70 Correct 8 ms 14548 KB Output is correct
71 Correct 85 ms 90532 KB Output is correct
72 Correct 78 ms 90316 KB Output is correct
73 Correct 82 ms 90416 KB Output is correct
74 Correct 74 ms 90308 KB Output is correct
75 Correct 82 ms 90560 KB Output is correct
76 Correct 81 ms 90456 KB Output is correct
77 Correct 91 ms 90396 KB Output is correct
78 Execution timed out 6040 ms 264352 KB Time limit exceeded
79 Halted 0 ms 0 KB -