Z - 飛行路線 HYSBZ - 2763 分層圖最短路

Alice和Bob現在要乘飛機旅行,他們選擇了一家相對便宜的航空公司。該航空公司一共在n個城市設有業務,設這些城市分別標記爲0到n-1,一共有m種航線,每種航線連接兩個城市,並且航線有一定的價格。Alice和Bob現在要從一個城市沿着航線到達另一個城市,途中可以進行轉機。航空公司對他們這次旅行也推出優惠,他們可以免費在最多k種航線上搭乘飛機。那麼Alice和Bob這次出行最少花費多少?

Input

數據的第一行有三個整數,n,m,k,分別表示城市數,航線數和免費乘坐次數。

第二行有兩個整數,s,t,分別表示他們出行的起點城市編號和終點城市編號。(0<=s,t<n)

接下來有m行,每行三個整數,a,b,c,表示存在一種航線,能從城市a到達城市b,或從城市b到達城市a,價格爲c。(0<=a,b<n,a與b不相等,0<=c<=1000)

 

Output

 

只有一行,包含一個整數,爲最少花費。

Sample Input

5 6 1 0 4 0 1 5 1 2 5 2 3 5 3 4 5 2 3 3 0 2 100

Sample Output

8

Hint

 

對於30%的數據,2<=n<=50,1<=m<=300,k=0;

 

對於50%的數據,2<=n<=600,1<=m<=6000,0<=k<=1;

 

對於100%的數據,2<=n<=10000,1<=m<=50000,0<=k<=10.

思路:看了學長的博客學到了分層圖這個東西,第一次知道下面是學長博客鏈接

https://blog.csdn.net/ZscDst/article/details/79489335

主要是建了k+1個分層圖代表了用0個到用k個再用一條權值爲0的線聯繫起來跑最短路ac

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int MAXN = 2e5+5;
typedef long long LL;
const LL INF = 0x3f3f3f3f3f3f3f3f;
 
struct Edge
{
    int from, to; LL dist;       //起點,終點,距離
    Edge(int from, int to, LL dist):from(from), to(to), dist(dist) {}
};
 
struct Dijkstra
{
    int n, m;                 //結點數,邊數(包括反向弧)
    vector<Edge> edges;       //邊表。edges[e]和edges[e^1]互爲反向弧
    vector<int> G[MAXN];      //鄰接表,G[i][j]表示結點i的第j條邊在edges數組中的序號
    int vis[MAXN];            //標記數組
    LL d[MAXN];              //s到各個點的最短路
    int p[MAXN];              //上一條弧
 
    void init(int n)
    {
        this->n = n;
        edges.clear();
        for (int i = 0; i <= n; i++) G[i].clear();
    }
 
    void AddEdge(int from, int to, int dist)
    {
        edges.push_back(Edge(from, to, dist));
        m = edges.size();
        G[from].push_back(m - 1);
    }
 
    struct HeapNode
    {
        int from; LL dist;
        bool operator < (const HeapNode& rhs) const
        {
            return rhs.dist < dist;
        }
        HeapNode(int u, LL w): from(u), dist(w) {}
    };
 
    void dijkstra(int s)
    {
        priority_queue<HeapNode> Q;
        for (int i = 0; i <= n; i++) d[i] = INF;
        memset(vis, 0, sizeof(vis));
        d[s] = 0;
        Q.push(HeapNode(s, 0));
        while (!Q.empty())
        {
            HeapNode x = Q.top(); Q.pop();
            int u = x.from;
            if (vis[u]) continue;
            vis[u] = true;
            for (int i = 0; i < G[u].size(); i++)
            {
                Edge& e = edges[G[u][i]];
                if (d[e.to] > d[u] + e.dist)
                {
                    d[e.to] = d[u] + e.dist;
                    p[e.to] = G[u][i];
                    Q.push(HeapNode(e.to, d[e.to]));
                }
            }
        }
    }
}gao;//最短路劉汝佳板子
int main()
{
	int n,m,k;
	scanf("%d%d%d",&n,&m,&k);
	int xstart,ystart;
	scanf("%d%d",&xstart,&ystart);
	int x,y,step;
	gao.init(n*11);//初始化k*n個點和邊因爲相當於建了k個最短路
	for(int i = 1;i <= m;++i)
	{
		scanf("%d%d%d",&x,&y,&step);
		for(int j = 0;j <= k;++j)
		{
			gao.AddEdge(x+j*n,y+j*n,step);
		 	gao.AddEdge(y+j*n,x+j*n,step);//每層圖到每個點的聯繫建邊	
		 	if(j < k)
		 	{
		 		gao.AddEdge(x+j*n,y+(j+1)*n,0);
				gao.AddEdge(y+j*n,x+(j+1)*n,0);	//讓每層圖聯繫起來
			}
		}	
	 } 
	 gao.dijkstra(xstart);//從開始點跑
	 LL min1 = INF;
	 for(int i = 0;i <= k;++i)
	 {
	 	min1 = min(min1,gao.d[ystart+i*n]);//比較每層最小的點
	 }
	 printf("%lld\n",min1);//輸出最小值
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章