UVA 11374 Airport Express

很常见的思路,预处理出S和T的两个单源最短路,然后枚举商业线。路径输出可以在求最短路的同时保存pa数组,也可以用dist数组检查。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 502, maxm = 2002;

int head[maxn], to[maxm], nxt[maxm],wei[maxm],ecnt;

void addEdge(int u,int v,int w)
{
    to[ecnt] = v;
    nxt[ecnt] = head[u];
    wei[ecnt] = w;
    head[u] = ecnt++;
}

typedef pair<int,int> Node;
#define fi first
#define se second
const int INF = 0x3f3f3f3f;
int d1[maxn],d2[maxn];

void dijkstra(int s,int t,int (&d)[maxn])
{
    priority_queue<Node,vector<Node>,greater<Node> > q;
    memset(d,0x3f,sizeof(d));
    d[s] = 0; q.push(Node(0,s));
    while(q.size()){
        Node x = q.top(); q.pop();
        int u = x.se;
        if(x.fi != d[u]) continue;
        for(int i = head[u]; ~i; i = nxt[i]){
            int v = to[i];
            if(d[v] > d[u]+wei[i]){
                d[v] = d[u]+wei[i];
                q.push(Node(d[v],v));
            }
        }
    }
}

int main()
{
    //freopen("in.txt","r",stdin);
    int N,S,E;
    bool first = true;
    while(~scanf("%d%d%d",&N,&S,&E)){
        if(!first) putchar(‘\n‘);
        first = false;
        int M; scanf("%d",&M);
        memset(head,-1,sizeof(head));
        ecnt = 0;
        while(M--){
            int u,v,w; scanf("%d%d%d",&u,&v,&w);
            addEdge(--u,--v,w); addEdge(v,u,w);
        }
        dijkstra(--S,--E,d1);
        dijkstra(E,S,d2);

        int pick = -1, p2, ans = d1[E];
        int K; scanf("%d",&K);
        for(int i = 0; i < K; i++){
            int u,v,w; scanf("%d%d%d",&u,&v,&w);
            if(d1[--u] + w  < ans - d2[--v]){
                ans = d1[u] + w + d2[v];
                pick = u; p2 = v;
            }else if( d2[u] + w  < ans - d1[v]){
                ans = d2[u] + w + d1[v];
                pick = v; p2 = u;
            }
        }
        int u;
        if(~pick){
            stack<int> stk;
            u = pick;
            stk.push(u);
            while(u != S){
                for(int i = head[u]; ~i; i = nxt[i]){
                    if(d1[u] - wei[i] == d1[to[i]] ){
                        u = to[i]; stk.push(u); break;
                    }
                }
            }
            while(stk.size()){
                printf("%d ",stk.top()+1); stk.pop();
            }
            u = p2;
        }else {
            u = S;
        }
        while(u != E){
            printf("%d ",u+1);
            for(int i = head[u]; ~i; i = nxt[i]){
                if(d2[u] - wei[i] == d2[to[i]] ){
                    u = to[i];  break;
                }
            }
        }
        printf("%d\n",E+1);
        if(~pick) printf("%d\n",pick+1);
        else puts("Ticket Not Used");
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-08-26 17:59:49

UVA 11374 Airport Express的相关文章

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(最短路)

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 11374 - Airport Express(dijstra)

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

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

UVA 11374 Airport Express(优先队列优化dijstra + 枚举)

UVA Airport Express 题意:在Iokh市机场快线分为经济线和商业线.线路和速度价格都不同.你只有一张商业线车票,即最多只能坐一站商业线,其他时候只能坐经济线.找出一条去机场最快的线路. 思路:因为商业线只能坐一站,假如乘坐一条商业线(a,b),那么起点到a,b到终点都必须是最短路.所以先预处理起点和终点到其他所有点的最短路,分别记为f()和g(),两次dijstra即可.那么我们只需枚举每条商业线求出所有的f(a)+T(a,b)+g(b)然后求最小即可. 1w是TLE,改成了优

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

UVA - 11374 Airport Express(dijkstra)

题目大意:机场快线分为经济线和商业线两种,线路,速度和价钱都不同.你有一张商业线车票,可以坐一站商业线,而其他时候只能坐经济线 现在给你起点和终点,要求你找出一条最快的线路 解题思路:不得不说,这题还是有点恶心的 要进行两次的dijkstra,一次以起点为源点,得到的数组d1表示结点和起点最近距离 另一次以终点为源点,得到数组d2,表示结点和终点最近的距离 现在M张商业票,给出的地点为x,y,z 那么有两种选择方式,一种是从起点走到x,然后使用商业票,然后再从y走到终点,那么距离就为 d1[x]

UVA 11374 Airport Express (最短路dijkstra+枚举+边的输出)

题意:给你一个数n,s,e,n为有多少个车站,s,e是起点和终点,接下来有m条经济路线,再接下来有k条商业线,你最多只能座一条商业线,现在要你求出从s到e的最短路,并输出它所经过的节点还有座商业线的车站. 思路:实际上这道题就是考你对dijkstra的理解了,其中d数组的含意是起点到第i个点的最短距离,那么每次寻找商业路线的时候,是不是可以比较d[e]跟从起点到点u的最小值+从终点到v的最小值+w[u][v].最后我们可以根据递归输出路径了. AC代码: #include<cstdio> #i

UVA 11374 Airport Express 机场快线(单源最短路,dijkstra,变形)

题意:给一幅图,要从s点要到e点,图中有两种无向边分别在两个集合中,第一个集合是可以无限次使用的,第二个集合中的边只能挑1条.问如何使距离最短?输出路径,用了第二个集合中的哪条边,最短距离. 思路: (1)简单易操作方法:既然第二个集合的边只能有1条,就穷举下这些边,可能的边集进行求最短路,同时记录3个答案.复杂度是O(m*k). (2)时间复杂度低:不妨先求从s到每个其他点的距离d1[i],再求e到其他每个点的距离d2[i],接下来穷举第二个集合中的每条边u-v,那么最短距离为d1[u]+di