Problem    UVA - 11374 - Airport Express

Time Limit: 1000 mSec

Problem Description

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,E ≤ N), 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,Y ≤ N,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 CommercialXpress 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

题解:考虑枚举用哪个商业票,为什么这么想呢,因为堆优化Dijkstra复杂度(n+m)logn,乘上个K,如果没有多组数据的话应该是能过的,其实可以做到更好,分别从起点和终点跑两遍最短路,这样对于枚举的用商业票的那一段来说就可以常数时间内算出总费用,因为最短路一定是w(u, v) + dist[u](起点到u最短路) + dist2[v](v到终点最短路),这样问题就在O(K)时间内解决了。

 #include <bits/stdc++.h>

 using namespace std;

 #define REP(i, n) for (int i = 1; i <= (n); i++)
#define sqr(x) ((x) * (x)) const int maxn = + ;
const int maxm = + ;
const int maxs = + ; typedef long long LL;
typedef pair<int, int> pii;
typedef pair<double, double> pdd; const LL unit = 1LL;
const int INF = 0x3f3f3f3f;
const LL mod = ;
const double eps = 1e-;
const double inf = 1e15;
const double pi = acos(-1.0); struct Edge
{
int to, w, next;
} edge[maxm]; struct HeapNode
{
int dis, u;
bool operator<(const HeapNode &a) const
{
return dis > a.dis;
}
}; int tot, head[maxn];
int n, m, k;
int st, en; void init()
{
tot = ;
memset(head, -, sizeof(head));
} void AddEdge(int u, int v, int w)
{
edge[tot].to = v;
edge[tot].w = w;
edge[tot].next = head[u];
head[u] = tot++;
} int dist[maxn], dist2[maxn];
int pre[maxn], Next[maxn];
bool vis[maxn]; int Dijkstra()
{
memset(dist, INF, sizeof(dist));
memset(vis, false, sizeof(vis));
memset(pre, -, sizeof(pre));
priority_queue<HeapNode> que;
pre[st] = st;
dist[st] = ;
que.push((HeapNode){, st});
while (!que.empty())
{
HeapNode first = que.top();
que.pop();
int u = first.u;
if (vis[u])
continue;
vis[u] = true;
for (int i = head[u]; i != -; i = edge[i].next)
{
int v = edge[i].to;
if (dist[v] > dist[u] + edge[i].w)
{
pre[v] = u;
dist[v] = dist[u] + edge[i].w;
que.push((HeapNode){dist[v], v});
}
}
}
return dist[en];
} void Dijkstra2()
{
memset(dist2, INF, sizeof(dist2));
memset(vis, false, sizeof(vis));
memset(Next, -, sizeof(Next));
priority_queue<HeapNode> que;
dist2[en] = ;
Next[en] = en;
que.push((HeapNode){, en});
while (!que.empty())
{
HeapNode first = que.top();
que.pop();
int u = first.u;
if (vis[u])
continue;
vis[u] = true;
for (int i = head[u]; i != -; i = edge[i].next)
{
int v = edge[i].to;
if (dist2[v] > dist2[u] + edge[i].w)
{
Next[v] = u;
dist2[v] = dist2[u] + edge[i].w;
que.push((HeapNode){dist2[v], v});
}
}
}
} int main()
{
ios::sync_with_stdio(false);
cin.tie();
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
bool ok = false;
while (cin >> n >> st >> en)
{
init();
cin >> m;
int x, y, z;
for (int i = ; i < m; i++)
{
cin >> x >> y >> z;
AddEdge(x, y, z);
AddEdge(y, x, z);
}
int Min = Dijkstra();
//cout << "Min:" << Min << endl;
Dijkstra2();
cin >> k;
int ansu = -, ansv = -;
for(int i = ; i < k; i++)
{
cin >> x >> y >> z;
if(dist[x] + z + dist2[y] < Min)
{
Min = dist[x] + z + dist2[y];
ansu = x, ansv = y;
}
if(dist[y] + z + dist2[x] < Min)
{
Min = dist[y] + z + dist2[x];
ansu = y, ansv = x;
}
}
if(!ok)
ok = true;
else
cout << endl;
//cout << "Min:" << Min << endl;
if(ansu == - && ansv == -)
{
int tmp = st;
while(tmp != en)
{
cout << tmp << " ";
tmp = Next[tmp];
}
cout << en << endl;
cout << "Ticket Not Used" << endl;
cout << Min << endl;
}
else
{
int tmp = ansu;
stack<int> ans;
while(!ans.empty())
ans.pop();
while(tmp != st)
{
ans.push(tmp);
tmp = pre[tmp];
}
ans.push(st);
while(!ans.empty())
{
cout << ans.top() << " ";
ans.pop();
}
tmp = ansv;
while (tmp != en)
{
cout << tmp << " ";
tmp = Next[tmp];
}
cout << en << endl;
cout << ansu << endl;
cout << Min << endl;
}
}
return ;
}

