4,5RE求调
查看原帖
4,5RE求调
752711
hateful_bug楼主2024/11/1 08:17
#include<bits/stdc++.h>
using namespace std;
const int N=3e5+10;
int n,m,h[N],b[1000000],nx[1000000],v[1000000],d[1000000],tt,mod,ans;
bool pd[N];
void zj(int x,int y,int z)
{
	b[++tt]=y;
	v[tt]=z;
	nx[tt]=h[x];
	h[x]=tt;
}
void dji()
{
	priority_queue<pair<int,int> > q;
	q.push({0,0});
	memset(d,0x3f3f3f3f,sizeof(d));
	d[0]=0;
	while(!q.empty())
	{
		int x=q.top().second;
		q.pop();
		if(pd[x])
		continue;
		pd[x]=true;
		for(int i=h[x];i;i=nx[i])
		{
			int y=b[i],z=v[i];
			if(d[y]>d[x]+z)
			{
				d[y]=d[x]+z;
				q.push({-d[y],y});
			}
		}
	}
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
	{
		int len;
		scanf("%d",&len);
		for(int j=0;j<=m;j++)
		pd[len-j]=true;
	}
	for(int i=1;i<=3000;i++)
	if(pd[i])
	{
		if(mod)
		for(int j=0;j<mod;j++)
		zj(j,(j+i)%mod,i);
		else
		mod=i;
	}
	dji();
	for(int i=0;i<mod;i++)
	ans=max(ans,d[i]-mod);
	if(ans==0||ans>1000000000)
	printf("-1");
	else
	printf("%d",ans);
	return 0;
}
2024/11/1 08:17
加载中...