luogu P4568 [JLOI2011]飞行路线 分层最短路

#include <cstring>
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <queue>
using namespace std;
typedef long long LL;
const int N = 2500001;
int e[N],ne[N],idx,h[N],w[N];
int vis[N];
int dist[N];
int n,m,k;
int s,t;
struct Dijkstra
{
	void init()
	{
		memset(h,-1,sizeof h);
		memset(dist,0x3f,sizeof dist);
		memset(vis,0,sizeof vis);
		idx=0;
	}
	void add(int a,int b,int c)
	{
		e[idx]=b;
		w[idx]=c;
		ne[idx]=h[a];
		h[a]=idx++;
	}
	void dijkstra()
	{
		memset(dist,0x3f,sizeof(dist));
		dist[s]=0;
		priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > points;
		points.push(make_pair(0,s));
		while(!points.empty())
		{
			int u=points.top().second;
			points.pop();
			if(!vis[u])
			{
				vis[u]=1;
				for(int i=h[u]; ~i; i=ne[i])
				{
					int to=e[i];
					if(dist[to]>dist[u]+w[i])
					{
						dist[to]=dist[u]+w[i];
						points.push(make_pair(dist[to],to));
					}
				}
			}
		}
	}
} dj;
int main()
{
	cin>>n>>m>>k;
	cin>>s>>t;
	dj.init();
	while(m--)
	{
		int a,b,c;
		cin>>a>>b>>c;
		for(int i=0; i<=k; i++)
		{
			dj.add(a+i*n,b+i*n,c);
			dj.add(b+i*n,a+i*n,c);
			if(i!=k)
			{
				dj.add(a+i*n,b+(i+1)*n,0);
				dj.add(b+i*n,a+(i+1)*n,0);
			}
		}
	}
	for(int i=1; i<=k; ++i)
		dj.add(t+(i-1)*n,t+i*n,0);
	dj.dijkstra();
	cout<<dist[t+k*n]<<endl;
	return 0;
}

posted @ 2020-05-18 13:30  晴屿  阅读(110)  评论(0编辑  收藏  举报