TLE(qwq
查看原帖
TLE(qwq
540177
wyf_sinon楼主2023/5/27 16:47

堆优化Dijkastra但是TLEwww

#include <bits/stdc++.h>
using namespace std;
#define LL long long
void read(LL &a)
{
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    a=x*f;
}
void write(int x) 
{
     if(x<0) putchar('-'),x=-x;
     if(x>9) write(x/10);
     putchar(x%10+'0');
}
const LL N=1e5+10,M=1e6+10,MX=0x3f3f3f3f3f3f3f3f;
LL n,m,s,t,k,ans=MX;
LL h[N],enter[M],ne[M],idx,edge[M],dis[N];
LL u,v,w;
bool vis[N];
typedef pair<long long,long long> PII;
priority_queue<PII, vector<PII>, greater<PII> > q;
void add(LL a,LL b,LL c) {
	edge[idx]=c;
	ne[idx]=h[a];
	enter[idx]=b;
	h[a]=idx++;
}
void Dijkastra () {
	memset(dis,0x3f,sizeof dis);
	dis[s]=0;
	q.push({0,s});
	while(q.size()) {
		PII xx=q.top();LL x=xx.second;
		q.pop();
		if(vis[x]) continue;
		vis[x]=true;
		for(int i=h[x]; i!=-1; i=ne[i]) {
			int y=enter[i];
			if( dis[y] > dis[x]+edge[i] ) {
				dis[y]=dis[x]+edge[i];
				q.push({dis[y],y});
			}
		}
	}
}
int main() {
	memset(h,-1,sizeof h);
	read(n);read(m);read(k);read(s);read(t);
	for(int i=1; i<=m; i++) {
		read(u);read(v);read(w);
		add(u,v,w);add(v,u,w);
		for(int j=1;j<=k;j++)
		{
			add(u+j*n,v+j*n,w);add(v+j*n,u+j*n,w);
			add(u+(j-1)*n,v+j*n,0);add(v+(j-1)*n,u+j*n,0);
		}
	}
	Dijkastra();
	for(int i=0;i<=k;i++) ans=min(ans,dis[t+n*i]);
	write(ans);
}
2023/5/27 16:47
加载中...