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. 分享个人学习js的笔记

    1.回到顶部效果. 2.滚动条向上滚动式,滑动滚轮.解决bug的方法. 3.有关Document. 4.getElementByClassName();获取元素类名的封装.单个类名的元素.任然不完美. ...

  2. 调整linux系统时间和硬件时间

    1.yum -y install ntp 2.cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime(如果拷贝失败,可以  mv /etc/localt ...

  3. Net基础篇_学习笔记_第九天_数组_三个练习

    练习一: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Sys ...

  4. 6、二叉树树(java实现)

    1.创建树的节点 public class Node { public Object data; //存储数据 public Node leftChild; //左子树指针 public Node r ...

  5. PTA A1007&A1008

    第四天 A1007 Maximum Subsequence Sum (25 分) 题目内容 Given a sequence of K integers { N1, N2, ..., NK }. A ...

  6. SpringCloud学习笔记(7):使用Spring Cloud Config配置中心

    简介 Spring Cloud Config为分布式系统中的外部化配置提供了服务器端和客户端支持,服务器端统一管理所有配置文件,客户端在启动时从服务端获取配置信息.服务器端有多种配置方式,如将配置文件 ...

  7. go语言 链表练习

    package main import "fmt" //定义节点 type Node struct { Data int Next *Node } /* * 返回第一个节点 * h ...

  8. 从CAP到zookeeper和eureka对比

    今天看了一篇eureka对比zookeeper的文章,对zookeeper满足CAP中的CP,eureka满足AP产生了一点疑问,故写此篇文章进行一些探讨. 首先我们来看看CAP的定义 Consist ...

  9. Dungeon Master POJ-2251 三维BFS

    题目链接:http://poj.org/problem?id=2251 题目大意 你被困在了一个三维的迷宫,找出能通往出口的最短时间.如果走不到出口,输出被困. 思路 由于要找最短路径,其实就是BFS ...

  10. 两个变量交换数字 不用第三个变量的情况下 int a = 5,b = 6

    今天可是涨见识额 记录一下 第一种方法: a=a+bb=a-ba=a-b 第二种: b= a+(a=b)*0 一句话搞定