uva 11374 最短路+记录路径 好题 dijkstra优先队列优化算法 邻接表法 可做模板 G++提交

来源:互联网 发布:大学图书馆数据库 编辑:程序博客网 时间:2024/06/02 22:57
UVA - 11374
Airport Express
Time Limit:1000MS Memory Limit:Unknown 64bit IO Format:%lld & %llu

[Submit]  [Go Back]  [Status]  

Description

Download as PDF

ProblemD: 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, theEconomy-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 andE (2 ≤N ≤ 500, 1 ≤S,EN), 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 nextM lines give the information of the routes of the Economy-Xpress. Each consists of three integersX, Y and Z (X,YN, 1 ≤Z ≤ 100). This meansX andY are connected and it takesZ 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 nextK 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 "TicketNot 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 thetotal time for the journey on the last line. Consecutive sets of output must be separated by a blank line.

Sample Input

4 1 441 2 21 3 32 4 43 4 512 4 3

Sample Output

1 2 425

Problemsetter: Raymond Chun
Originally appeared in CXPC, Feb. 2004

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=22966


题意:

在Iokh市中,机场快线是市民从市内去机场的首选交通工具。机场快线分为经济线和商业线两种,线路,速度和价钱都不同。你有一张商业线车票,可以做一站商业线,而其他时候只能乘坐经济线。假设换乘时间忽略不计,你的任务是找一条去机场最快的路线。。

分析:枚举商业线T(a,b),则总时间为f(a)+T(a,b)+g(b);f和g用两次dijkstra来计算,以S为起点的dijkstra和以E为起点的dijkstra;

注意下面的模板是从0开始的

#include<stdio.h>#include<queue>#include<algorithm>#include<vector>#include<string.h>using namespace std;const int INF=10000000;const int maxn=500+5;struct Edge{    int from,to,dis;    };struct node{    int d,u;    bool operator <(const node &rhs) const {    return d>rhs.d;//从小到大排列    }};struct Dij{    int n,m;//点数和边数    vector<Edge>edges;//边列表    vector<int>nd[maxn];//从每个节点出发的边编号 从0开始    bool vis[maxn];//是否参观过    int mmin[maxn];//s到各个点的最小距离    int pre[maxn];//最短路当前点的前一个点    void init(int n)    {        this->n=n;        for(int i=0;i<n;i++)  nd[i].clear();//邻接表清空        edges.clear();//清空边表    }    void addedge(int from,int to,int dis)    //如果是无向图 每条无向边需要调用2次    {         edges.push_back((Edge){from,to,dis});//是{ 不是(         m=edges.size();         nd[from].push_back(m-1);    }    void dij(int s)//求s到所有点的距离    {        priority_queue<node>que;        for(int i=0;i<n;i++) mmin[i]=INF;        mmin[s]=0;        memset(vis,0,sizeof(vis));        que.push((node){0,s});//注意符号 不是括号        while(!que.empty())        {            node x=que.top();que.pop();            int u=x.u;            if(vis[u]) continue;            vis[u]=true;            for(int i=0;i<nd[u].size();i++)            {                Edge& e=edges[nd[u][i]];                if(mmin[e.to]>mmin[u]+e.dis)                 {                   mmin[e.to]=mmin[u]+e.dis;                   pre[e.to]=e.from;                   que.push((node){mmin[e.to],e.to});                 }            }        }    }    void get_path(vector<int>&path,int s,int e)    {        int pos=e;        while(1)        {        path.push_back(pos);        if(pos==s) return ;        pos=pre[pos];        }    }};vector<int>path;int main(){    int i,j,k,n,m,cas,x,y,z,s,e;    while(scanf("%d %d %d",&n,&s,&e)!=EOF)    {        s--;e--;//从0开始的         Dij solve[2];            if(cas++!=0) printf("\n");            solve[0].init(n);            solve[1].init(n);            scanf("%d",&m);           while(m--)           {                scanf("%d %d %d",&x,&y,&z);                x--;y--;                solve[0].addedge(x,y,z);solve[0].addedge(y,x,z);                solve[1].addedge(x,y,z);solve[1].addedge(y,x,z);           }           solve[0].dij(s);           solve[1].dij(e);           scanf("%d",&m);          path.clear();           int ans=solve[0].mmin[e],xx=-1,yy=-1;           while(m--)           {               scanf("%d %d %d",&x,&y,&z);               x--;y--;               if(ans>solve[0].mmin[x]+z+solve[1].mmin[y])               {                   ans=solve[0].mmin[x]+z+solve[1].mmin[y];                   xx=x;yy=y;               }               if(ans>solve[1].mmin[x]+z+solve[0].mmin[y])               {                   ans=solve[1].mmin[x]+z+solve[0].mmin[y];                   xx=y;yy=x;               }           }           if(xx==-1)           {                solve[0].get_path(path,s,e);                reverse(path.begin(),path.end());                for(i=0;i<path.size()-1;i++) printf("%d ",path[i]+1);                printf("%d\n",path[i]+1);                printf("Ticket Not Used\n");                printf("%d\n",ans);           }           else           {                solve[0].get_path(path,s,xx);                reverse(path.begin(),path.end());                solve[1].get_path(path,e,yy);                for(i=0;i<path.size()-1;i++) printf("%d ",path[i]+1);                printf("%d\n",path[i]+1);                printf("%d\n",xx+1);                printf("%d\n",ans);           }     }     return 0;}


此题保留下来给自己看




原创粉丝点击