【蒟蒻求助】奇怪的TLE
查看原帖
【蒟蒻求助】奇怪的TLE
330381
zhangchengyan楼主2022/9/5 21:11

91pts91pts ,吸氧也没过去,代码如下:

#include<bits/stdc++.h>
using namespace std;
int cnt=1,n,p,k,head[6000005],to[6000005],v[6000005],nxt[6000005],sum[1000005],in[1000005],s,t;
void add(int x,int y,int z)
{
	to[cnt]=y;
	v[cnt]=z;
	nxt[cnt]=head[x];
	head[x]=cnt++;
}
bool bfs(int s)
{
	memset(in,0,sizeof(in));
	queue<int> q;
	memset(sum,63,sizeof(sum));
	q.push(s),sum[s]=0,in[s]=1;
	while(!q.empty())
	{
		int d=q.front();
		q.pop();
		in[d]=0;
		for(int i=head[d];i!=-1;i=nxt[i])
		{
			int nw=to[i],nv=v[i];
			if(sum[d]+v[i]<sum[nw])
			{
				sum[nw]=sum[d]+v[i];
				if(!in[nw])
				{
					q.push(nw);
					in[nw]=1;
				}
			}
		}
	}
	return sum[t]<=k;
}
int main()
{
	ios::sync_with_stdio(0);
	memset(head,-1,sizeof(head));
	cin>>n>>p>>k;
	cin>>s>>t;
	++s,++t;
	while(p--)
	{
		int x,y,z;
		cin>>x>>y>>z;
		++x,++y;
		add(x,y,z),add(y,x,z);
        for(int j=1;j<=k;++j)
        {
            add(x+(j-1)*n,y+j*n,0);
            add(y+(j-1)*n,x+j*n,0);
            add(x+j*n,y+j*n,z);
            add(y+j*n,x+j*n,z);
        }
	}
	for(int i=1;i<=k;++i)
	  add(t+(i-1)*n,t+i*n,0);
	bfs(s);
	cout<<sum[t+k*n];
	return 0;
}
2022/9/5 21:11
加载中...