跳到正文

题解

题解:P14181 「FAOI-R8」Hotel California

sbno333约 4 分钟
原文
文章信息与版本

文章信息

发布
2025年10月7日
收录
2025年12月12日
更新
2026年11月14日
篇幅
约 4 分钟
题目
P14181

版本

  1. v1当前

考虑最短路。

如果下一次对 xx 取模,在这之前我们只关心当前位置对 xx 取模的值,而不关心对 yy 取模的值。

考虑我们对于每个 xix_i 分别建立 xix_i 节点,表示当前能否到达位置编号对 xix_i 取模后为 jj 的点,然后在建立正常的 mm 个点。

然后每次时间滚动,把当且所有能到达的状态拿出来跑 bfs 更新可达性即可,mm 个点啥时候可达的再特殊关照一下。

复杂度 O(n2m)O(n^2m)。

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m;
int c[29];
vector<bool> s[29];
int xx[29];
int inn;
struct st{
	int t,x,d;
}o[29],oo[29];
int nn;
int an[100009];
#define pii pair<int,int>
#define piii pair<pii,int>
piii q[2000009];
int h=1,t=0;
signed main(){
	std::ios::sync_with_stdio(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>c[i];
	}
	for(int i=0;i<=m;i++){
		s[0].push_back(0);
	}
	s[0][0]=1;
	xx[0]=m+1;
	for(int i=1;i<=n;i++){
		cin>>o[i].t>>o[i].x>>o[i].d;
		if(o[i].t==2){
			xx[++inn]=o[i].x;
			for(int j=0;j<xx[inn];j++){
				s[inn].push_back(0);
			}
			s[inn][0]=1;
		}
	}
	for(int i=1;i<=m;i++){
		an[i]=1e16;
	}
	int z;
	z=1;
	while(z<=n){
		nn=0;
		for(int i=1;i<=n;i++){
			if(o[i].d==z)
				oo[++nn]=o[i];
		}
		h=1,t=0;
		for(int i=0;i<=inn;i++){
			for(int j=0;j<xx[i];j++){
				if(s[i][j])
				q[++t]={{i,j},0};
			}
		}
		while(h<=t){
			int tm;
			tm=q[h].second;
			int x,y;
			x=q[h].first.first;
			y=q[h].first.second;
			++h;
			if(x==0){
				for(int i=1;i<=inn;i++){
					if(!s[i][y%xx[i]]){
						s[i][y%xx[i]]=1;
						q[--h]={{i,y%xx[i]},tm};
					}
				}
				if(tm==c[z]){
					continue;
				}
				for(int i=1;i<=nn;i++){
					if(oo[i].t==1){
						if(y+oo[i].x<=m&&!s[0][y+oo[i].x]){
							s[0][y+oo[i].x]=1;
							q[++t]={{0,y+oo[i].x},tm+1};
						}
					}else{
						int z;
						z=2*y%oo[i].x;
						if(z<=m&&!s[0][z]){
							s[0][z]=1;
							q[++t]={{0,z},tm+1};
						}
					}
				}
				continue;
			}
			if(tm==c[z]){
				continue;
			}
			for(int i=1;i<=nn;i++){
				if(oo[i].t==1){
					if(!s[x][(y+oo[i].x)%xx[x]]){
						s[x][(y+oo[i].x)%xx[x]]=1;
						q[++t]={{x,(y+oo[i].x)%xx[x]},tm+1};
					}
				}else if(oo[i].x==xx[x]){
					int z;
					z=2*y%oo[i].x;
					if(z<=m&&!s[0][z]){
						s[0][z]=1;
						q[++t]={{0,z},tm+1};
					}
				}
			}
		}
		for(int i=1;i<=m;i++){
			if(s[0][i]){
				an[i]=min(an[i],z);
			}
		}
		z++;
	}
	for(int i=1;i<=m;i++){
		cout<<(an[i]<=1e12?an[i]:-1ll)<<endl;
	}
	return 0;
}
原文
存档评论1 条抓取于