问题描述
众所周知,TT 有一只魔法猫。今天他在 B 站上开启了一次旅行直播,记录他与魔法猫在喵星旅游时的奇遇。 TT 从家里出发,准备乘坐猫猫快线前往喵星机场。猫猫快线分为经济线和商业线两种,它们的速度与价钱都不同。当然啦,商业线要比经济线贵,TT 平常只能坐经济线,但是今天 TT 的魔法猫变出了一张商业线车票,可以坐一站商业线。假设 TT 换乘的时间忽略不计,请你帮 TT 找到一条去喵星机场最快的线路,不然就要误机了!
输入
输入包含多组数据。每组数据第一行为 3 个整数 N, S 和 E (2 ≤ N ≤ 500, 1 ≤ S, E ≤ 100),即猫猫快线中的车站总数,起点和终点(即喵星机场所在站)编号。下一行包含一个整数 M (1 ≤ M ≤ 1000),即经济线的路段条数。接下来有 M 行,每行 3 个整数 X, Y, Z (1 ≤ X, Y ≤ N, 1 ≤ Z ≤ 100),表示 TT 可以乘坐经济线在车站 X 和车站 Y 之间往返,其中单程需要 Z 分钟。下一行为商业线的路段条数 K (1 ≤ K ≤ 1000)。接下来 K 行是商业线路段的描述,格式同经济线。所有路段都是双向的,但有可能必须使用商业车票才能到达机场。保证最优解唯一。
输出
对于每组数据,输出3行。第一行按访问顺序给出 TT 经过的各个车站(包括起点和终点),第二行是 TT 换乘商业线的车站编号(如果没有使用商业线车票,输出"Ticket Not Used",不含引号),第三行是 TT 前往喵星机场花费的总时间。本题不忽略多余的空格和制表符,且每一组答案间要输出一个换行
输入样例
4 1 4
4
1 2 2
1 3 3
2 4 4
3 4 5
1
2 4 3
输出样例
1 2 4
2
5
思路
这里将经济线和商业线分开讨论,因为商业只能采用一条,故可以先不考虑商业线,只考虑经济线时的最短路径,然后列举每一条商业性计算利用商业性会不会减少。
这里采用Dijkstra算法计算只用经济线时的最短路。dijkstra算法是这样实现的, 首先求出设置 s 为源点, 𝑑𝑖𝑠 [a] 表示源点 s 到点 a 的最短距离, 初始化 𝑑𝑖𝑠 [s] = 0, 𝑑𝑖𝑠 [𝑖] = 𝑖𝑛𝑓 (无穷大), 将 s 加入最小堆 ,每次从堆中取出一个点 x,遍历 x 的所有邻接边 (𝑥 𝑦 𝑤) ,比较 𝑑𝑖𝑠[ 𝑦] 与 𝑑𝑖𝑠 [𝑥] + 𝑤 的大小 ,这一比较过程称为松弛操作,如果松弛成功,这里指𝑑𝑖𝑠[ 𝑦]> 𝑑𝑖𝑠 [𝑥] + 𝑤,pre[y]=pre[x]记录x的前驱,令𝑑𝑖𝑠[ 𝑦] = 𝑑𝑖𝑠 [𝑥] + 𝑤并将dis[y]加入该最小堆,直至堆空。
因为边是无向的,所以遍历商业边时需要考虑站点方向,所以我们可以分别以起点和终点为其实点进行一次dij,计算出两点的到其他点的最短路,然后分别以商业线的一端靠近起点和终点计算距离,和最小距离作比较,最小距离初始化为不用商业线的最小距离,如果小于最小值就更新最小值记录minu, minv表示靠近起点终点的商业线两个站点。最后如果没有使用商业线,直接按照pre数组利用一个vector倒序输出就行了。如果使用了商业线就在path1从minu到起点开始遍历,插入到vector队尾,然后再在path2从minv到终点开始遍历插入到vector队首,最后逆序输出路径
代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <cmath>
#include <vector>
using namespace std;
const int inf = 10000000;
const int maxn = 505;
int N, S, E, m, K, head[maxn], tot = 0;
int dis1[maxn], dis2[maxn], vis[maxn], path1[maxn], path2[maxn];
bool flag = false;
struct couple {
int d, y;
couple() {}
couple(int a, int b) {
d = a, y = b;
}
bool operator<(const couple &c) const {
return d > c.d;
}
};
struct edge {
int u,v, w, next = -1;
}e[10005];
priority_queue<pair<int,int>> q;
//前向星
void add(int u, int v, int w) {
e[tot].u = u;
e[tot].v = v;
e[tot].w = w;
e[tot].next = head[u];
head[u] = tot;
tot++;
}
void dij(int s, int*dis, int*path)
{
while (q.size())q.pop();
for (int i = 1; i <= N; i++)
{
vis[i] = path[i]=0;
dis[i] = inf;
}
path[s] = -1;
dis[s] = 0;
q.push(make_pair(0, s));
while (!q.empty()) {
int x = q.top().second;
q.pop();
if (vis[x])continue;
vis[x] = 1;
for(int i=head[x];;i=e[i].next)
{
int y=e[i].v;
int w=dis[x]+e[i].w;
if(dis[y]>w){
dis[y]=w;
path[y]=x;
q.push(make_pair(-dis[y],y));
}
if (e[i].next == -1)break;
}
}
}
void init() {
for (int i = 0; i <= N; i++) {
head[i] = -1;
}
tot=0;
}
int main()
{
while (scanf("%d%d%d", &N, &S, &E) != EOF) {
init();
cin >> m;
while (m--) {
int x, y, z;
scanf("%d%d%d",&x,&y,&z);
add(x, y, z);
add(y, x, z);
}
//分别以起点和终点为源点求单源最短路
dij(S, dis1, path1);
dij(E, dis2, path2);
vector<int> v;
int minu, minv, ans = dis1[E];
bool no=0;
scanf("%d",K);
for (int i = 1; i <= K; i++)
{//枚举每条商业线
int x, y, z;
scanf("%d%d%d",&x,&y,&z);
if (dis1[x] + dis2[y] + z < ans) {
minu = x; minv = y;
ans = dis1[x] + dis2[y] + z;
no=1;
}
if (dis2[x] + dis1[y] + z < ans) {
minu = y; minv = x;
ans = dis2[x] + dis1[y] + z;
no=1;
}
}
//回车
if (!flag) flag = true;
else scanf("\n");
if (no)//使用商业线
{
for (int i = minu; i != S; i = path1[i])
v.push_back(i);
v.push_back(S);
for (int i = minv; i != E; i = path2[i])
v.insert(v.begin(), i);
v.insert(v.begin(), E);
for (int i = v.size() - 1; i >= 1; i--)
printf("%d ", v[i]);
printf("%d\n%d\n%d\n", v[0], minu, ans);
}
else
{
for (int i = E; i != S; i = path1[i])
v.push_back(i);
v.push_back(S);
for (int i = v.size() - 1; i >= 1; i--)
printf("%d ", v[i]);
printf("%d\nTicket Not Used\n%d\n", v[0], dis1[E]);
}
}
// system("pause");
return 0;
}