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

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-Xpressand 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 NS and E (2 ≤ N ≤ 500, 1 ≤ SE ≤ 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 XY and Z (XY ≤ 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

  1. 4 1 4
  2. 4
  3. 1 2 2
  4. 1 3 3
  5. 2 4 4
  6. 3 4 5
  7. 1
  8. 2 4 3

Sample Output

  1. 1 2 4
  2. 2
  3. 5

Problemsetter: Raymond Chun

Originally appeared in CXPC, Feb. 2004


题目大意:

机场快线分为经济线和商业线。两种路线价格、路线、速度不同。给你初始地点和目标地点,还有所有的经济线和商业线,要你求出从到目标地点最快的路线,这条路线有一个要求就是最多坐一条商业线,当然也可以不做,速度最快就好。

要求输出所经过的路径、在哪个站点使用商业线、以及总的时间。

思路:

我们可以先调用两次dijkstra或者两次SPFA,求出起点和终点到所有点的最短路径,然后商业线一一枚举。

example:

INPUT:

5 5 1

3

1 2 4

2 3 4

3 4 4

5

1 2 2

2 3 2

1 3 2

2 4 2

4 5 10

OUT:

5 4 3 2 1

5

22

判断商业线的时候,要特别注意。看下面的代码,要判断双向的。我一开始一直WA就是这样。。

dijkstra版本:0.019s

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<queue>
  4. const int INF=999999;
  5. const int MAXN=520;
  6. const int MAXM=2520;
  7. using namespace std;
  8. struct edge
  9. {
  10. int to;
  11. int val;
  12. int next;
  13. }e[MAXM];
  14.  
  15. struct node
  16. {
  17. int from;
  18. int val;
  19. node(int f,int v){from=f;val=v;}
  20. bool operator < (const node& b)const
  21. {
  22. return val > b.val;
  23. }
  24. };
  25.  
  26. int head[MAXN],len;
  27. int dis_s[MAXN],dis_r[MAXN],path_s[MAXN],path_r[MAXN];
  28.  
  29. void add(int from,int to,int val)
  30. {
  31. e[len].to=to;
  32. e[len].val=val;
  33. e[len].next=head[from];
  34. head[from]=len++;
  35. }
  36.  
  37. int n,s,en;
  38. void dijkstra(int start,int dis[],int path[])
  39. {
  40. bool vis[MAXN]={0};
  41. dis[start]=0;
  42.  
  43. priority_queue<node> q;
  44. q.push(node(start,0));
  45. int num=0;
  46. while(!q.empty())
  47. {
  48. node temp=q.top();
  49. q.pop();
  50.  
  51. if(vis[temp.from])
  52. continue;
  53.  
  54. vis[temp.from]=true;
  55.  
  56. if(num==n)
  57. break;
  58. for(int i=head[temp.from];i!=-1;i= e[i].next)
  59. {
  60. if( !vis[e[i].to] &&
  61. dis[temp.from] + e[i].val < dis[e[i].to])
  62. {
  63. dis[e[i].to]=dis[temp.from] + e[i].val ;
  64. path[e[i].to] = temp.from;
  65. q.push(node(e[i].to,dis[e[i].to]));
  66. }
  67. }
  68. }
  69. }
  70.  
  71. void print(int cur)
  72. {
  73. if(path_s[cur]!=-1)
  74. print(path_s[cur]);
  75.  
  76. if(cur!=en)
  77. printf("%d ",cur);
  78. else
  79. printf("%d\n",cur);
  80. }
  81. int main()
  82. {
  83. bool not_first=false;
  84. while(~scanf("%d%d%d",&n,&s,&en))
  85. {
  86. if(not_first)
  87. printf("\n");
  88. not_first=true;
  89.  
  90. len=0;
  91. for(int i=1;i<=n;i++)
  92. {
  93. dis_s[i]=dis_r[i]=INF;
  94. head[i]=path_r[i]=path_s[i]=-1;
  95. }
  96.  
  97. int m;
  98. scanf("%d",&m);
  99. for(int i=0;i<m;i++)
  100. {
  101. int from,to,val;
  102. scanf("%d%d%d",&from,&to,&val);
  103. add(from,to,val);
  104. add(to,from,val);
  105. }
  106. dijkstra(s,dis_s,path_s);
  107. dijkstra(en,dis_r,path_r);
  108.  
  109. int k;
  110. scanf("%d",&k);
  111. int ans_from=-1,ans_to,ans_val,ans=dis_s[en];
  112.  
  113. for(int i=0;i<k;i++)
  114. {
  115. int from,to,val,temp;
  116. scanf("%d%d%d",&from , &to , &val );
  117. temp=dis_s[from] + dis_r[to] + val;
  118. if(temp < ans)
  119. {
  120. ans=temp;
  121. ans_from=from;
  122. ans_to=to;
  123. ans_val=val;
  124. }
  125. temp=dis_s[to] + dis_r[from] + val;
  126. if(temp < ans)
  127. {
  128. ans=temp;
  129. ans_from=to;
  130. ans_to=from;
  131. ans_val=val;
  132. }
  133. }
  134. if(ans_from==-1)
  135. print(en);
  136. else
  137. {
  138. print(ans_from);
  139. int cur=ans_to;
  140. while(cur!=en)
  141. {
  142. printf("%d ",cur);
  143. cur=path_r[cur];
  144. }
  145. printf("%d\n",en);
  146. }
  147. if(ans_from==-1)
  148. printf("Ticket Not Used\n");
  149. else
  150. printf("%d\n",ans_from);
  151. printf("%d\n",ans);
  152.  
  153. }
  154.  
  155. return 0;
  156. }

采用SLF优化的SPFA版本:0.015s

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<queue>
  4. const int INF=999999;
  5. const int MAXN=520;
  6. const int MAXM=2520;
  7. using namespace std;
  8. struct edge
  9. {
  10. int to;
  11. int val;
  12. int next;
  13. }e[MAXM];
  14.  
  15. int head[MAXN],len;
  16. int dis_s[MAXN],dis_r[MAXN],path_s[MAXN],path_r[MAXN];
  17.  
  18. void add(int from,int to,int val)
  19. {
  20. e[len].to=to;
  21. e[len].val=val;
  22. e[len].next=head[from];
  23. head[from]=len++;
  24. }
  25.  
  26. int n,s,en;
  27. void SPFA(int start,int dis[],int path[])
  28. {
  29. bool vis[MAXN]={0};
  30. dis[start]=0;
  31.  
  32. deque<int> q;
  33. q.push_back(start);
  34. vis[start]=true;
  35. while(!q.empty())
  36. {
  37. int cur=q.front();
  38. q.pop_front();
  39. vis[cur]=false;
  40. for(int i=head[cur];i!=-1;i=e[i].next)
  41. {
  42. int id=e[i].to;
  43. if( dis[cur] + e[i].val < dis[id] )
  44. {
  45. path[id]=cur;
  46. dis[id]=dis[cur] + e[i].val;
  47. if(!vis[id])
  48. {
  49. if(!q.empty() && dis[id] <dis[q.front()] )
  50. q.push_front(id);
  51. else
  52. q.push_back(id);
  53. vis[id]=true;
  54. }
  55. }
  56. }
  57. }
  58. }
  59. void print(int cur)
  60. {
  61. if(path_s[cur]!=-1)
  62. print(path_s[cur]);
  63.  
  64. if(cur!=en)
  65. printf("%d ",cur);
  66. else
  67. printf("%d\n",cur);
  68. }
  69. int main()
  70. {
  71. bool not_first=false;
  72. while(~scanf("%d%d%d",&n,&s,&en))
  73. {
  74. if(not_first)
  75. printf("\n");
  76. not_first=true;
  77.  
  78. len=0;
  79. for(int i=1;i<=n;i++)
  80. {
  81. dis_s[i]=dis_r[i]=INF;
  82. head[i]=path_r[i]=path_s[i]=-1;
  83. }
  84.  
  85. int m;
  86. scanf("%d",&m);
  87. for(int i=0;i<m;i++)
  88. {
  89. int from,to,val;
  90. scanf("%d%d%d",&from,&to,&val);
  91. add(from,to,val);
  92. add(to,from,val);
  93. }
  94. SPFA(s,dis_s,path_s);
  95. SPFA(en,dis_r,path_r);
  96.  
  97. int k;
  98. scanf("%d",&k);
  99. int ans_from=-1,ans_to,ans_val,ans=dis_s[en];
  100.  
  101. for(int i=0;i<k;i++)
  102. {
  103. int from,to,val,temp;
  104. scanf("%d%d%d",&from , &to , &val );
  105. temp=dis_s[from] + dis_r[to] + val;
  106. if(temp < ans)
  107. {
  108. ans=temp;
  109. ans_from=from;
  110. ans_to=to;
  111. ans_val=val;
  112. }
  113. temp=dis_s[to] + dis_r[from] + val;
  114. if(temp < ans)
  115. {
  116. ans=temp;
  117. ans_from=to;
  118. ans_to=from;
  119. ans_val=val;
  120. }
  121. }
  122. if(ans_from==-1)
  123. print(en);
  124. else
  125. {
  126. print(ans_from);
  127. int cur=ans_to;
  128. while(cur!=en)
  129. {
  130. printf("%d ",cur);
  131. cur=path_r[cur];
  132. }
  133. printf("%d\n",en);
  134. }
  135. if(ans_from==-1)
  136. printf("Ticket Not Used\n");
  137. else
  138. printf("%d\n",ans_from);
  139. printf("%d\n",ans);
  140.  
  141. }
  142.  
  143. return 0;
  144. }

UVA 11374 Airport Express SPFA||dijkstra的更多相关文章

  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 (Dijkstra模板+枚举)

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

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

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

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

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

  5. UVA 11374 Airport Express(最短路)

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

  6. UVA 11374 Airport Express (最短路)

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

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

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

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

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

  9. UVA-11374 Airport Express (dijkstra+枚举)

    题目大意:n个点,m条无向边,边权值为正,有k条特殊无向边,起止点和权值已知,求从起点到终点的边权值最小的路径,特殊边最多只能走一条. 题目分析:用两次dijkstra求出起点到任何一个点的最小权值, ...

随机推荐

  1. POJ 3175 枚举

    思路: 枚举小数点前 的数是啥 判一判 复杂度是根号的-.. 注意精度!!!! //By SiriusRen #include <cmath> #include <cstdio> ...

  2. MyBatis映射

    mybatis-config.xml映射文件 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ...

  3. 【深入篇】Android常用布局方式简介

    LinearLayout 线性布局是程序中最常见的布局方式.一般分为水平线性布局和竖直线性布局,通过android.orientation属性可以设置线性布局的方向. 在布局中操作颜色时,要用的是十六 ...

  4. RGB-D action recognition using linear coding

    First, a depth spatial-temporal descriptor is developed to extract the interested local regions in d ...

  5. 2017国家集训队作业[agc014d]Black and White Tree

    2017国家集训队作业[agc014d]Black and White Tree 题意: ​ 有一颗n个点的树,刚开始每个点都没有颜色.Alice和Bob会轮流对这棵树的一个点涂色,Alice涂白,B ...

  6. WebService 获取客户端 IP 和 MAC 等信息

    IP地址 public string getClientIP() { string result = HttpContext.Current.Request.ServerVariables[" ...

  7. gpasswd---指定要管理的工作组,及更改密码

    gpasswd 命令详解 gpasswd命令是Linux下工作组文件/etc/group和/etc/gshadow的管理工具,用于指定要管理的工作组. 2.选项详解: -a : 添加用户到组 -d : ...

  8. C++中的纯虚函数

    ---恢复内容开始--- 在C++中的一种函数申明被称之为:纯虚函数(pure virtual function).它的申明格式如下 class CShape { public: ; }; 在什么情况 ...

  9. mycat 不得不说的缘分(转)

    ,尾声,左兄与任正非.leader-us与马云 新成立的公司里面,有个左兄,很传奇,大一在大学入伍,然后复员专业,来上海学IT,年纪轻轻,睡在地铁站,苦心专研数据库.系统.中间件,现在已经成为了业界大 ...

  10. 7lession-基础数据使用介绍

    1.数值 这个使用比较简单 a = 1 b = 3.2 c = 12.5+4j d = 20L 2.字符串 代码 s = "hello world,i am comming" pr ...