zoukankan      html  css  js  c++  java
  • uva 11374 Airport Express(spfa 邻接表+队列)

    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 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
    题意:第一行给出 n个地方,起点和终点;
    第二行 平民航线的个数 接着下面是平民航线的起点、终点和时间;
    剩下的是土豪航线的数量 接着是土豪航线的起点、终点和时间;
    思路:做个假设如果用spfa对每一条土豪航线进行取最短路,那么消耗的时间会非常大,不可取。
    起点和终点已知我们可以用spfa求出起点到土豪航线起点所消耗的时间,也可以求出土豪航线终点到终点的时间,最后再加上土豪航线的时间,就是最后求出的总时间。这样只要使用两次spfa就可 以求出总时间。
    注意:这个道题有好多坑,看注释吧,uva不保存交过的代码,这点有点坑。
    ac代码:
      1 #include<stdio.h>
      2 #include<string.h>
      3 #include<iostream>
      4 #include<queue>
      5 #include<algorithm>
      6 using namespace std;
      7 const int inf=1<<29;                                  
      8 const int maxn=1100;
      9 const int maxm=maxn*maxn;
     10 int e,head[maxn],pnt[maxm],nxt[maxm],cost[maxm],dist[maxn],pre[maxn],dist1[maxn],pre1[maxn];
     11 int n,m,a,b,d[maxm];
     12 int cnt[maxn];
     13 queue<int> q;                                           //这里没有用优先队列,因为数据不大,ac只用了25ms。
     14 bool vis[maxn];
     15 void AddEdge(int u,int v,int c)
     16 {
     17     pnt[e]=v;
     18     nxt[e]=head[u];
     19     cost[e]=c;
     20     head[u]=e++;
     21 }
     22 void Spfa(int st)                                        //计算起点每个到土豪航线的起点的时间
     23 {
     24     memset(dist,0x3f3f3f,sizeof(dist));                 // 初始化dist数组,dist为极大值。
     25     dist[st]=0;
     26     q.push(st);
     27     while(!q.empty())
     28     {
     29         int u=q.front();
     30         q.pop();
     31         vis[u]=0;
     32         for(int i=head[u]; i!=-1; i=nxt[i])
     33             if(dist[pnt[i]]>dist[u]+cost[i])
     34             {
     35                 pre[pnt[i]]=u;
     36                 dist[pnt[i]]=dist[u]+cost[i];
     37                 if(!vis[pnt[i]])
     38                 {
     39                     q.push(pnt[i]);
     40                     vis[pnt[i]]=1;
     41                 }
     42             }
     43     }
     44 }
     45 void Spfa1(int st)                                        //计算每个土豪航线的终点到终点的时间
     46 {
     47     memset(dist1,0x3f3f3f,sizeof(dist1));
     48     dist1[st]=0;
     49     q.push(st);
     50     while(!q.empty())
     51     {
     52         int u=q.front();
     53         q.pop();
     54         vis[u]=0;
     55         for(int i=head[u]; i!=-1; i=nxt[i])
     56             if(dist1[pnt[i]]>dist1[u]+cost[i])
     57             {
     58                 pre1[pnt[i]]=u;
     59                 dist1[pnt[i]]=dist1[u]+cost[i];
     60                 if(!vis[pnt[i]])
     61                 {
     62                     q.push(pnt[i]);
     63                     vis[pnt[i]]=1;
     64                 }
     65             }
     66     }
     67 }
     68 void dfs(int u)                                             //搜索起点到土豪航线的起点路径
     69 {
     70     if(u==a)
     71     {
     72         printf("%d",u);
     73         return;
     74     }
     75     dfs(pre[u]);
     76     printf(" %d",u);
     77 }
     78 void dfs1(int u)                                             //搜索土豪航线的终点到终点的路径
     79 {
     80     printf(" %d",u);
     81     if(u==b)
     82         return;
     83     dfs1(pre1[u]);
     84 }
     85 
     86 int main()
     87 {
     88     int x,c1=0;
     89     while(scanf("%d%d%d",&n,&a,&b)!=EOF)
     90     {
     91         if(c1)printf("
    ");                                     //这里是个坑,不写这句话就wrong,还有最后一句话
     92         e=0;
     93         int flag=0;
     94         scanf("%d",&m);
     95         memset(head,-1,sizeof(head));
     96         memset(pre,-1,sizeof(pre));                            //储存起点到土豪航线起点路径
     97         for(int i=0; i<m; i++)
     98         {
     99             int u,v,c;
    100             scanf("%d%d%d",&u,&v,&c);
    101             AddEdge(u,v,c);
    102             AddEdge(v,u,c);
    103         }
    104         Spfa(a);
    105         int num=dist[b];
    106         memset(pre1,-1,sizeof(pre1));                           //储存土豪航线的终点到终点的路径
    107         Spfa1(b);
    108         scanf("%d",&x);
    109         int k=0,l=0;                                           //k是记录土豪航线的起点,l是记录土豪航线的终点
    110         for(int i=0; i<x; i++)
    111         {
    112             int w,y,z;
    113             scanf("%d%d%d",&w,&y,&z);
    114             if(num>dist[w]+dist1[y]+z)
    115             {
    116                 num=dist[w]+dist1[y]+z;
    117                 k=w;
    118                 l=y;
    119             }
    120             if(num>dist[y]+dist1[w]+z)                         //因为是无向图,两个方向都要计算
    121             {
    122                 num=dist[y]+dist1[w]+z;
    123                 k=y;
    124                 l=w;
    125             }
    126         }
    127         if(k==0)
    128         {
    129             dfs(b);
    130             printf("
    Ticket Not Used
    %d
    ",num);
    131         }
    132         else
    133         {
    134             dfs(k);
    135             dfs1(l);
    136             printf("
    %d
    %d
    ",k,num);
    137         }
    138         c1=1;                                                  //这句很重要
    139     }
    140 }


  • 相关阅读:
    Word如何去水印
    计算机二级公共基础知识 #02
    计算机二级公共基础知识 #01
    Linux常用快捷键
    计算机二级C语言概述 #00
    信管专业的同学都进来看一看叭~~~~~
    Python--Hanoi塔问题
    MATLAB——实验一:查看图像的RGB值
    Python课 #06号作业
    Python课 #05号作业
  • 原文地址:https://www.cnblogs.com/Xacm/p/3937463.html
Copyright © 2011-2022 走看看