P4568 [JLOI2011] 飞行路线 (分层图最短路)

测试链接:https://www.luogu.com.cn/problem/P4568

P4568 [JLOI2011] 飞行路线

题目描述

Alice 和 Bob 现在要乘飞机旅行,他们选择了一家相对便宜的航空公司。该航空公司一共在 \(n\) 个城市设有业务,设这些城市分别标记为 \(0\)\(n-1\),一共有 \(m\) 种航线,每种航线连接两个城市,并且航线有一定的价格。

Alice 和 Bob 现在要从一个城市沿着航线到达另一个城市,途中可以进行转机。航空公司对他们这次旅行也推出优惠,他们可以免费在最多 \(k\) 种航线上搭乘飞机。那么 Alice 和 Bob 这次出行最少花费多少?

输入格式

第一行三个整数 \(n,m,k\),分别表示城市数,航线数和免费乘坐次数。

接下来一行两个整数 \(s,t\),分别表示他们出行的起点城市编号和终点城市编号。

接下来 \(m\) 行,每行三个整数 \(a,b,c\),表示存在一种航线,能从城市 \(a\) 到达城市 \(b\),或从城市 \(b\) 到达城市 \(a\),价格为 \(c\)

输出格式

输出一行一个整数,为最少花费。

输入输出样例 #1

输入 #1

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

输出 #1

8

说明/提示

数据规模与约定

对于 \(30\%\) 的数据,\(2 \le n \le 50\)\(1 \le m \le 300\)\(k=0\)

对于 \(50\%\) 的数据,\(2 \le n \le 600\)\(1 \le m \le 6\times10^3\)\(0 \le k \le 1\)

对于 \(100\%\) 的数据,\(2 \le n \le 10^4\)\(1 \le m \le 5\times 10^4\)\(0 \le k \le 10\)\(0\le s,t,a,b < n\)\(a\ne b\)\(0\le c\le 10^3\)

另外存在一组 hack 数据。

思路:

这道题目考虑扩点,把使用各个次数的免单情况都放入dis数组中,这样dis数组就变为了n*(k+1)的二维数组
初始化为大数字,为了方便后面更新最小值,然后建立邻接表存储无向图的信息,vis数组表示是否访问过该节点且不同免单使用次数的情况,使用小根堆,每次访问此时cost最小的点,后面分别考虑免单和不免单的情况

题解:

#include <bits/stdc++.h>
using namespace std;
const int N=1e4+10;
const int M=5e4+10;
typedef long long ll;
int n,m,k;
int s,e;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m>>k;
    cin>>s>>e;
    vector<vector<pair<int,int>>>graph(n);
    vector<vector<int>>dis(n,vector<int>(k+1,INT_MAX-5000));
    vector<vector<bool>>vis(n,vector<bool>(k+1,false));
    priority_queue<vector<int>,vector<vector<int>>,greater<vector<int>>>pq;
    
    for(int i=0;i<m;i++)
    {
        int u,v,w;
        cin>>u>>v>>w;
        graph[u].push_back({v,w});
        graph[v].push_back({u,w});
    }
    pq.push({0,0,s});
    dis[s][0]=0;
    int cmd=0;
    while(!pq.empty())
    {
        vector<int> node =pq.top();
        int cur = node[2];
        int t = node[1];
        int cost = node[0];
        pq.pop();
        if(vis[cur][t])continue;
        if(cur==e)
        {
            cout<<cost<<endl;
            break;
        }
        if(t<k&&!vis[cur][t+1]&&dis[cur][t]<dis[cur][t+1])
        {
            dis[cur][t+1] = dis[cur][t];
            pq.push({dis[cur][t+1],t+1,cur});
        }
        vis[cur][t]=true;
        for(auto it:graph[cur])
        {
            int nt = it.first;
            int ct = it.second;
            if(!vis[nt][t])
            {
                if(dis[nt][t]>dis[cur][t]+ct)
                {
                    dis[nt][t]=dis[cur][t]+ct;
                    pq.push({dis[nt][t],t,nt});
                }
            }

            if(t<k&&!vis[nt][t+1])
            {
                if(dis[nt][t+1]>dis[cur][t])
                {
                    dis[nt][t+1]=dis[cur][t];
                    pq.push({dis[nt][t+1],t+1,nt});
                }
            }
        }
    }

    return 0;
}
posted @ 2025-07-24 16:35  屈臣  阅读(65)  评论(0)    收藏  举报