50分求调
查看原帖
50分求调
1139221
Agil12_楼主2024/5/2 17:04
#include<bits/stdc++.h>
#define x first
#define y second
#define long long int
using namespace std;
const int N=1e5+10;
typedef pair<int,int>P;
int n,m;
vector<P>g[N];
int d1[N],d2[N];
bool st[N];
int dijkstra()
{
	memset(d1,0x3f,sizeof d1);
	memset(d2,0x3f,sizeof d2);
	d1[1]=0;
	priority_queue<P,vector<P>,greater<P>>q;
	q.push({0,1});
	//st[1]=true;
	while(q.size())
	{
		auto t=q.top();
		q.pop();
		int id=t.y;
		if(st[id])continue;
		st[id]=true;
		if(t.x>d2[id])continue;
		for(auto &w:g[id])
		{
			if(d1[w.y]>t.x+w.x)
			{
				d2[w.y]=d1[w.y];
				d1[w.y]=t.x+w.x;
			    q.push({d1[w.y],w.y});
			}
			if(d2[w.y]>t.x+w.x&&t.x+w.x>d1[w.y])
			{
				d2[w.y]=t.x+w.x;
				q.push({d2[w.y],w.y});
			}
		}
	}
	return d2[n];
}
signed main()
{
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>n>>m;
	while(m--)
	{
		int a,b,c;
		cin>>a>>b>>c;
		g[a].push_back({c,b});
		g[b].push_back({c,a});
	}
	cout<<dijkstra()<<endl;
	
	return 0;
}
2024/5/2 17:04
加载中...