3天了,求救啊
查看原帖
3天了,求救啊
531499
windows_fleon楼主2022/10/9 17:23
#include<bits/stdc++.h>
using namespace std;
int n,m;
const int N=105010,M=3000010;
priority_queue <pair <int,int>,vector <pair <int,int> >,greater <pair<int,int> > > q;
int head[N],cnt=1,dist[N],dist2[N];
struct aa{
	int u,v,w,next;
}f[M];
void adde(int u,int v,int w){
	f[cnt].u=u;
	f[cnt].v=v;
	f[cnt].w=w;
	f[cnt].next=head[u];
	head[u]=cnt++;
}
void dijkstra(int s){
	for(int i=1;i<=n;i++) dist[i]=dist2[i]=0x3f3f3f3f;
	q.push(make_pair(0,s));
	dist[s]=0;
	while(!q.empty()){
		int u=q.top().second,d=q.top().first;
		q.pop();
		if(d>dist2[u]) continue;
		for(int i=head[u];i!=-1;i=f[i].next){
			int v=f[i].v;
			int d2=d+f[i].w;
			if(d2<dist[v]){
				swap(d2,dist[v]);
				q.push(make_pair(dist[v],v));
			}
			if(d2<dist2[v]&&d2>dist[v]){
				dist2[v]=d2;
				q.push(make_pair(dist2[v],v));
			}
		}
	}
}
int main(){
	cin>>m>>n;
	memset(head,-1,sizeof(head));
	for(int i=1;i<=m;i++){
		int u,v,w;
		cin>>u>>v>>w;
		adde(u,v,w);
		adde(v,u,w);
	}
	dijkstra(1);
	cout<<dist2[n]<<endl;
	return 0;
}
2022/10/9 17:23
加载中...