TLE求调
查看原帖
TLE求调
548010
小陈同学cyh楼主2024/11/10 20:25

TLE 2,3,6

#include<bits/stdc++.h>
using namespace std;
int head[100005],cnt,n,m,s,ans[100005];
bool vis[100005];
struct edge
{
	int next;
	int to;
	int wei;
}edge[500005];
struct priority
{
	int ans;
	int id;
	bool operator <(const priority &x)const
	{
		return x.ans<ans;
	}
}tmp;
priority_queue<priority> q;
void addedge(int x,int y,int z)
{
	cnt++;
	edge[cnt].to=y;
	edge[cnt].wei=z;
	edge[cnt].next=head[x];
	head[x]=cnt;
}
int main()
{
	cin>>n>>m>>s;
	for (int i=1;i<=n;i++)
	{
		ans[i]=2147483647;
	}
	ans[s]=0;
	for (int i=1;i<=m;i++)
	{
		int a,b,c;
		scanf("%d%d%d",&a,&b,&c);
		addedge(a,b,c);
	}
	tmp.ans=0;tmp.id=s;
	q.push(tmp);
	while (!q.empty())
	{
		tmp=q.top();
		q.pop();
		if (!vis[tmp.id])
		{
			vis[tmp.id]=true;
		}
		for (int i=head[tmp.id];i;i=edge[i].next)
		{
			int v=edge[i].to;
			if (ans[v]>ans[tmp.id]+edge[i].wei)
			{
				ans[v]=ans[tmp.id]+edge[i].wei;
				if (!vis[v])
				{
					q.push(priority{ans[v],v});
				}
			}
		}
	}
	for (int i=1;i<=n;i++)
	{
		printf("%d ",ans[i]);
	}
}
2024/11/10 20:25
加载中...