UVA 11374 dijkstra预处理+枚举

  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 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.

  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



输出格式:

  对于每组数据,输出3行。第一行表示按访问顺序给出经过的各个车站(包括起点和终点),第二行是换乘商业线的车站编号,(如果没有商业险车票,输出Ticket Not Used),第三行输出总时间

分析:

  商用站只能坐一站,枚举出所有的可能

  比如商用站从a到b,那么从起点到a,从a到终点两条路线都应该是最短的,所以我们只需从七点开始,到终点结束做两条单源最短路,记录从起点开始到每个节点的x的最短时间f(x)和从每个点到终点的最短时间g(x),则总时间为f(a)+T(a,b)+f(b),

其中T(a,b)表示a到b商用路线所用的时间



#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <string>
#include <algorithm>

const int inf = 0x3f3f3f;
const int MAXN = 5e2+10;
const int MAXNN = 1e3+10;
struct edge{
    int st;
    int to;
    int dist;
    int next;
};
struct heapnode{    //优先队列节点
    int st;
    int dist;
    bool operator < (const heapnode& rhs)const {
        return dist>rhs.dist;
    }
};
using namespace std;
int first[MAXN];
edge e[2*MAXNN];
int top;
int d1[MAXN];   //->S
int d2[MAXN];   //->E
int p1[2*MAXN];
int p2[2*MAXN];
int done[MAXN];
int n,m,k;
int cs,ce;
 int na,nb;

void init(){
    memset(first,-1,sizeof(first));
    top = 0;
}

void addedge(int u,int v,int dist){
    e[top].st = u;
    e[top].to = v;
    e[top].dist = dist;
    e[top].next = first[u];
    first[u] = top++;
}

void dijkstra_s(int s){
    priority_queue<heapnode>Q;
    heapnode a,tmp;
    memset(done,0,sizeof(done));
    for(int i=0;i<n;i++){
        d1[i] = inf;
    }
    d1[s] = 0;
    a.st = s;
    a.dist = 0;
    Q.push(a);
    int u;
    while(!Q.empty()){
        tmp = Q.top();
        Q.pop();
        u = tmp.st;
        if(done[u])continue;
        done[u] = 1;
        for(int i=first[u];i!=-1;i=e[i].next){
            if(d1[e[i].to]>d1[u]+e[i].dist){
                d1[e[i].to] = d1[u]+e[i].dist;
                p1[e[i].to] = i;
                a.st = e[i].to;
                a.dist = d1[e[i].to];
                Q.push(a);
            }
        }
    }
}

void dijkstra_e(int s){
    priority_queue<heapnode>Q;
    heapnode a,tmp;
    memset(done,0,sizeof(done));
    for(int i=0;i<n;i++){
        d2[i] = inf;
    }
    d2[s] = 0;
    a.st = s;
    a.dist = 0;
    Q.push(a);
    int u;
    while(!Q.empty()){
        tmp = Q.top();
        Q.pop();
        u = tmp.st;
        if(done[u])continue;
        done[u] = 1;
        for(int i=first[u];i!=-1;i=e[i].next){
            if(d2[e[i].to]>d2[u]+e[i].dist){
                d2[e[i].to] = d2[u]+e[i].dist;
                p2[e[i].to] = i;
                a.st = e[i].to;
                a.dist = d2[e[i].to];
                Q.push(a);
            }
        }
    }
}

void printA(int u){
    if(u==cs){
        cout<<u+1;
        return;
    }
    printA(e[p1[u]].st);
    cout<<" "<<u+1;
}

void printB(int u){
    if(u==ce){
        cout<<" "<<u+1<<endl;
        return ;
    }
    cout<<" "<<u+1;
    printB(e[p2[u]].st);
}

