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条.问如何使距离最短?输出路径,用了第二个集合中的哪条边,最短距离. 思路: ...
随机推荐
- 基于队列queue实现的线程池
本文通过文章同步功能推送至博客园,显示排版可能会有所错误,请见谅! 写在前文:在Python中给多进程提供了进程池类,对于线程,Python2并没有直接提供线程池类(Python3中提供了线程池功能) ...
- 持续集成高级篇之Jenkins windows/linux混合集群搭建(二)
系列目录 前面我们说过,要使用ssh方式来配置windows从节点,如果采用ssh方式,则windows和linux配置从节点几乎没有区别,目前发现的惟一的区别在于windows从节点上目录要设置在c ...
- 008 Python基本语法元素小结
目录 一.概要 二.保留字 三.温度转换 一.概要 缩进.注释.命名.变量.保留字 数据类型.字符串. 整数.浮点数.列表 赋值语句.分支语句.函数 input().print().eval(). p ...
- Redis真的那么好用吗
Redis是什么 Redis是一个开源的底层使用C语言编写的key-value存储数据库.可用于缓存.事件发布订阅.高速队列等场景.而且支持丰富的数据类型:string(字符串).hash(哈希).l ...
- CF979C Kuro and Walking Route(简单的dfs/树形dp)
题意:给出一个$n$个点,$n-1$条边的无向连通图,给出两个点$x,y$,经过$x$后的路径上就不能经过$y$,问可以走的路径$(u,v)$有多少条,($(u,v)$和$(v,u)$考虑为两条不同的 ...
- Python中使用pip安装库时提示:远程主机强迫关闭了一个现有的连接
场景 在cmd中使用pip install moviepy时,需要安装一些依赖库,很长时间后提示: 远程主机中断了一个现有的连接. 原因是默认镜像源下载过慢,将其修改为国内或者设置安装时的源. 这里以 ...
- sqoop导oracle数据到hive中并动态分区
静态分区: 在hive中创建表可以使用hql脚本: test.hql USE TEST; CREATE TABLE page_view(viewTime INT, userid BIGINT, pag ...
- IO流的工具类
1.需要先导入jar包: FilenameUtils import org.apache.commons.io.FilenameUtils; public class FilenameUtilesDe ...
- ElasticSearch实战系列二: ElasticSearch的DSL语句使用教程---图文详解
前言 在上一篇中介绍了ElasticSearch集群和kinaba的安装教程,本篇文章就来讲解下 ElasticSearch的DSL语句使用. ElasticSearch DSL 介绍 Elastic ...
- 记录工作中linux相关操作
在项目部署之后,查看日志能查看部署结果是否正确部署. 最开始查看日志我会使用cat service.log tail -f service.log vim service.log 打开日志之后 /+查 ...