UVA - 11374 - Airport Express(堆优化Dijkstra)的更多相关文章

  1. UVA 11374 Airport Express SPFA||dijkstra

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

  2. UVA - 11374 Airport Express (Dijkstra模板+枚举)

    Description Problem D: Airport Express In a small city called Iokh, a train service, Airport-Express ...

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

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

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

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

  5. UVA 11374 Airport Express(最短路)

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

  6. UVA 11374 Airport Express (最短路)

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

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

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

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

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

  9. BZOJ 3040 最短路 (堆优化dijkstra)

    这题不是裸的最短路么?但是一看数据范围就傻了.点数10^6,边数10^7.这个spfa就别想了(本来spfa就是相当不靠谱的玩意),看来是要用堆优化dijkstra了.但是,平时写dijkstra时为 ...

随机推荐

  1. Spring系列(七) Spring MVC 异常处理

    Servlet传统异常处理 Servlet规范规定了当web应用发生异常时必须能够指明, 并确定了该如何处理, 规定了错误信息应该包含的内容和展示页面的方式.(详细可以参考servlet规范文档) 处 ...

  2. 如何用浏览器在线查看.ipynb文件

            当我们用jupyter notebook编辑好.ipynb文件后,肯定会想不用运行jupyter notebook也能方便得查看.ipynb的文件,如果直接打开.ipynb的文件,我们 ...

  3. 如何优雅的使用 Angular 表单验证

    随便说说,这一节可以跳过 去年参加 ngChine 2018 杭州开发者大会的时候记得有人问我: Worktile 是什么时候开始使用 Angular 的,我说是今年(2018年) 3 月份开始在新模 ...

  4. 如何定制Linux外围文件系统?

    本文由云+社区发表 作者:我是乖宝宝哦 一般来说,我们所说的Linux系统指的是各种基于Linux Kernel和GNU Project的操作系统发行版.为了掌握Linux操作系统的使用,了解 Lin ...

  5. 4.镜像管理【Docker每天5分钟】

    Docker给PaaS世界带来的“降维打击”,其实是提供了一种非常便利的打包机制.该机制打包了应用运行所需要的整个操作系统,从而保证了本地环境和云端环境的高度一致,避免了用户通过“试错”来匹配不同运行 ...

  6. springmvc 项目完整示例08 前台页面以及知识点总结

    至此已经基本测试成功了,我们稍作完善,让它成为一个更加完整的项目 我们现在重新规划下逻辑 两个页面 一个登录页面 一个欢迎页面 登陆页面输入账号密码,登陆成功的话,跳转登陆成功 欢迎页面 并且,更新用 ...

  7. js for循环删除两个数组相同元素

    var id = ['a','b','c','a','d','a','a','b','d','c','a','b','c','a','b','c'] var del = ['a','c']; var ...

  8. React-router杂记

    HashRouter: 即对应url中的hash值,如xx.com/#/a.xx.com/#/a/b, 服务器对任务url都返回同一个url,具体的路径由浏览器区分,因为浏览器不会发送hash后面的值 ...

  9. MEF 基础简介 三

    MEF导出类的方法和属性 首先来说导出属性,因为这个比较简单,和导出类差不多,先来看看代码,主要看我加注释的地方,MusicBook.cs中的代码如下: using System; using Sys ...

  10. C# 实现对PPT文档加密、解密以及重置密码的操作

    工作中我们会使用到各种各样的文档,其中,PPT起着不可或缺的作用.一份PPT文档里可能包含重要商业计划.企业运营资料或者公司管理资料等.因此,在竞争环境里,企业重要资料的保密工作就显得尤为重要,而对于 ...