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

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

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

#include<cstdio>
#include<cstring>
#include<queue>
const int INF=999999;
const int MAXN=520;
const int MAXM=2520;
using namespace std;
struct edge
{
int to;
int val;
int next;
}e[MAXM]; struct node
{
int from;
int val;
node(int f,int v){from=f;val=v;}
bool operator < (const node& b)const
{
return val > b.val;
}
}; int head[MAXN],len;
int dis_s[MAXN],dis_r[MAXN],path_s[MAXN],path_r[MAXN]; void add(int from,int to,int val)
{
e[len].to=to;
e[len].val=val;
e[len].next=head[from];
head[from]=len++;
} int n,s,en;
void dijkstra(int start,int dis[],int path[])
{
bool vis[MAXN]={0};
dis[start]=0; priority_queue<node> q;
q.push(node(start,0));
int num=0;
while(!q.empty())
{
node temp=q.top();
q.pop(); if(vis[temp.from])
continue; vis[temp.from]=true; if(num==n)
break;
for(int i=head[temp.from];i!=-1;i= e[i].next)
{
if( !vis[e[i].to] &&
dis[temp.from] + e[i].val < dis[e[i].to])
{
dis[e[i].to]=dis[temp.from] + e[i].val ;
path[e[i].to] = temp.from;
q.push(node(e[i].to,dis[e[i].to]));
}
}
}
} void print(int cur)
{
if(path_s[cur]!=-1)
print(path_s[cur]); if(cur!=en)
printf("%d ",cur);
else
printf("%d\n",cur);
}
int main()
{
bool not_first=false;
while(~scanf("%d%d%d",&n,&s,&en))
{
if(not_first)
printf("\n");
not_first=true; len=0;
for(int i=1;i<=n;i++)
{
dis_s[i]=dis_r[i]=INF;
head[i]=path_r[i]=path_s[i]=-1;
} int m;
scanf("%d",&m);
for(int i=0;i<m;i++)
{
int from,to,val;
scanf("%d%d%d",&from,&to,&val);
add(from,to,val);
add(to,from,val);
}
dijkstra(s,dis_s,path_s);
dijkstra(en,dis_r,path_r); int k;
scanf("%d",&k);
int ans_from=-1,ans_to,ans_val,ans=dis_s[en]; for(int i=0;i<k;i++)
{
int from,to,val,temp;
scanf("%d%d%d",&from , &to , &val );
temp=dis_s[from] + dis_r[to] + val;
if(temp < ans)
{
ans=temp;
ans_from=from;
ans_to=to;
ans_val=val;
}
temp=dis_s[to] + dis_r[from] + val;
if(temp < ans)
{
ans=temp;
ans_from=to;
ans_to=from;
ans_val=val;
}
}
if(ans_from==-1)
print(en);
else
{
print(ans_from);
int cur=ans_to;
while(cur!=en)
{
printf("%d ",cur);
cur=path_r[cur];
}
printf("%d\n",en);
}
if(ans_from==-1)
printf("Ticket Not Used\n");
else
printf("%d\n",ans_from);
printf("%d\n",ans); } return 0;
}

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

