求助P4779
  • 板块学术版
  • 楼主唯有谔谔
  • 当前回复1
  • 已保存回复1
  • 发布时间2022/7/9 16:04
  • 上次更新2023/10/27 21:21:25
查看原帖
求助P4779
158821
唯有谔谔楼主2022/7/9 16:04
#include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>
using namespace std;
struct noe
{
	int next,to,g;
}a[1000001];
int cnt=1;
int head[1000001];
int n,m,s;
typedef pair<int,int> P;
int d[1000001];
void ad(int u,int v,int w)
{
	a[cnt].to=v;
	a[cnt].g=w;
	a[cnt].next=head[u];
	head[u]=cnt++; 
}
int main()
{
	memset(head,-1,sizeof(head));
	priority_queue<P,vector<P>,greater<P> > q;
	memset(d,0x3f,sizeof(d));
	cin>>n>>m>>s;
	d[s]=0;
	q.push(P(0,s)); 
	for(int i=1;i<=m;i++)
	{
		int x,y,z;
		scanf("%d %d %d",&x,&y,&z);
		ad(x,y,z);
	}
	while(!q.empty())
	{
		P p=q.top();
		q.pop();
		int u=p.second;
		if(d[u]>p.first)
		{
			continue;
		}
		else
		{
			for(int i=head[u];i!=-1;i=a[i].next)
		{
			int v=a[i].to;
			if(d[v]>d[u]+a[i].g)
			{
				d[v]=d[u]+a[i].g;
				q.push(P(d[v],v));
			}
		}
		}
		
	}
	for(int i=1;i<=n;i++)
	{
	      printf("%d ",d[i]);
	}
	return 0;
}

3个TLE,求调

2022/7/9 16:04
加载中...