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的更多相关文章

  1. 训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板)

    layout: post title: 训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板) author: "luowentaoaa" catalo ...

  2. UVA - 11374 - Airport Express(堆优化Dijkstra)

    Problem    UVA - 11374 - Airport Express Time Limit: 1000 mSec Problem Description In a small city c ...

  3. UVA 11374 Airport Express SPFA||dijkstra

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

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

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

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

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

  6. BNUOJ 19792 Airport Express

    Airport Express Time Limit: 1000ms Memory Limit: 131072KB This problem will be judged on UVA. Origin ...

  7. uva 11374

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

  8. UVA 11374 Airport Express(最短路)

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

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

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

随机推荐

  1. Kafka服务端之网络连接源码分析

    #### 简介 上次我们通过分析KafkaProducer的源码了解了生产端的主要流程,今天学习下服务端的网络层主要做了什么,先看下 KafkaServer的整体架构图 ![file](https:/ ...

  2. ES6-数组的新方法

    1.Array.of() 方法创建一个具有可变数量参数的新数组实例,而不考虑参数的数量或类型. Array.of() 和 Array 构造函数之间的区别在于处理整数参数:Array.of(7)创建一个 ...

  3. Winform中使用zxing和Graphics实现自定义绘制二维码布局

    场景 zxing.dll下载 https://download.csdn.net/download/badao_liumang_qizhi/11623214 效果 实现 根据上面文章中将简单的二维码生 ...

  4. Cookie的有效路径

    程序实现: protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletE ...

  5. SpringAop实现公共字段填充

    一.说明 项目中经常会有一些放在缓存中的公共字段需要进行填充,我们知道mybatis-plus很方便地可以实现公共字段填充.在这里我定义了一个字段填充的注解,当我们需要进行数据填充的时候只要在方法上打 ...

  6. 一文读懂NodeJS全栈开发利器:CabloyJS(万字长文)

    目录 0 修订 0.1 修订说明 0.2 修订历史 1 基本概念 1.1 CabloyJS是什么 1.2 CabloyJS核心解决什么问题 1.3 CabloyJS的开发历程 2 数据版本与开发流程 ...

  7. ogeek线下赛web分析1-python-web

    1.python from flask import Flask, request, render_template,send_from_directory, make_response from A ...

  8. Hive导入数据到HBase,再与Phoenix映射同步

    1. 创建HBase 表 create 'hbase_test','user' 2. 插入数据 put 'hbase_test','111','user:name','jack' put 'hbase ...

  9. 使用CoordinatorLayout打造各种炫酷的效果

    使用CoordinatorLayout打造各种炫酷的效果 自定义Behavior -- 仿知乎,FloatActionButton隐藏与展示 NestedScrolling 机制深入解析 一步步带你读 ...

  10. 使用 Nginx 部署前后端分离项目,解决跨域问题

    前后端分离这个问题其实松哥和大家聊过很多了,上周松哥把自己的两个开源项目部署在服务器上以帮助大家可以快速在线预览(喜大普奔,两个开源的 Spring Boot + Vue 前后端分离项目可以在线体验了 ...