A - Airport Express

Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Appoint description: System Crawler  (2014-03-01)

Description

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 theCommercial-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 Mlines 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 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

::刚开始还以为这道题路是单向的,wa了很多次。这道题枚举商业线的做法挺经典,跟判断某条线路是否在最短路上很类似(分别以起点st和终点en跑两次最短路,得dis,dis2,当dis[u]+w(u,v)+dis2[v]=dis[en],可以判断w(u,v)在最短路上),这道题我是用spfa算法做的

代码:

   1: #include <iostream>

   2: #include <cstdio>

   3: #include <cstring>

   4: #include <algorithm>

   5: #include <queue>

   6: #include <stack>

   7: using namespace std;

   8: typedef long long ll;

   9: const int INF=1e9+7;

  10: const int N=505;

  11: bool vis[N];

  12: int head[N];

  13: int dis[N],dis2[N],pre[N],pre2[N];

  14: int n,m,k,st,en,cnt=0;

  15:  

  16: struct edge

  17: {

  18:     int u,v,w,p;

  19:     edge() {}

  20:     edge(int a,int b,int c,int d):u(a),v(b),w(c),p(d) {}

  21: }e[N<<4];

  22:  

  23: void spfa(edge e[],int head[],int dis[],int st,int pre[])

  24: {

  25:     memset(vis,0,sizeof(vis));

  26:     for(int i=1; i<=n; i++) dis[i]=INF;

  27:     queue<int >q;

  28:     dis[st]=0;

  29:     q.push(st);

  30:     while(!q.empty())

  31:     {

  32:         int u=q.front();q.pop();

  33:         vis[u]=0;

  34:         for(int i=head[u]; ~i; i=e[i].p)

  35:         {

  36:             int v=e[i].v;

  37:             if(dis[u]+e[i].w<dis[v])

  38:             {

  39:                 dis[v]=dis[u]+e[i].w;

  40:                 pre[v]=u;

  41:                 if(!vis[v])

  42:                 {

  43:                     q.push(v);

  44:                     vis[v]=1;

  45:                 }

  46:             }

  47:         }

  48:     }

  49: }

  50:  

  51: void printa(int x)

  52: {

  53:     if(x==st)

  54:     {

  55:         printf("%d",x);

  56:         return;

  57:     }

  58:     printa(pre[x]);

  59:     printf(" %d",x);

  60: }

  61:  

  62: void printb(int x)

  63: {

  64:     printf(" %d",x);

  65:     if(x==en) return ;

  66:     printb(pre2[x]);

  67: }

  68:  

  69: int main()

  70: {

  71: //    freopen("in.txt","r",stdin);

  72:     int flag=0;

  73:     while(scanf("%d%d%d",&n,&st,&en)>0)

  74:     {

  75:         if(flag) printf("\n");

  76:         memset(head,-1,sizeof(head));

  77:         memset(pre,-1,sizeof(pre));

  78:         memset(pre2,-1,sizeof(pre2));

  79:         scanf("%d",&m);

  80:         cnt=0;

  81:         for(int i=0; i<m; i++)

  82:         {

  83:             int u,v,w;

  84:             scanf("%d%d%d",&u,&v,&w);

  85:             e[cnt]=edge(u,v,w,head[u]);

  86:             head[u]=cnt++;

  87:             e[cnt]=edge(v,u,w,head[v]);

  88:             head[v]=cnt++;

  89:         }

  90:         spfa(e,head,dis,st,pre);

  91:         spfa(e,head,dis2,en,pre2);

  92: //        for(int i=1; i<=n ; i++) printf("%ddis=%d\n",i,dis[i]);

  93: //        for(int i=1; i<=n ; i++) printf("%ddis2=%d\n",i,dis2[i]);

  94:  

  95:         scanf("%d",&k);

  96:         int a=0,b=0,L=dis[en];

  97:         for(int i=0; i<k; i++)

  98:         {

  99:             int u,v,w;

 100:             scanf("%d%d%d",&u,&v,&w);

 101:             if(dis[u]+dis2[v]+w<L)

 102:             {

 103:                 a=u,b=v,L=dis[u]+dis2[v]+w;

 104:             }

 105:             if(dis[v]+dis2[u]+w<L)

 106:             {

 107:                 a=v,b=u,L=dis[v]+dis2[u]+w;

 108:             }

 109:         }

 110:         if(a==0)

 111:         {

 112:             printa(en);

 113:             printf("\nTicket Not Used\n");

 114:         }

 115:         else

 116:         {

 117:             printa(a);

 118:             printb(b);

 119:             printf("\n%d\n",a);

 120:         }

 121:         printf("%d\n",L);

 122:         flag=1;

 123:     }

 124:     return 0;

 125: }

一下是一些测试数据:

