Description

Problem D: Airport Express

In a small city called Iokh, a train service, Airport-Express, takes residents to the airport more quickly than other transports. There are two types of trains in Airport-Express, the
Economy-Xpress and the Commercial-Xpress. They travel at different speeds, take different routes and have different costs.

Jason is going to the airport to meet his friend. He wants to take the Commercial-Xpress which is supposed to be faster, but he doesn't have enough money. Luckily he has a ticket for the Commercial-Xpress which can take him one station forward. If he used
the ticket wisely, he might end up saving a lot of time. However, choosing the best time to use the ticket is not easy for him.

Jason now seeks your help. The routes of the two types of trains are given. Please write a program to find the best route to the destination. The program should also tell when the ticket should be used.

Input

The input consists of several test cases. Consecutive cases are separated by a blank line.

The first line of each case contains 3 integers, namely N,
S and E (2 ≤ N ≤ 500, 1 ≤
S, EN), which represent the number of stations, the starting point and where the airport is located respectively.

There is an integer M (1 ≤ M ≤ 1000) representing the number of connections between the stations of the Economy-Xpress. The next
M lines give the information of the routes of the Economy-Xpress. Each consists of three integers
X, Y and Z (X,
YN, 1 ≤ Z ≤ 100). This means
X and Y are connected and it takes
Z minutes to travel between these two stations.

The next line is another integer K (1 ≤ K ≤ 1000) representing the number of connections between the stations of the Commercial-Xpress. The next
K lines contain the information of the Commercial-Xpress in the same format as that of the Economy-Xpress.

All connections are bi-directional. You may assume that there is exactly one optimal route to the airport. There might be cases where you MUST use your ticket in order to reach the airport.

Output

For each case, you should first list the number of stations which Jason would visit in order. On the next line, output "Ticket Not Used" if you decided NOT to use the ticket; otherwise, state the station where Jason should get on the train
of Commercial-Xpress. Finally, print the total time for the journey on the last line. Consecutive sets of output must be separated by a blank line.

Sample Input

4 1 4
4
1 2 2
1 3 3
2 4 4
3 4 5
1
2 4 3

Sample Output

1 2 4
2
5

题意:去机场有两种方法,一个是经济线一个是商业线。线路、速度、价格都不同样,你有一张商业票。能够坐一站商业线。而其它时候仅仅能做经济线,换乘时间不计算,你的任务是找一条去机场最快的线路。

思路:枚举商业线的起点和终点,然后分别从我们的起点和终点最短路。然后找出最优解就能够了

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int MAXN = 505;
const int INF = 0x3f3f3f3f; struct Edge {
int from, to, dist;
}; struct HeapNode {
int d, u;
bool operator< (const HeapNode rhs) const {
return d > rhs.d;
}
}; struct Dijkstra {
int n, m; // 点数和边数
vector<Edge> edges; //边列表
vector<int> G[MAXN]; // 每一个点出发的边编号(0開始)
bool done[MAXN]; // 是否已标记
int d[MAXN]; //s 到各个点的距离
int p[MAXN]; //最短路中上一个点,也能够是上一条边 void init(int n) {
this->n = n;
for (int i = 0; i < n; i++)
G[i].clear();
edges.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);
} void dijkstra(int s) {
priority_queue<HeapNode> Q;
for (int i = 0; i < n; i++)
d[i] = INF;
d[s] = 0;
memset(done, 0, sizeof(done));
Q.push((HeapNode){0, s});
while (!Q.empty()) {
HeapNode x = Q.top();
Q.pop();
int u = x.u;
if (done[u])
continue;
done[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] = e.from;
Q.push((HeapNode){d[e.to], e.to});
}
}
}
} void getPath(vector<int> &path, int s, int e) {
int cur = e;
while (1) {
path.push_back(cur);
if (cur == s)
return ;
cur = p[cur];
}
}
};
int n, m, k, s, e;
int x, y, z;
vector<int> path; int main() {
int first = 1;
while (scanf("%d%d%d", &n, &s, &e) != EOF) {
if (first)
first = 0;
else printf("\n");
s--, e--;
Dijkstra ans[2];
ans[0].init(n);
ans[1].init(n);
scanf("%d", &m);
while (m--) {
scanf("%d%d%d", &x, &y, &z);
x--, y--;
ans[0].AddEdge(x, y, z);
ans[0].AddEdge(y, x, z);
ans[1].AddEdge(x, y, z);
ans[1].AddEdge(y, x, z);
}
ans[0].dijkstra(s);
ans[1].dijkstra(e);
scanf("%d", &k);
path.clear();
int Min = ans[0].d[e];
int flagx = -1, flagy = -1;
while (k--) {
scanf("%d%d%d", &x, &y, &z);
x--, y--;
if (Min > ans[0].d[x] + z + ans[1].d[y]) {
Min = ans[0].d[x] + z + ans[1].d[y];
flagx = x, flagy = y;
}
if (Min > ans[1].d[x] + z + ans[0].d[y]) {
Min = ans[1].d[x] + z + ans[0].d[y];
flagx = y, flagy = x;
}
}
if (flagx == -1) {
ans[0].getPath(path, s, e);
reverse(path.begin(), path.end());
for (int i = 0; i < path.size()-1; i++)
printf("%d ", path[i]+1);
printf("%d\n", path[path.size()-1]+1);
printf("Ticket Not Used\n");
printf("%d\n", Min);
}
else {
ans[0].getPath(path, s, flagx);
reverse(path.begin(), path.end());
ans[1].getPath(path, e, flagy);
for (int i = 0; i < path.size()-1; i++)
printf("%d ", path[i]+1);
printf("%d\n", path[path.size()-1]+1);
printf("%d\n", flagx+1);
printf("%d\n", Min);
}
}
return 0;
}

