luogu 2865 次短路模板

题目

#include
#include
#include
#include
#include
#include
#include 
#include
#include
#include 
#include
#include
#include
#include
#include
#define IO ios::sync_with_stdio(false)
#define eps 1e-7
#define int long long
using namespace std;
int n,m,dis1[5005],dis2[5005];
struct node
{
	int v,val;
	bool operator < (const node &vv) const 
	{
		return vv.val<val;
	}
};
list<node>lis[5005];
priority_queue<node>q;
signed main()
{
	IO;
	cin>>n>>m;
	for(int i=1;i<=m;i++)
	{
		int x,y,v;
		cin>>x>>y>>v;
		lis[x].push_back((node){y,v});
		lis[y].push_back((node){x,v});
	}
	fill(dis1+1,dis1+5002+1,0xfffffff);
	fill(dis2+1,dis2+5002+1,0xfffffff);
	q.push((node){1,0});
	dis1[1]=0;
	while(!q.empty())
	{
		int x=q.top().v,dis=q.top().val;
		q.pop();
		if(dis>dis2[x])continue;
		for(auto it=lis[x].begin();it!=lis[x].end();it++)
		{
			int y=it->v;
			if(dis1[y]>dis+it->val)
			{
				dis2[y]=dis1[y];
				dis1[y]=dis+it->val;
				q.push((node){y,dis1[y]});
			}
			if(dis2[y]>dis+it->val&&dis+it->val>dis1[y])
			{
				dis2[y]=dis+it->val;
				q.push((node){y,dis2[y]});
			}
		}
	}
	cout<<dis2[n];
}

你可能感兴趣的:(次短路)