UVA - 11374 - Airport Express(堆优化Dijkstra)
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
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)的更多相关文章
- UVA 11374 Airport Express SPFA||dijkstra
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- UVA - 11374 Airport Express (Dijkstra模板+枚举)
Description Problem D: Airport Express In a small city called Iokh, a train service, Airport-Express ...
- UVA 11374 Airport Express 机场快线(单源最短路,dijkstra,变形)
题意: 给一幅图,要从s点要到e点,图中有两种无向边分别在两个集合中,第一个集合是可以无限次使用的,第二个集合中的边只能挑1条.问如何使距离最短?输出路径,用了第二个集合中的哪条边,最短距离. 思路: ...
- UVa 11374 - Airport Express ( dijkstra预处理 )
起点和终点各做一次单源最短路, d1[i], d2[i]分别代表起点到i点的最短路和终点到i点的最短路,枚举商业线车票cost(a, b); ans = min( d1[a] + cost(a, b ...
- UVA 11374 Airport Express(最短路)
最短路. 把题目抽象一下:已知一张图,边上的权值表示长度.现在又有一些边,只能从其中选一条加入原图,使起点->终点的距离最小. 当加上一条边a->b,如果这条边更新了最短路,那么起点st- ...
- UVA 11374 Airport Express (最短路)
题目只有一条路径会发生改变. 常见的思路,预处理出S和T的两个单源最短路,然后枚举商业线,商业线两端一定是选择到s和t的最短路. 路径输出可以在求最短路的同时保存pa数组得到一棵最短路树,也可以用di ...
- UVA 11374 Airport Express(枚举+最短路)
枚举每条商业线<a, b>,设d[i]为起始点到每点的最短路,g[i]为终点到每点的最短路,ans便是min{d[a] + t[a, b] + g[b]}.注意下判断是否需要经过商业线.输 ...
- uva 11374 最短路+记录路径 dijkstra最短路模板
UVA - 11374 Airport Express Time Limit:1000MS Memory Limit:Unknown 64bit IO Format:%lld & %l ...
- BZOJ 3040 最短路 (堆优化dijkstra)
这题不是裸的最短路么?但是一看数据范围就傻了.点数10^6,边数10^7.这个spfa就别想了(本来spfa就是相当不靠谱的玩意),看来是要用堆优化dijkstra了.但是,平时写dijkstra时为 ...
随机推荐
- transient和synchronized的使用
transient和synchronized这两个关键字没什么联系,这两天用到了它们,所以总结一下,两个关键字做个伴! transient 持久化时不被存储,当你的对象实现了Serializable接 ...
- 全文检索-Elasticsearch (一) 安装与基础概念
ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口 Elasticsearch由java开发,所以在搭建时,需先安 ...
- leetcode — palindrome-partitioning
import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * Source : https://o ...
- [三]基础数据类型之Integer详解
Integer 基本数据类型int 的包装类 Integer 类型的对象包含一个 int 类型的字段 属性简介 值为 2^31-1 的常量,它表示 int 类型能够表示的最大值 @N ...
- 一个emoji引发的一条血案:mysql存储emoji表情字符时报错解决
以下是我插入一条带表情的数据到mysql后出现错误 2019-03-04 14:24:40,462 ERROR 2807 [-/139.199.27.244/-/2ms POST /api/activ ...
- C# Task 篇幅一
在https://www.cnblogs.com/loverwangshan/p/10415937.html中我们有讲到委托的异步方法,Thread,ThreadPool,然后今天来讲一下Task, ...
- C# List 集合 交集、并集、差集、去重, 对象集合、 对象、引用类型、交并差补、List<T>
关键词:C# List 集合 交集.并集.差集.去重, 对象集合. 对象.引用类型.交并差.List<T> 有时候看官网文档是最高效的学习方式! 一.简单集合 Intersect 交集, ...
- Ubuntu 18.04 安装java8
step1: 添加ppa sudo add-apt-repository ppa:webupd8team/java sudo apt-get update step2: 安装oracle-java-i ...
- SpringBoot项目部署到服务器上,tomcat不启动该项目
今天lz把项目重新传到服务器上后,重启tomcat遇到个问题,就是这个tomcat怎么都不启动这个项目,别的项目都没事,一番查找后发现问题所在. 我们先建个SpringBoot工程,重现一下问题: 写 ...
- Fundebug前端JavaScript插件更新至1.7.1,拆分录屏代码,还原部分Script error.
摘要: BUG监控插件压缩至18K. 1.7.1拆分了录屏代码,BUG监控插件压缩至18K,另外我们还原了部分Script error,帮助用户更方便地Debug.请大家及时更新哈~ 拆分录屏代码 从 ...