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. Java类文件的结构

    Class文件是以8位字节为基础单位的二进制流,各部分中间没有分隔符.遇到8位字节以上的空间数据项时,则会按照高位在前的方式分割成若干个8位字节进行存储. Class文件采用类似C语言的伪结构体来存储 ...

  2. RabbitMQ消息队列(二)-RabbitMQ消息队列架构与基本概念

    没错我还是没有讲怎么安装和写一个HelloWord,不过快了,这一章我们先了解下RabbitMQ的基本概念. RabbitMQ架构 说是架构其实更像是应用场景下的架构(自己画的有点丑,勿嫌弃) 从图中 ...

  3. 【Java基础】【16List集合】

    16.01_集合框架(去除ArrayList中重复字符串元素方式)(掌握) A:案例演示 需求:ArrayList去除集合中字符串的重复值(字符串的内容相同) 思路:创建新集合方式 /** * A:案 ...

  4. ES6躬行记(16)——Set

    ES6引入了两种新的数据结构:Set和Map.Set是一组值的集合,其中值不能重复:Map(也叫字典)是一组键值对的集合,其中键不能重复.Set和Map都由哈希表(Hash Table)实现,并可按添 ...

  5. JVM(五)垃圾回收器的前世今生

    全文共 2195 个字,读完大约需要 8 分钟. 如果垃圾回收的算法属于内存回收的方法论的话,那本文讨论的垃圾回收器就属于内存回收的具体实现. 因为不同的厂商(IBM.Oracle),实现的垃圾回收器 ...

  6. DSAPI多功能组件编程应用-网络相关(下)

    [DSAPI.DLL下载地址] 在本篇,我将重点介绍DSAPI.DLL中Socket编程的使用.众所周知,Socket用起来不难,但是写起来麻烦.我对Socket进行了封装,进行了高度简化.下面我将通 ...

  7. linux yum配置代理

    yum里面可以单独设置代理就是yum源的参数加proxy=“http://ip:PORT”即在/etc/yum.conf中加入下面几句.proxy=http://210.45.72.XX:808pro ...

  8. MySql如何查询JSON字段值的指定key的数据

    实例:SELECT param->'$.pay' as pay_type FROM game.roominfo; 其中:param是roominfo表的一个字段,当中存的是JSON字符串,pay ...

  9. Powerdesigner逆向工程64位Oracle数据库

    Powerdesigner老版本不支持64位Client,新版本弄不到破解码 解决方法,用Powerdesigner+32位Oracle Clent访问64位Oracle Server 遇到的坑分享下 ...

  10. Oracle day02 函数

    order by关键字作用:用于对查询结果进行排序 用法:    1.利用asc .desc对排序列进行升序或降序    2.order by后可以添加多个列(逗号分隔),当一个列的值相同时,在按第二 ...