uva 11374
|
Problem D: Airport Express |

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 Zminutes 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 Commercial-Xpress 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
4 1 4
4
1 2 2
1 3 3
2 4 4
3 4 5
1
2 4 3
Sample Output
1 2 4
2
5 最短路
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue> using namespace std; const int MAX_N = ;
const int edge = ;
const int INF = ( << );
struct node {
int d,u;
bool operator < (const node &rhs) const {
return d > rhs.d;
}
}; int N,S,E,M,K;
int first[MAX_N],v[edge],Next[edge];
int w[edge],d1[MAX_N],d2[MAX_N];
int x[MAX_N * ],y[MAX_N * ],w1[MAX_N * ];
bool done[MAX_N];
int p1[MAX_N],p2[MAX_N];
int ou[MAX_N],tem[MAX_N]; void add_edge(int id,int u) {
int e = first[u];
Next[id] = e;
first[u] = id;
} void dijkstra(int s,int *d,int *p) {
fill(p + ,p + N + ,-);
for(int i = ; i <= N; ++i) d[i] = INF;
d[s] = ;
memset(done,,sizeof(done));
priority_queue<node > q;
q.push(node {d[s],s}); while(!q.empty()) {
node x = q.top(); q.pop();
int u = x.u;
if(done[u]) continue;
done[u] = ;
for(int e = first[u]; e != -; e = Next[e]) {
if(d[ v[e] ] > d[u] + w[e]) {
d[ v[e] ] = d[u] + w[e];
q.push(node { d[ v[e] ],v[e]});
p[ v[e] ] = u; }
} }
} void output(int id) {
int len = ;
int len1 = ; if(id != ) {
int a,b;
a = id < ? y[-id] :x[id];
b = id < ? x[-id] :y[id];
for(int p = a; p != -; p = p1[p]) {
tem[len++] = p;
}
for(int i = len - ; i >= ; --i) {
ou[len1++] = tem[i];
}
//printf("b= %d\n",p2[b]);
for(int p = b; p != -; p = p2[p]) {
ou[len1++] = p;
}
} else { for(int p = E; p != -; p = p1[p]) {
ou[len1++] = p;
}
for(int i = ,j = len1 - ; i < j; ++i,--j) {
swap(ou[i],ou[j]);
}
} for(int i = ; i < len1; ++i) {
printf("%d%c",ou[i],i == len1 - ? '\n' : ' ');
}
} void solve() {
dijkstra(S,d1,p1);
dijkstra(E,d2,p2);
int ans = d1[E],id = ;
//printf("ans = %d\n",ans);
for(int i = ; i <= K; ++i) {
if(ans > d1[ x[i]] + d2[ y[i] ] + w1[i]) {
ans = d1[ x[i] ] + d2[ y[i] ] + w1[i];
id = i;
}
if(ans > d1[ y[i] ] + d2[ x[i] ] + w1[i]) {
ans = d1[ y[i] ] + d2[ x[i] ] + w1[i];
id = -i;
}
} output(id);
if(id == ) {
printf("Ticket Not Used\n");
} else {
printf("%d\n",id < ? y[-id] : x[id]);
} printf("%d\n",ans); } int main()
{
//freopen("sw.in","r",stdin);
bool ok = ;
while(~scanf("%d%d%d",&N,&S,&E)) {
if(ok) printf("\n");
ok = ;
scanf("%d",&M);
for(int i = ; i <= N; ++i) first[i] = -;
for(int i = ; i < * M; i += ) {
int u;
scanf("%d%d%d",&u,&v[i],&w[i]);
v[i + ] = u;
w[i + ] = w[i];
add_edge(i,u);
add_edge(i + ,v[i]);
} scanf("%d",&K);
for(int i = ; i <= K; ++i) {
scanf("%d%d%d",&x[i],&y[i],&w1[i]);
} solve(); } //cout << "Hello world!" << endl;
return ;
}
uva 11374的更多相关文章
- UVA - 11374 - Airport Express(堆优化Dijkstra)
Problem UVA - 11374 - Airport Express Time Limit: 1000 mSec Problem Description In a small city c ...
- 训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板)
layout: post title: 训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板) author: "luowentaoaa" catalo ...
- uva 11374 最短路+记录路径 dijkstra最短路模板
UVA - 11374 Airport Express Time Limit:1000MS Memory Limit:Unknown 64bit IO Format:%lld & %l ...
- UVA 11374 Airport Express(最短路)
最短路. 把题目抽象一下:已知一张图,边上的权值表示长度.现在又有一些边,只能从其中选一条加入原图,使起点->终点的距离最小. 当加上一条边a->b,如果这条边更新了最短路,那么起点st- ...
- UVA 11374 Airport Express SPFA||dijkstra
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- UVA 11374 Halum (差分约束系统,最短路)
题意:给定一个带权有向图,每次你可以选择一个结点v 和整数d ,把所有以v为终点的边权值减少d,把所有以v为起点的边权值增加d,最后要让所有的边权值为正,且尽量大.若无解,输出结果.若可无限大,输出结 ...
- UVA 11374 Airport Express 机场快线(单源最短路,dijkstra,变形)
题意: 给一幅图,要从s点要到e点,图中有两种无向边分别在两个集合中,第一个集合是可以无限次使用的,第二个集合中的边只能挑1条.问如何使距离最短?输出路径,用了第二个集合中的哪条边,最短距离. 思路: ...
- UVA 11374 Airport Express(枚举+最短路)
枚举每条商业线<a, b>,设d[i]为起始点到每点的最短路,g[i]为终点到每点的最短路,ans便是min{d[a] + t[a, b] + g[b]}.注意下判断是否需要经过商业线.输 ...
- UVa 11374 机场快线
https://vjudge.net/problem/UVA-11374 题意: 机场快线分为经济线和商业线两种,线路.速度和价格都不同.你有一张商业线车票,可以坐一站商业线,而其他时候只能乘坐经济线 ...
随机推荐
- 第一篇代码 嗨翻C语言 21点扑克
/* * 计算牌面点数的程序. * 使用“拉斯难加斯公开许可证”. * 学院21点扑克游戏小组. */#include <stdio.h>#include <stdlib.h& ...
- MongoDB分片简单实例
分片 在Mongodb里面存在另一种集群,就是分片技术,可以满足MongoDB数据量大量增长的需求. 当MongoDB存储海量的数据时,一台机器可能不足以存储数据也足以提供可接受的读写吞吐量.这时,我 ...
- Php+Redis 实现Redis提供的lua脚本功能
<?php require_once "predis-0.8/autoload.php"; $config['schema'] = 'tcp'; $config['host' ...
- hdu 2275 Kiki & Little Kiki 1
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2275 题意:n个操作 Push 入容器 Pop弹出一个 满足<=该数的最大的数(若没有输出No ...
- ios中怎么样转行大小写
转换大小写:lowercaseString(小写) uppercaseString(大写)
- 基于Elasticsearch的自定义评分算法扩展
实现思路: 重写评分方法,调整计算文档得分的过程,然后根据function_score或script_sort进行排序检索. 实现步骤: 1.新建java项目TestProject,引入Elast ...
- PHP闭包(Closure)初探
不知不觉发现PHP已经出到了5.5版本,而自己一直在用PHP5.2,让我看起来像深山出来的小伙子一样,又土又落后.在我习惯在javascript中使用闭包之后,忽然间对PHP的闭包打起了兴趣. 于是乎 ...
- ListBox mvvm 学习笔记
1. ListBox MvvM 例子1. 简单的绑定,ItemsSource 绑定到一个实现了IEnumerable 的类上.一般该绑定都是双向的,所以优先考虑使用 ObservableCollec ...
- 【AFNetworking】AFNetworking源码阅读(一)
1. 前言 2. iOS Example代码结构 3.AFNetworkActivityIndicatorManager 4. UIRefreshControl+AFNetworking 5. AFN ...
- C++中数组求偏移量计算公式
已知数组:type A[10][5]A[0][0] --A[8][4]面试常考:数组定义A[0....x][0...y]已知A[m][n] --求A[k][l]的地址: &A[m][n] ...