#include<cstdio>
#include<cstring>
#include<queue>
const int INF=999999;
const int MAXN=520;
const int MAXM=2520;
using namespace std;
struct edge
{
int to;
int val;
int next;
}e[MAXM]; int head[MAXN],len;
int dis_s[MAXN],dis_r[MAXN],path_s[MAXN],path_r[MAXN]; void add(int from,int to,int val)
{
e[len].to=to;
e[len].val=val;
e[len].next=head[from];
head[from]=len++;
} int n,s,en;
void SPFA(int start,int dis[],int path[])
{
bool vis[MAXN]={0};
dis[start]=0; deque<int> q;
q.push_back(start);
vis[start]=true;
while(!q.empty())
{
int cur=q.front();
q.pop_front();
vis[cur]=false;
for(int i=head[cur];i!=-1;i=e[i].next)
{
int id=e[i].to;
if( dis[cur] + e[i].val < dis[id] )
{
path[id]=cur;
dis[id]=dis[cur] + e[i].val;
if(!vis[id])
{
if(!q.empty() && dis[id] <dis[q.front()] )
q.push_front(id);
else
q.push_back(id);
vis[id]=true;
}
}
}
}
}
void print(int cur)
{
if(path_s[cur]!=-1)
print(path_s[cur]); if(cur!=en)
printf("%d ",cur);
else
printf("%d\n",cur);
}
int main()
{
bool not_first=false;
while(~scanf("%d%d%d",&n,&s,&en))
{
if(not_first)
printf("\n");
not_first=true; len=0;
for(int i=1;i<=n;i++)
{
dis_s[i]=dis_r[i]=INF;
head[i]=path_r[i]=path_s[i]=-1;
} int m;
scanf("%d",&m);
for(int i=0;i<m;i++)
{
int from,to,val;
scanf("%d%d%d",&from,&to,&val);
add(from,to,val);
add(to,from,val);
}
SPFA(s,dis_s,path_s);
SPFA(en,dis_r,path_r); int k;
scanf("%d",&k);
int ans_from=-1,ans_to,ans_val,ans=dis_s[en]; for(int i=0;i<k;i++)
{
int from,to,val,temp;
scanf("%d%d%d",&from , &to , &val );
temp=dis_s[from] + dis_r[to] + val;
if(temp < ans)
{
ans=temp;
ans_from=from;
ans_to=to;
ans_val=val;
}
temp=dis_s[to] + dis_r[from] + val;
if(temp < ans)
{
ans=temp;
ans_from=to;
ans_to=from;
ans_val=val;
}
}
if(ans_from==-1)
print(en);
else
{
print(ans_from);
int cur=ans_to;
while(cur!=en)
{
printf("%d ",cur);
cur=path_r[cur];
}
printf("%d\n",en);
}
if(ans_from==-1)
printf("Ticket Not Used\n");
else
printf("%d\n",ans_from);
printf("%d\n",ans); } return 0;
}

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. 兼容MIUI5和MIUI6的开启悬浮窗设置界面

    前一段时间项目中需要对MIUI的悬浮窗开启设置界面进行了引导和跳转,MIUI6中又改变了开启悬浮窗设置的位置,在苦苦寻觅之后,找到了解决的方法,贴出来以方便大家参考和使用. @Override pub ...

  2. SuSe Linux Enterprise Server 10 With Sp2 安装过程图解

    SuSe Linux Enterprise Server 10 With Sp2 安装过程图解 650) this.width=650;" style="border-right- ...

  3. Kinect 开发 —— 深度信息

    转自:http://www.cnblogs.com/yangecnu/archive/2012/04/04/KinectSDK_Depth_Image_Processing_Part1.html 深度 ...

  4. Python-Flask项目开发--为什么需要搭建虚拟环境?

    在使用python开发过程中,需要使用到某些工具包/框架等,需要联网下载.   例如,联网安装Flask框架flask-0.10.1版本:pip install flask==0.10.1   此时, ...

  5. 洛谷 P3385 【模板】负环

    P3385 [模板]负环 题目描述 暴力枚举/SPFA/Bellman-ford/奇怪的贪心/超神搜索 输入输出格式 输入格式: 第一行一个正整数T表示数据组数,对于每组数据: 第一行两个正整数N M ...

  6. C#空值和null判断

    一.空值判断效率 string s = ""; if(s == ""){} if(s == string.Empty){} if (string.IsNullO ...

  7. BZOJ2780: [Spoj]8093 Sevenk Love Oimaster(广义后缀自动机,Parent树,Dfs序)

    Description Oimaster and sevenk love each other. But recently,sevenk heard that a girl named ChuYuXu ...

  8. Scala中的“=>”和“<-”

    “=>”符号大概可以看做是创建函数实例的语法糖,例如 args.foreach(arg => println(arg)) 大概可以看做 args.foreach(Function(arg) ...

  9. 如何在同一台机器上安装多个MySQL的实例(转)

    最近由于工作的需要,需要在同一台机器上搭建两个MySQL的实例,(注:已经存在了一个3306的MySQL的实例). 先说下,什么是mysql的多实例,简单的来说就是一台机器上安装了多个mysql的服务 ...

  10. 《开源公开课分享》:Java开源框架案例分享

        缺乏高端技术人才?缺乏开发标准?    代码复用性低?技术风险难于把控?     招聘成本高?培训成本高?    假设想法不够雄伟,那么就会局限于细节:假设一開始就铺很大的摊子,将会失去控制: ...