博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Airport Express UVA - 11374
阅读量:6566 次
发布时间:2019-06-24

本文共 4481 字,大约阅读时间需要 14 分钟。

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
#include
#include
#include
#include
#include
#define ll long long#define MAXN 3000using namespace std;int dis[2][MAXN],pre[2][MAXN],have[MAXN],hh[MAXN];int n,m,k,s,t,num=0;queue
q;struct edge{ int first; int next; int to; int quan;}a[MAXN*2];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=1;i<=n;i++) have[i]=pre[i]=0,dis[i]=1<<30; while(!q.empty()) q.pop(); dis[s]=0;q.push(s); while(!q.empty()){ int now=q.front(); q.pop(); have[now]=0; 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]=1; q.push(to); } } } }}void print(int ss){ memset(hh,0,sizeof(hh));int numm=0; for(int now=ss;pre[0][now]!=0;now=pre[0][now]){ hh[++numm]=now; } hh[++numm]=s; for(int i=numm;i>=2;i--) printf("%d ",hh[i]);printf("%d",hh[1]); }int main(){ int Case=0; while(scanf("%d%d%d",&n,&s,&t)!=EOF){ if(Case++) printf("\n"); memset(a,0,sizeof(a)),num=0; scanf("%d",&m); for(int i=1;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[0],pre[0]),spfa(t,dis[1],pre[1]); scanf("%d",&k); int ans=dis[0][t],from=0,to=0; for(int i=1;i<=k;i++){ int x,y,z;scanf("%d%d%d",&x,&y,&z); if((ll)ans>(ll)dis[0][x]+(ll)z+(ll)dis[1][y]){ ans=dis[0][x]+z+dis[1][y],from=x,to=y; } if((ll)ans>(ll)dis[0][y]+(ll)z+(ll)dis[1][x]){ ans=dis[0][y]+z+dis[1][x],from=y,to=x; } } if(!from){ print(t); printf("\nTicket Not Used\n"); printf("%d\n",dis[0][t]); } else{ print(from);printf(" "); for(int now=to;pre[1][now]!=0;now=pre[1][now]){ printf("%d ",now); } printf("%d",t); printf("\n%d\n",from); printf("%d\n",ans); } }}

 

转载于:https://www.cnblogs.com/renjianshige/p/7467699.html

你可能感兴趣的文章
luaCPU性能测试
查看>>
mysql优化
查看>>
【批处理】for循环中产生不同的随机数
查看>>
Gradle -help
查看>>
/etc/security/limits.conf
查看>>
js 框架
查看>>
android 实现ListView中添加RaidoButton单选
查看>>
WS-Security 中文问题&Stax(Streaming API for XML) (二)
查看>>
dos 分页显示及查看应用程序占用端口
查看>>
Oracle数据库:启动操作
查看>>
linux下的防火墙
查看>>
SNAT与DNAT
查看>>
Linux 修改密码“ Authentication token manipulation err”
查看>>
openstack
查看>>
Lync Server 2013 安装体验(一)
查看>>
Hadoop2.6.0学习笔记(五)自定义InputFormat和RecordReader
查看>>
EBB-24、DNS2
查看>>
css3做的nav
查看>>
汇编笔记
查看>>
在线枚举内核模块函数及地址(win64位)
查看>>