int main()
{
    int a,b,td;
    int mc= 0;
    while(scanf("%d%d%d",&n,&cs,&ce)!=EOF){
        if(mc++)cout<<endl;
        init();
        scanf("%d",&m);
        for(int i=0;i<m;i++){
            scanf("%d%d%d",&a,&b,&td);
            a--;
            b--;
            addedge(a,b,td);
            addedge(b,a,td);
        }
        cs--;
        ce--;
        dijkstra_s(cs);
        dijkstra_e(ce);
        int ans = d1[ce];
        na = nb = -1;
        /*cout<<"****"<<endl;
        cout<<ans<<endl;
        cout<<d2[cs]<<endl;
        cout<<"****"<<endl;*/
        scanf("%d",&k);
        for(int i=0;i<k;i++){
            scanf("%d%d%d",&a,&b,&td);
            a--;
            b--;
            if(d1[a]+d2[b]+td<ans){
                na = a;
                nb = b;
                ans = d1[a]+d2[b]+td;
            }
            if(d1[b]+d2[a]+td<ans){
                na = b;
                nb = a;
                ans = d1[b]+d2[a]+td;
            }
        }

        if(na==-1){
            printA(ce);
            cout<<endl;
        }else{
            printA(na);
            printB(nb);
        }

        if(ans!=d1[ce]){
            cout<<na+1<<endl;
        }else{
            cout<<"Ticket Not Used"<<endl;
        }
        cout<<ans<<endl;
        //cout<<endl;
    }

    //cout << "Hello world!" << endl;
    return 0;
}

时间: 2024-08-02 14:03:48

UVA 11374 dijkstra预处理+枚举的相关文章

UVA - 11374 Airport Express (Dijkstra模板+枚举)

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-Xpress and the Comm

Airport Express UVA - 11374(dijkstra)

Airport Express UVA - 11374 题意:n个点,有m条普通路径,k条高速路径,但是k条只能选一条走.问从s到e最短时间. 如果选a-->b这条高速,那么s-->a和b--->e必然也要是最短路. 于是我们可以先用两次dijkstra预处理出s到各点的最短路和e到各点的最短路,然后枚举k条高速走哪条. 输出路径的时候,可以递归~ 1 #include <bits/stdc++.h> 2 using namespace std; 3 const int in

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

layout: post title: 训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板) author: "luowentaoaa" catalog: true mathjax: true tags: - 最短路 - Dijkstra - 图论 - 训练指南 Airport Express UVA - 11374 题意 机场快线有经济线和商业线,现在分别给出经济线和商业线的的路线,现在只能坐一站商业线,其他坐经济线,问从起点到终点的最短用时是多少,还有

uva 11374 最短路+记录路径 dijkstra最短路模板

UVA - 11374 Airport Express Time Limit:1000MS   Memory Limit:Unknown   64bit IO Format:%lld & %llu [Submit]  [Go Back]  [Status] Description ProblemD: Airport Express In a small city called Iokh, a train service, Airport-Express, takes residents to t

UVA 11374 Airport Express 机场快线 Dijistra+路径

题目链接:UVA 11374 Airport Express Airport Express Time Limit: 1000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu [Submit]   [Go Back]   [Status] Description Problem D: Airport Express In a small city called Iokh, a train service, Airport-Expr

UVA 11374 - Airport Express(dijstra)

UVA 11374 - Airport Express 题目链接 题意:给定一些经济线,和一些商务线,商务线最多坐一次,每个线有一个时间,问最短时间 思路:从起点,终点各做一次dijstra,然后枚举商务线,就可以算出总时间,最后求出总时间最少 代码: #include <cstdio> #include <cstring> #include <vector> #include <queue> using namespace std; #define INF

uva 11374

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 the Commercial-Xpres

uva 11374 Airport Express(最短路)

uva 11374 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 the Commercial-Xpress

uva 10806 Dijkstra, Dijkstra. (最小费最大流)

uva 10806 Dijkstra, Dijkstra. 题目大意:你和你的伙伴想要越狱.你的伙伴先去探路,等你的伙伴到火车站后,他会打电话给你(电话是藏在蛋糕里带进来的),然后你就能够跑去火车站了,那里有人接应你. 可是.由于你的伙伴跑去火车站的时候穿的是囚服,所以,他经过的街道都被戒严了,你必须从其它街道跑过去. 假设你能够到达火车站,请输出你和你的伙伴在路上花费的最短时间,假设不能请"Back to jail". 解题思路:最小费最大流.设置一个超级源点连向监狱(起点1), 容