Airport Express UVA - 11374
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.
题解:
这道题思路还是十分巧妙,考虑如果使用商业票的路径,一定是从原点出发,跑一条最短路到某条边的端点,经过这条边然后再走最短路到终点,那么既然只有一条边就可以用枚举这条边,我们考虑预处理出起点和终点的最短路,就可以O1算解了,当然这个题目可以考虑分层图的做法,而且输出时候也方便一些,格式十分繁琐,看代码吧,不让会一直格式错误。
代码:
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<stdlib.h>
#include<queue>
#define ll long long
#define MAXN 3000
using namespace std;
int dis[][MAXN],pre[][MAXN],have[MAXN],hh[MAXN];
int n,m,k,s,t,num=;
queue<int> q;
struct edge{
int first;
int next;
int to;
int quan;
}a[MAXN*]; void addedge(int from,int to,int quan){
a[++num].to=to;
a[num].quan=quan;
a[num].next=a[from].first;
a[from].first=num;
} void spfa(int s,int dis[],int pre[]){
for(int i=;i<=n;i++) have[i]=pre[i]=,dis[i]=<<;
while(!q.empty()) q.pop();
dis[s]=;q.push(s);
while(!q.empty()){
int now=q.front();
q.pop();
have[now]=;
for(int i=a[now].first;i;i=a[i].next){
int to=a[i].to,quan=a[i].quan;
if(dis[to]>dis[now]+quan){
dis[to]=dis[now]+quan;
pre[to]=now;
if(!have[to]){
have[to]=;
q.push(to);
}
}
}
}
} void print(int ss){
memset(hh,,sizeof(hh));int numm=;
for(int now=ss;pre[][now]!=;now=pre[][now]){
hh[++numm]=now;
}
hh[++numm]=s;
for(int i=numm;i>=;i--) printf("%d ",hh[i]);printf("%d",hh[]);
} int main(){
int Case=;
while(scanf("%d%d%d",&n,&s,&t)!=EOF){
if(Case++) printf("\n");
memset(a,,sizeof(a)),num=;
scanf("%d",&m);
for(int i=;i<=m;i++){
int x,y,z;scanf("%d%d%d",&x,&y,&z);
addedge(x,y,z),addedge(y,x,z);
}
spfa(s,dis[],pre[]),spfa(t,dis[],pre[]);
scanf("%d",&k);
int ans=dis[][t],from=,to=;
for(int i=;i<=k;i++){
int x,y,z;scanf("%d%d%d",&x,&y,&z);
if((ll)ans>(ll)dis[][x]+(ll)z+(ll)dis[][y]){
ans=dis[][x]+z+dis[][y],from=x,to=y;
}
if((ll)ans>(ll)dis[][y]+(ll)z+(ll)dis[][x]){
ans=dis[][y]+z+dis[][x],from=y,to=x;
}
}
if(!from){
print(t);
printf("\nTicket Not Used\n");
printf("%d\n",dis[][t]);
}
else{
print(from);printf(" ");
for(int now=to;pre[][now]!=;now=pre[][now]){
printf("%d ",now);
}
printf("%d",t);
printf("\n%d\n",from);
printf("%d\n",ans);
}
}
}
Airport Express UVA - 11374的更多相关文章
- 训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板)
layout: post title: 训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板) author: "luowentaoaa" catalo ...
- UVA - 11374 - Airport Express(堆优化Dijkstra)
Problem UVA - 11374 - Airport Express Time Limit: 1000 mSec Problem Description In a small city c ...
- 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 最短路+记录路径 dijkstra最短路模板
UVA - 11374 Airport Express Time Limit:1000MS Memory Limit:Unknown 64bit IO Format:%lld & %l ...
- BNUOJ 19792 Airport Express
Airport Express Time Limit: 1000ms Memory Limit: 131072KB This problem will be judged on UVA. Origin ...
- uva 11374
Problem D: Airport Express In a small city called Iokh, a train service, Airport-Express, takes resi ...
- UVA 11374 Airport Express(最短路)
最短路. 把题目抽象一下:已知一张图,边上的权值表示长度.现在又有一些边,只能从其中选一条加入原图,使起点->终点的距离最小. 当加上一条边a->b,如果这条边更新了最短路,那么起点st- ...
- UVA 11374 Airport Express 机场快线(单源最短路,dijkstra,变形)
题意: 给一幅图,要从s点要到e点,图中有两种无向边分别在两个集合中,第一个集合是可以无限次使用的,第二个集合中的边只能挑1条.问如何使距离最短?输出路径,用了第二个集合中的哪条边,最短距离. 思路: ...
随机推荐
- vue.js安装教程
vue.js环境搭建 1.下载node.js 网址:https://nodejs.org/en/ 版本:v10.16.3 2.安装node.js Node.js下载如下所示: 检查nodejs是否安装 ...
- PHPOffice 导入
1.因为Phpexecel已经停止维护,所以要使用心得phpoffice; 2.注意引入 use PhpOffice\PhpSpreadsheet\Helper\Sample; use PhpOffi ...
- JSP四大作用域属性范围
JSP四大作用域分别为:page, request ,session, application . 第一个作用域是page,他只在当前页面有效,也就是用户请求的页面有效,当当前页面关闭或转到其他页面时 ...
- Maven生成项目站点
概述 Maven不仅仅是一个自动化构建工具和一个依赖工具,还能够帮助聚合项目信息.POM可以包含各种项目信息.如项目描述.版本控制系统地址.缺陷跟踪系统地址.许可证信息.开发者信息等. 另Maven社 ...
- Caused by: java.net.UnknownHostException
项目中使用某一组件,启动失败Caused by: java.net.UnknownHostException: xxxCentOS6.3: xxxCentOS6.3 解析不到xxxCentOS6.3. ...
- 让Samba支持Windows10的自动发现
Windows10如果开了SMB 1.0支持,就非常不安全,不开就搜索不到samba的NETBIOS. 在安装配置好samba,并且确认windows可以通过netbios名访问后. 可以使用http ...
- MOOC web前端开发笔记(一)
网站和网页 网站 互联网上用于展示特定内容的相关网页的集合. 网页 网站中的一页,一个网站中的网页通过"超链接"的方式被组织在一起. 主页 进入网站看到的第一个网页,主页的文件名通 ...
- vue-cli+webpack打包,上线
1.先修改配置文件再打包.有些人打包后运行一片空白,主要是由于路径问题 所以首先需要修改config下的index.js配置文件 上图中第一个要修改的就是静态文件的路径,打包后静态文件就在当前目录下, ...
- Eclipse中Spring Boot响应jsp的简单demo
首先在Eclipse里新建一个maven工程,这里的打包类型和父包如果后续再去pom中添加也可以 此时的工程路径是这样的 接下来去到pom中添加相关的依赖,如果有报错maven update一下即可 ...
- web前端开发面试题(附答案)-3
1.用纯css创建一个三角形的原理: .demo{ width:0; height: 0; border: 5px solid transparent; border-left-color: red; ...