UVA - 11374 Airport Express (Dijkstra模板+枚举)的更多相关文章

  1. UVa 11374 - Airport Express ( dijkstra预处理 )

    起点和终点各做一次单源最短路, d1[i], d2[i]分别代表起点到i点的最短路和终点到i点的最短路,枚举商业线车票cost(a, b);  ans = min( d1[a] + cost(a, b ...

  2. UVA - 11374 - Airport Express(堆优化Dijkstra)

    Problem    UVA - 11374 - Airport Express Time Limit: 1000 mSec Problem Description In a small city c ...

  3. UVA 11374 Airport Express SPFA||dijkstra

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  4. UVA 11374 Airport Express 机场快线(单源最短路,dijkstra,变形)

    题意: 给一幅图,要从s点要到e点,图中有两种无向边分别在两个集合中,第一个集合是可以无限次使用的,第二个集合中的边只能挑1条.问如何使距离最短?输出路径,用了第二个集合中的哪条边,最短距离. 思路: ...

  5. UVA 11374 Airport Express(枚举+最短路)

    枚举每条商业线<a, b>,设d[i]为起始点到每点的最短路,g[i]为终点到每点的最短路,ans便是min{d[a] + t[a, b] + g[b]}.注意下判断是否需要经过商业线.输 ...

  6. UVA 11374 Airport Express (最短路)

    题目只有一条路径会发生改变. 常见的思路,预处理出S和T的两个单源最短路,然后枚举商业线,商业线两端一定是选择到s和t的最短路. 路径输出可以在求最短路的同时保存pa数组得到一棵最短路树,也可以用di ...

  7. UVA 11374 Airport Express(最短路)

    最短路. 把题目抽象一下:已知一张图,边上的权值表示长度.现在又有一些边,只能从其中选一条加入原图,使起点->终点的距离最小. 当加上一条边a->b,如果这条边更新了最短路,那么起点st- ...

  8. uva 11374 最短路+记录路径 dijkstra最短路模板

    UVA - 11374 Airport Express Time Limit:1000MS   Memory Limit:Unknown   64bit IO Format:%lld & %l ...

  9. 训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板)

    layout: post title: 训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板) author: "luowentaoaa" catalo ...

随机推荐

  1. HttpClient 学习整理 (转)

    source:http://www.blogjava.net/Alpha/archive/2007/01/22/95216.html HttpClient 是我最近想研究的东西,以前想过的一些应用没能 ...

  2. ''tensorflow.python.framework.errors_impl.ResourceExhaustedError: OOM when allocating tensor with shape[?]'' 错误分析

    这是tensorflow 一个经常性错误,错误的原因在于:显卡内存不够. 解决方法就是降低显卡的使用内存,途径有以下几种措施: 1 减少Batch 的大小 2 分析错误的位置,在哪一层出现显卡不够,比 ...

  3. 大项目之网上书城(五)——主页(End)

    目录 大项目之网上书城(五)--主页(End) 主要改动 1.主页(终于完成啦) 完整代码 效果图 2.head.jsp的小改动 代码 3.login.jsp ###代码 效果图 4.login.js ...

  4. [SCOI2011]棘手的操作(可并堆/并查集/线段树)

    我懒死了 过于棘手 但这题真的很水的说 毕竟写啥都能过 常见思路: ①:由于不强制在线,所以重新编号之后线段树维护 ②:用各种可以高速合并的数据结构,比如可并堆,可并平衡树啥的 讲一种无脑算法: 对于 ...

  5. python3.x Day5 异常处理

    异常处理: 预计可能会发生的异常,明确如果发生,如何处理,不过一般不参与业务逻辑,也不要一次性捕捉全部异常,不然可能程序就不可控了. data={} mmm=[] try: #捕获异常, data[& ...

  6. 常见算法的python实现

    提到排序算法,常见的有如下几种:冒泡排序.选择排序.插入排序.快速排序.堆排序.归并排序.希尔排序:查找算法最常见二分查找.这些算法的时间复杂度如下: 算法名称 时间复杂度(一般情况) 冒泡排序 O( ...

  7. LeetCode (45) Jump Game II

    题目 Given an array of non-negative integers, you are initially positioned at the first index of the a ...

  8. Spring核心技术(八)——Spring自动装载的注解

    本文针对自动装载的一些注解进行描述. 基于注解的容器配置 @Required注解 @Required注解需要应用到Bean的属性的setter方法上面,如下面的例子: public class Sim ...

  9. 关于OPENSSL的EVP函数的使用

    4月份没什么做,就是做了OPENSSL的 加密和解密的应用,现在公开一下如何调用OPENSSL对字符串进行加密和解密,当中也学会了对加密数据进行BASE64编码,现在公开一下代码,在这感谢GITHUB ...

  10. 利用express启一个server服务

    安装express $ npm install express --save 在node.js中,我们最常用的框架就是express Express 是一个基于 Node.js 平台的极简.灵活的 w ...