/*

7 1 3
9

1 2 1

2 3 100

1 4 20

1 5 10

5 4 10

5 6 10

6 4 20

4 7 30

3 7 10

0

(

1 4 7 3

Ticket Not Used

60

)

7 1 3

9

1 2 1

2 3 100

1 4 20

1 5 10

5 4 10

5 6 10

6 4 20

4 7 30

3 7 10

1

5 4 1

(

1 5 4 7 3

5

51

)

*/

/*

3 1 3

1

2 3 2

1

1 2 1

3 1 3

2

1 2 8

2

1 2 1

2 3 2

4 1 4

2

1 2 8

3 4 1000

2

1 2 1

2 3 2

3 1 3

2

1 2 5

2 3 8

2

1 2 1

2 3 2

4 1 4

4

1 2 2

1 3 3

2 4 4

3 4 5

1

2 4 3

4 1 4

4

1 2 2

1 3 3

2 4 4

3 4 5

1

2 4 30

*/

Uva-11374-Airport Express的更多相关文章

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

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

  2. UVA 11374 Airport Express SPFA||dijkstra

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

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

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

  4. UVA 11374 Airport Express(最短路)

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

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

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

  6. UVA 11374 Airport Express(枚举+最短路)

    枚举每条商业线<a, b>,设d[i]为起始点到每点的最短路,g[i]为终点到每点的最短路,ans便是min{d[a] + t[a, b] + g[b]}.注意下判断是否需要经过商业线.输 ...

  7. UVa 11374 - Airport Express ( dijkstra预处理 )

    起点和终点各做一次单源最短路, d1[i], d2[i]分别代表起点到i点的最短路和终点到i点的最短路,枚举商业线车票cost(a, b);  ans = min( d1[a] + cost(a, b ...

  8. UVA 11374 Airport Express (最短路)

    题目只有一条路径会发生改变. 常见的思路,预处理出S和T的两个单源最短路,然后枚举商业线,商业线两端一定是选择到s和t的最短路. 路径输出可以在求最短路的同时保存pa数组得到一棵最短路树,也可以用di ...

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

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

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

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

随机推荐

  1. 重构第9天:提取接口(Extract Interface)

    理解:提取接口的意思是,多于一个类共同使用某个类中的方法或属性,那么我们可以把这些方法和属性提出来,作为一个单独的接口.这样的好处是解除代码间的依赖,降低耦合性. 详解: 先看重构前的代码: publ ...

  2. Python入门笔记(23):模块

    一.模块基础 1.模块 自我包含,且有组织的代码片段就是模块 模块是Pyhon最高级别的程序组织单元,它将程序代码和数据封装起来以便重用.实际的角度,模块往往对应Python程序文件. 每个文件都是一 ...

  3. linux怎么模糊查找一个文件

    linux如何模糊查找一个文件 在当前目录下搜索指定文件: find . -name test.txt 在当前目录下模糊搜索文件: find . -name '*.txt' 在当前目录下搜索特定属性的 ...

  4. 优秀程序设计的Kiss原则(keep it simple,stupid)

    优秀程序设计的Kiss原则(keep it simple,stupid) 良好的编程原则与良好的设计工程原则密切相关.本文总结的这些设计原则,帮助开发者更有效率的编写代码,并帮助成为一名优秀的程序员. ...

  5. Chrome使用技巧(几个月的心得)

    转用Chrome,不仅仅因为它的插件之丰富,更因为它的响应速度其他浏览器都望尘莫及.接着我就要写写一些心得. 如何最简易地用上谷歌搜索? 1,下载hosts文件:https://pan.baidu.c ...

  6. 桂电在linux、Mac OS环境下使用出校器(支持2.14)

    这是guetsec学长在三院科协学长所抓包逆向分析1.81版出校器的基础上,用python写的一款为Mac和linux环境开发的出校器. 最后我做了略微修改,支持暂时最新版本2.14.下面有直接从源码 ...

  7. [Tool] Windows 8.1安装SQL Server

    [Tool] Windows 8.1安装SQL Server 问题情景 因为工作的关系,需要在Windows 8.1.64Bit设备上安装SQL Server 2012.本来以为是个只要按下一步就可以 ...

  8. 函数改变全局变量-JS

    切记,一定按三步走: 1. 全局变量声明 2. 函数声明 3. 函数调用 正确做法: var dataStr = null; function remoteCallback(data) { dataS ...

  9. 使用Lucene.NET实现简单的站内搜索

    使用Lucene.NET实现简单的站内搜索 导入Lucene.NET 开发包 Lucene 是apache软件基金会一个开放源代码的全文检索引擎工具包,是一个全文检索引擎的架构,提供了完整的查询引擎和 ...

  10. oracle10g 统计信息查看、收集

      1. 统计信息查看 1.1 单个表的全局统计信息.统计效果查看 2. 统计信息分析(收集) 2.1 分析工具选择 2.2 分析前做index重建 2.3 分析某数据表,可以在PL/SQL的comm ...