답안 #1036556

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1036556 2024-07-27T13:51:50 Z Unforgettablepl Board Game (JOI24_boardgame) C++17
17 / 100
31 ms 6228 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long

const int INF = 1e13;

int32_t main(){
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	int n,m,k;
	cin >> n >> m >> k;
	vector<vector<int>> adj(n+1);
	for(int i=1;i<=m;i++){
		int u,v;cin>>u>>v;
		adj[u].emplace_back(v);
		adj[v].emplace_back(u);
	}
	vector<bool> isStop(n+1);
	bool works = false;
	for(int i=1;i<=n;i++){
		char a;cin>>a;
		if(a=='1'){works=isStop[i]=true;}
	}
	vector<int> starts(k+1);
	for(int i=1;i<=k;i++)cin>>starts[i];
	vector<int> minmoves(n+1,INF);
	minmoves[0]=0;
	if(works){
		// calculate minmoves
		vector<int> nearest_single(n+1,-1);
		priority_queue<pair<int,int>> pq;
		for(int i=1;i<=n;i++)if(isStop[i])for(int&x:adj[i])pq.emplace(-1,x);
		vector<bool> visited(n+1);
		while(!pq.empty()){
			auto [dist,idx] = pq.top();pq.pop();dist=-dist;
			if(visited[idx])continue;
			visited[idx]=true;
			nearest_single[idx]=dist;
			for(int&i:adj[idx])if(!visited[i])pq.emplace(-dist-1,i);
		}
		vector<int> nearest_double(n+1,-1);
		for(int i=1;i<=n;i++)if(isStop[i]){
			for(int&x:adj[i]){
				if(isStop[x]){
					for(int&y:adj[i])pq.emplace(-1,y);
					break;
				}
			}
		}
		visited = vector<bool>(n+1);
		while(!pq.empty()){
			auto [dist,idx] = pq.top();pq.pop();dist=-dist;
			if(visited[idx])continue;
			visited[idx]=true;
			nearest_double[idx]=dist;
			for(int&i:adj[idx])if(!visited[i])pq.emplace(-dist-1,i);
		}
		vector<int> slopeedit(n+1);
		int base = 0;
		auto process = [&](int x){
			base+=nearest_single[x];
			slopeedit[1]+=2;
			if(nearest_double[1]!=-1)slopeedit[nearest_double[x]-nearest_single[x]+1]--;
		};
		for(int i=2;i<=k;i++){
			process(starts[i]);
		}
		int currslope = 0;
		for(int i=1;i<=n;i++){
			base+=currslope;
			minmoves[i]=base;
			currslope+=slopeedit[i];
		}
		for(int i=n;i;i--)minmoves[i]-=minmoves[i-1];
	}
	vector<int> currbest(n+1,INF);
	vector<bool> visited(n+1);
	priority_queue<tuple<int,int,int>> pq;
	pq.emplace(0,0,starts[1]);
	while(!pq.empty()){
		auto [stops,dist,idx] = pq.top();pq.pop();dist=-dist;stops=-stops;
		if(visited[idx])continue;
		if(isStop[idx])currbest[idx] = dist-minmoves[stops];
		else currbest[idx] = dist;
		visited[idx] = true;
		for(int&i:adj[idx]){
			if(isStop[i]){
				if(dist+1+minmoves[stops+1]<currbest[i]){
					currbest[i] = dist+1+minmoves[stops+1];
					pq.emplace(-stops-1,-dist-1-minmoves[stops+1],i);
					visited[i]=false;
				}
			} else {
				if(dist+1<currbest[i]){
					currbest[i] = dist+1;
					pq.emplace(-stops,-dist-1,i);
					visited[i]=false;
				}
			}
		}
	}
	for(int i=1;i<=n;i++)cout<<currbest[i]<<'\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 11 ms 3420 KB Output is correct
3 Correct 20 ms 5724 KB Output is correct
4 Correct 15 ms 4952 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 600 KB Output is correct
2 Correct 17 ms 3896 KB Output is correct
3 Correct 18 ms 5464 KB Output is correct
4 Correct 19 ms 5468 KB Output is correct
5 Correct 28 ms 6228 KB Output is correct
6 Correct 25 ms 5652 KB Output is correct
7 Correct 19 ms 4852 KB Output is correct
8 Correct 23 ms 5352 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 604 KB Output is correct
2 Correct 17 ms 3856 KB Output is correct
3 Correct 26 ms 5464 KB Output is correct
4 Correct 20 ms 5460 KB Output is correct
5 Correct 22 ms 5980 KB Output is correct
6 Correct 24 ms 5468 KB Output is correct
7 Correct 28 ms 5708 KB Output is correct
8 Correct 22 ms 5980 KB Output is correct
9 Correct 17 ms 3288 KB Output is correct
10 Correct 18 ms 3284 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 604 KB Output is correct
2 Incorrect 2 ms 604 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 5720 KB Output is correct
2 Incorrect 31 ms 6096 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 5720 KB Output is correct
2 Incorrect 31 ms 6096 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 604 KB Output is correct
2 Incorrect 2 ms 604 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 11 ms 3420 KB Output is correct
3 Correct 20 ms 5724 KB Output is correct
4 Correct 15 ms 4952 KB Output is correct
5 Correct 2 ms 600 KB Output is correct
6 Correct 17 ms 3896 KB Output is correct
7 Correct 18 ms 5464 KB Output is correct
8 Correct 19 ms 5468 KB Output is correct
9 Correct 28 ms 6228 KB Output is correct
10 Correct 25 ms 5652 KB Output is correct
11 Correct 19 ms 4852 KB Output is correct
12 Correct 23 ms 5352 KB Output is correct
13 Correct 2 ms 604 KB Output is correct
14 Correct 17 ms 3856 KB Output is correct
15 Correct 26 ms 5464 KB Output is correct
16 Correct 20 ms 5460 KB Output is correct
17 Correct 22 ms 5980 KB Output is correct
18 Correct 24 ms 5468 KB Output is correct
19 Correct 28 ms 5708 KB Output is correct
20 Correct 22 ms 5980 KB Output is correct
21 Correct 17 ms 3288 KB Output is correct
22 Correct 18 ms 3284 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Incorrect 2 ms 604 KB Output isn't correct
25 Halted 0 ms 0 KB -