52分求助
查看原帖
52分求助
215817
gyc071116楼主2024/10/26 10:51

前三个点TLE

#include<bits/stdc++.h>
using namespace std;
struct ee{
	int u;
	int next;
	int val;
}edge[200005];
int h[100005],etot,path[100005];
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > q;
void add_edge(int v,int u,int val){
	edge[etot]=(ee){u,h[v],val};
	h[v]=etot++;
}
int main(){
	int n,m,s;
	scanf("%d %d %d",&n,&m,&s);
	for(int i=1;i<=n;i++){
		h[i]=-1;
		path[i]=0x3f3f3f3f;
	}
	for(int i=1;i<=m;i++){
		int c,cc,y;
		scanf("%d %d %d",&c,&cc,&y);
		add_edge(c,cc,y);
	}
	path[s]=0;
	q.push(make_pair(s,0));
	while(!q.empty()){
		int Node=q.top().first,Path=q.top().second;
		q.pop();
		if(Path==path[Node]){
			for(int i=h[Node];i!=-1;i=edge[i].next){
				if(edge[i].val+Path<path[edge[i].u]){
					path[edge[i].u]=edge[i].val+Path;
					q.push(make_pair(edge[i].u,path[edge[i].u]));
				}
			}
		}
	}
	for(int i=1;i<=n;i++) printf("%d ",path[i]);
	return 0;
}
2024/10/26 10:51
加载中...