답안 #989842

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
989842 2024-05-28T20:51:29 Z Cyber_Wolf Board Game (JOI24_boardgame) C++17
68 / 100
4000 ms 12484 KB
#include <bits/stdc++.h>

using namespace std;

#define lg long long
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

const int N = 5e4+5, M = 305;

vector<int> adj[N];
int loc[N], stop[N], stops = 0;
long long dist[N][M], dist2[N][M], sto[M], doub[N], sing[N], ans2[N], dist3[N][2], gh[N], inc[N];
bitset<M> vis[N], visd;

int main()
{
	fastio;
	int n, m, k;
	cin >> n >> m >> k;
	for(int i = 0; i < m; i++)
	{
		int u, v;
		cin >> u >> v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	string s;
	cin >> s;
	vector<int> st;
	for(int i = 1; i <= n; i++)
	{
		stop[i] = (s[i-1] == '1');
		stops += stop[i];
		if(stop[i])	st.push_back(i);
	}
	for(int i = 0; i < k; i++)
	{
		cin >> loc[i];
	}
	if(!stops)
	{
		memset(ans2, 0x3f, sizeof(ans2));
		queue<int>q;
		visd = 0;
		visd[loc[0]] = 1;
		ans2[loc[0]] = 0;
		q.push(loc[0]);
		while(q.size())
		{
			int u = q.front();
			q.pop();
			if(stop[u] && u != loc[0])	continue;
			for(auto it : adj[u])
			{
				if(ans2[it] > ans2[u]+1)
				{
					ans2[it] = ans2[u]+1;
					q.push(it);
				}
			}
		}
		for(int i = 1; i <= n; i++)	cout << ans2[i] << '\n';
		return 0;
	}
	// if(n/k < M-5)
	// {
		// memset(dist, 0x3f, sizeof(dist));
		// memset(dist2, 0x3f, sizeof(dist2));
		// stops = min(stops, M-5);
		// queue<int> q[stops+3];
		// q[0].push(loc[0]);
		// dist[loc[0]][0] = 0;
		// vis[loc[0]][0] = true;
		// for(int i = 0; i <= stops+2; i++)
		// {
			// int its = 0;
			// while(q[i].size())
			// {
				// int u = q[i].front();
				// q[i].pop();
				// vis[u][i] = false;
				// int newOg = i;
				// if(stop[u] && (its || i))
				// {
					// newOg++;
				// }
				// if(newOg > stops+2)	continue;
				// for(auto it : adj[u])
				// {
					// if(dist[it][newOg] > dist[u][i]+1)
					// {
						// if(!vis[it][newOg])	q[newOg].push(it);
						// vis[it][newOg] = true;
						// dist[it][newOg] = dist[u][i]+1;	
					// }
				// }
				// its++;
			// }
		// }
		// for(int i = 1; i <= n; i++)	vis[i] = 0, dist2[i][0] = 0;
		// for(auto it : st)	q[0].push(it), vis[it][0] = true;
		// for(int i = 0; i <= stops+2; i++)
		// {
			// while(q[i].size())
			// {
				// int u = q[i].front();
				// q[i].pop();
				// vis[u][i] = false;
				// int newOg = i;
				// if(stop[u])
				// {
					// newOg++;
				// }
				// if(newOg > stops+2)	continue;
				// for(auto it : adj[u])
				// {
					// if(dist2[it][newOg] > dist2[u][i]+1)
					// {
						// if(!vis[it][newOg])	q[newOg].push(it);
						// vis[it][newOg] = true;
						// dist2[it][newOg] = dist2[u][i]+1;	
					// }
				// }
			// }
		// }
		// for(int i = 1; i < k; i++)
		// {
			// for(int j = 0; j <= stops+2; j++)
			// {
				// if(dist2[loc[i]][j] > 1e18)
				// {
					// sto[j] = -1;
					// break;
				// }
				// sto[j] += dist2[loc[i]][j];
			// }
		// }
		// for(int i = 1; i <= n; i++)
		// {
			// long long ans = dist[i][0];
			// for(int j = 1; j <= stops+2; j++)
			// {
				// if(sto[j] == -1)	continue;
				// ans = min(ans, dist[i][j]+sto[j]);
			// }
			// cout << ans << '\n';
		// }
		// return 0;
	// }
	memset(sing, 0x3f, sizeof(sing));
	memset(doub, 0x3f, sizeof(doub));
	queue<int> q;
	for(auto it : st)
	{
		q.push(it);
		sing[it] = 0;
	}
	while(q.size())
	{
		int u = q.front();
		q.pop();
		for(auto it : adj[u])
		{
			if(sing[it] > sing[u]+1)
			{
				sing[it] = sing[u]+1;
				q.push(it);
			}
		}
	}
	for(int i = 1; i <= n; i++)
	{
		sing[i] = 2*(!sing[i])+sing[i];
	}
	for(int i = 1; i < k; i++)	gh[0] += sing[loc[i]];
	deque<int> dq;
	for(auto it : st)
	{
		for(auto it2 : adj[it])
		{
			if(stop[it2])
			{
				dq.push_back(it);
				doub[it] = 0;
				break;
			}
		}
	}
	while(dq.size())
	{
		int u = dq.front();
		dq.pop_front();
		for(auto it : adj[u])
		{
			if(doub[it] > doub[u]+(!stop[u]))
			{
				doub[it] = doub[u]+(!stop[u]);
				if(stop[u])	dq.push_front(it);
				else	dq.push_back(it);	
			}
		}
	}
	bool bad = false;
	for(int i = 1; i < k; i++)
	{
		if(doub[loc[i]] >= 1e18)
		{
			bad = true;
			break;
		}
		inc[i] = doub[loc[i]]-sing[loc[i]];
	}
	if(!bad)
	{
		sort(inc+1, inc+k);
		for(int i = 1; i < k; i++)
		{
			gh[i] = gh[i-1]+inc[i];
		}	
	}
	memset(ans2, 0x3f, sizeof(ans2));
	visd = 0;
	visd[loc[0]] = 1;
	ans2[loc[0]] = 0;
	q.push(loc[0]);
	while(q.size())
	{
		int u = q.front();
		q.pop();
		if(stop[u] && u != loc[0])	continue;
		for(auto it : adj[u])
		{
			if(ans2[it] > ans2[u]+1)
			{
				ans2[it] = ans2[u]+1;
				q.push(it);
			}
		}
	}
	for(int its = 0; its < k; its++)
	{
		if(bad && its)	break;
		memset(dist3, 0x3f, sizeof(dist3));
		dist3[loc[0]][0] = 0;
		vis[loc[0]][0] = true;
		priority_queue<array<lg, 3>> pq;
		pq.push({0, loc[0], 0});
		while(pq.size())
		{
			int u = pq.top()[1];
			int ty = pq.top()[2];
			vis[u][ty] = 0;
			int stopp = (u != loc[0] && stop[u]);
			int newty = (ty||stopp);
			pq.pop();
			for(auto it : adj[u])
			{
				if(dist3[it][newty] > dist3[u][ty]+(stopp ? 2*(k-1-its)+its+1 : 1))
				{
					dist3[it][newty] = dist3[u][ty]+(stopp ? 2*(k-1-its)+its+1 : 1);
					if(!vis[it][newty])
					{
						pq.push({-dist3[it][newty], it, newty});
					}
					vis[it][newty] = true;
				}
			}
		}
		for(int i = 1; i <= n; i++)
		{
			ans2[i] = min(ans2[i], dist3[i][1]+gh[its]-2*(k-1-its));
		}
	}
	for(int i = 1; i <= n; i++)	cout << ans2[i] << '\n';
	
	
	

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5212 KB Output is correct
2 Correct 10 ms 6236 KB Output is correct
3 Correct 17 ms 7004 KB Output is correct
4 Correct 14 ms 7064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7512 KB Output is correct
2 Correct 19 ms 9600 KB Output is correct
3 Correct 22 ms 11348 KB Output is correct
4 Correct 23 ms 11356 KB Output is correct
5 Correct 34 ms 11272 KB Output is correct
6 Correct 29 ms 11356 KB Output is correct
7 Correct 22 ms 11000 KB Output is correct
8 Correct 22 ms 11052 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7768 KB Output is correct
2 Correct 19 ms 10644 KB Output is correct
3 Correct 29 ms 11348 KB Output is correct
4 Correct 21 ms 11356 KB Output is correct
5 Correct 22 ms 11616 KB Output is correct
6 Correct 30 ms 11344 KB Output is correct
7 Correct 30 ms 11236 KB Output is correct
8 Correct 23 ms 11604 KB Output is correct
9 Correct 13 ms 8796 KB Output is correct
10 Correct 13 ms 10588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7260 KB Output is correct
2 Correct 4 ms 9564 KB Output is correct
3 Correct 2 ms 7380 KB Output is correct
4 Correct 12 ms 9588 KB Output is correct
5 Correct 4 ms 9304 KB Output is correct
6 Correct 2 ms 7260 KB Output is correct
7 Correct 752 ms 9504 KB Output is correct
8 Correct 822 ms 9588 KB Output is correct
9 Correct 3 ms 9308 KB Output is correct
10 Correct 2 ms 9308 KB Output is correct
11 Correct 3 ms 9308 KB Output is correct
12 Correct 3 ms 9308 KB Output is correct
13 Correct 4 ms 9564 KB Output is correct
14 Correct 2 ms 9564 KB Output is correct
15 Correct 15 ms 9564 KB Output is correct
16 Correct 28 ms 9564 KB Output is correct
17 Correct 65 ms 9564 KB Output is correct
18 Correct 3 ms 9564 KB Output is correct
19 Correct 4 ms 9564 KB Output is correct
20 Correct 23 ms 9568 KB Output is correct
21 Correct 511 ms 9584 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 11348 KB Output is correct
2 Correct 23 ms 11356 KB Output is correct
3 Correct 19 ms 12060 KB Output is correct
4 Correct 31 ms 12124 KB Output is correct
5 Correct 44 ms 12072 KB Output is correct
6 Correct 17 ms 11612 KB Output is correct
7 Correct 18 ms 11384 KB Output is correct
8 Correct 13 ms 10680 KB Output is correct
9 Correct 14 ms 10584 KB Output is correct
10 Correct 23 ms 11448 KB Output is correct
11 Correct 31 ms 11608 KB Output is correct
12 Correct 29 ms 11868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 11348 KB Output is correct
2 Correct 23 ms 11356 KB Output is correct
3 Correct 19 ms 12060 KB Output is correct
4 Correct 31 ms 12124 KB Output is correct
5 Correct 44 ms 12072 KB Output is correct
6 Correct 17 ms 11612 KB Output is correct
7 Correct 18 ms 11384 KB Output is correct
8 Correct 13 ms 10680 KB Output is correct
9 Correct 14 ms 10584 KB Output is correct
10 Correct 23 ms 11448 KB Output is correct
11 Correct 31 ms 11608 KB Output is correct
12 Correct 29 ms 11868 KB Output is correct
13 Correct 29 ms 11860 KB Output is correct
14 Correct 120 ms 12024 KB Output is correct
15 Correct 523 ms 11860 KB Output is correct
16 Correct 281 ms 12088 KB Output is correct
17 Correct 215 ms 12016 KB Output is correct
18 Correct 690 ms 12116 KB Output is correct
19 Correct 879 ms 11968 KB Output is correct
20 Correct 203 ms 12484 KB Output is correct
21 Correct 17 ms 10584 KB Output is correct
22 Correct 133 ms 10844 KB Output is correct
23 Correct 267 ms 10844 KB Output is correct
24 Correct 34 ms 11600 KB Output is correct
25 Correct 232 ms 11980 KB Output is correct
26 Correct 452 ms 12008 KB Output is correct
27 Correct 29 ms 10844 KB Output is correct
28 Correct 219 ms 10868 KB Output is correct
29 Correct 333 ms 10872 KB Output is correct
30 Correct 36 ms 11868 KB Output is correct
31 Correct 330 ms 11860 KB Output is correct
32 Correct 609 ms 11860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7260 KB Output is correct
2 Correct 4 ms 9564 KB Output is correct
3 Correct 2 ms 7380 KB Output is correct
4 Correct 12 ms 9588 KB Output is correct
5 Correct 4 ms 9304 KB Output is correct
6 Correct 2 ms 7260 KB Output is correct
7 Correct 752 ms 9504 KB Output is correct
8 Correct 822 ms 9588 KB Output is correct
9 Correct 3 ms 9308 KB Output is correct
10 Correct 2 ms 9308 KB Output is correct
11 Correct 3 ms 9308 KB Output is correct
12 Correct 3 ms 9308 KB Output is correct
13 Correct 4 ms 9564 KB Output is correct
14 Correct 2 ms 9564 KB Output is correct
15 Correct 15 ms 9564 KB Output is correct
16 Correct 28 ms 9564 KB Output is correct
17 Correct 65 ms 9564 KB Output is correct
18 Correct 3 ms 9564 KB Output is correct
19 Correct 4 ms 9564 KB Output is correct
20 Correct 23 ms 9568 KB Output is correct
21 Correct 511 ms 9584 KB Output is correct
22 Correct 37 ms 10840 KB Output is correct
23 Correct 1991 ms 10928 KB Output is correct
24 Correct 21 ms 11096 KB Output is correct
25 Correct 431 ms 11088 KB Output is correct
26 Correct 734 ms 11088 KB Output is correct
27 Execution timed out 4046 ms 11120 KB Time limit exceeded
28 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5212 KB Output is correct
2 Correct 10 ms 6236 KB Output is correct
3 Correct 17 ms 7004 KB Output is correct
4 Correct 14 ms 7064 KB Output is correct
5 Correct 3 ms 7512 KB Output is correct
6 Correct 19 ms 9600 KB Output is correct
7 Correct 22 ms 11348 KB Output is correct
8 Correct 23 ms 11356 KB Output is correct
9 Correct 34 ms 11272 KB Output is correct
10 Correct 29 ms 11356 KB Output is correct
11 Correct 22 ms 11000 KB Output is correct
12 Correct 22 ms 11052 KB Output is correct
13 Correct 3 ms 7768 KB Output is correct
14 Correct 19 ms 10644 KB Output is correct
15 Correct 29 ms 11348 KB Output is correct
16 Correct 21 ms 11356 KB Output is correct
17 Correct 22 ms 11616 KB Output is correct
18 Correct 30 ms 11344 KB Output is correct
19 Correct 30 ms 11236 KB Output is correct
20 Correct 23 ms 11604 KB Output is correct
21 Correct 13 ms 8796 KB Output is correct
22 Correct 13 ms 10588 KB Output is correct
23 Correct 3 ms 7260 KB Output is correct
24 Correct 4 ms 9564 KB Output is correct
25 Correct 2 ms 7380 KB Output is correct
26 Correct 12 ms 9588 KB Output is correct
27 Correct 4 ms 9304 KB Output is correct
28 Correct 2 ms 7260 KB Output is correct
29 Correct 752 ms 9504 KB Output is correct
30 Correct 822 ms 9588 KB Output is correct
31 Correct 3 ms 9308 KB Output is correct
32 Correct 2 ms 9308 KB Output is correct
33 Correct 3 ms 9308 KB Output is correct
34 Correct 3 ms 9308 KB Output is correct
35 Correct 4 ms 9564 KB Output is correct
36 Correct 2 ms 9564 KB Output is correct
37 Correct 15 ms 9564 KB Output is correct
38 Correct 28 ms 9564 KB Output is correct
39 Correct 65 ms 9564 KB Output is correct
40 Correct 3 ms 9564 KB Output is correct
41 Correct 4 ms 9564 KB Output is correct
42 Correct 23 ms 9568 KB Output is correct
43 Correct 511 ms 9584 KB Output is correct
44 Correct 36 ms 11348 KB Output is correct
45 Correct 23 ms 11356 KB Output is correct
46 Correct 19 ms 12060 KB Output is correct
47 Correct 31 ms 12124 KB Output is correct
48 Correct 44 ms 12072 KB Output is correct
49 Correct 17 ms 11612 KB Output is correct
50 Correct 18 ms 11384 KB Output is correct
51 Correct 13 ms 10680 KB Output is correct
52 Correct 14 ms 10584 KB Output is correct
53 Correct 23 ms 11448 KB Output is correct
54 Correct 31 ms 11608 KB Output is correct
55 Correct 29 ms 11868 KB Output is correct
56 Correct 29 ms 11860 KB Output is correct
57 Correct 120 ms 12024 KB Output is correct
58 Correct 523 ms 11860 KB Output is correct
59 Correct 281 ms 12088 KB Output is correct
60 Correct 215 ms 12016 KB Output is correct
61 Correct 690 ms 12116 KB Output is correct
62 Correct 879 ms 11968 KB Output is correct
63 Correct 203 ms 12484 KB Output is correct
64 Correct 17 ms 10584 KB Output is correct
65 Correct 133 ms 10844 KB Output is correct
66 Correct 267 ms 10844 KB Output is correct
67 Correct 34 ms 11600 KB Output is correct
68 Correct 232 ms 11980 KB Output is correct
69 Correct 452 ms 12008 KB Output is correct
70 Correct 29 ms 10844 KB Output is correct
71 Correct 219 ms 10868 KB Output is correct
72 Correct 333 ms 10872 KB Output is correct
73 Correct 36 ms 11868 KB Output is correct
74 Correct 330 ms 11860 KB Output is correct
75 Correct 609 ms 11860 KB Output is correct
76 Correct 37 ms 10840 KB Output is correct
77 Correct 1991 ms 10928 KB Output is correct
78 Correct 21 ms 11096 KB Output is correct
79 Correct 431 ms 11088 KB Output is correct
80 Correct 734 ms 11088 KB Output is correct
81 Execution timed out 4046 ms 11120 KB Time limit exceeded
82 Halted 0 ms 0 KB -