UVA 11374 - Airport Express(最短路)

Posted AC_Arthur

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA 11374 - Airport Express(最短路)相关的知识,希望对你有一定的参考价值。

题目链接:点击打开链接

题意:某个人要从起点到终点, 有m个经济站和k个商业站, 只能乘坐一次商业站, 求最短时间。

思路:因为多了商业站, 所以不能直接套用最短路, 但是注意到只有一次乘坐商业站的机会, 所以直接枚举坐哪个商业站就行了, 这样,其他部分就是最短路了, 假设商业站是从a到b,花费c,那么答案就是d1[a] + d2[b] + c, d1是从起点的最短路,d2是从终点出发的最短路。

细节参见代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int mod = 1000000000 + 7;
const int INF = 1000000000;
const int maxn = 500 + 10;
int T,n,m,S,d1[maxn], p1[maxn], d2[maxn], p2[maxn], vis[maxn], ok, dist;
struct node 
    int u, val;
    node(int u=0, int val=0):u(u), val(val) 
    bool operator < (const node& rhs) const 
        return val > rhs.val;
    
;
void print(int root, int p[], int id, int S) 
    vector<int> ans;
    while(root != S) 
        ans.push_back(root);
        root = p[root];
    
    ans.push_back(root);
    int len = ans.size();

    if(id == 1) for(int i=len-1;i>=0;i--) 
        if(i != len-1) printf(" ");
        printf("%d",ans[i]);
    
    else for(int i=0;i<len;i++) 
        if(i != 0) printf(" ");
        printf("%d",ans[i]);
    

vector<node> g[maxn];
void BFS(int haha, int d[], int p[]) 
    priority_queue<node> q;
    q.push(node(haha, 0));
    for(int i=1;i<=n;i++) 
        d[i] = INF;
    
    d[haha] = 0;
    memset(vis, false, sizeof(vis));
    while(!q.empty()) 
        node u = q.top(); q.pop();
        if(vis[u.u]) continue;
        vis[u.u] = true;
        int len = g[u.u].size();
        for(int i=0;i<len;i++) 
            node v = g[u.u][i];
            if(d[v.u] > d[u.u] + v.val) 
                d[v.u] = d[u.u] + v.val;
                p[v.u] = u.u;
                q.push(node(v.u, d[v.u]));
            
        
    

int a,b,c,kase=0;
int main() 
    while(~scanf("%d%d%d",&n,&S,&T)) 
        scanf("%d",&m);
        for(int i=1;i<=n;i++) g[i].clear();
        while(m--) 
            scanf("%d%d%d",&a,&b,&c);
            g[a].push_back(node(b, c));
            g[b].push_back(node(a, c));
        
        scanf("%d",&m);
        ok = -1;
        BFS(S, d1, p1);
        BFS(T, d2, p2);
        int s = -1,t = -1,ans = d1[T], res = 0;
        for(int i=0;i<m;i++) 
            scanf("%d%d%d",&a,&b,&c);
            int cur = d1[a] + d2[b] + c;
            if(cur < ans) 
                ans = cur;
                s = a; t = b;
            
            cur = d1[b] + d2[a] + c;
            if(cur < ans) 
                ans = cur;
                s = b; t = a;
            
        
        if(kase) printf("\\n");
        else ++kase;
        if(s > 0) 
            print(s, p1, 1, S); printf(" ");
            print(t, p2, 2, T); printf("\\n");
            printf("%d\\n",s);
        
        else 
            print(T, p1, 1, S); printf("\\n");
            printf("Ticket Not Used\\n");
        
        printf("%d\\n",ans);
    
    return 0;



以上是关于UVA 11374 - Airport Express(最短路)的主要内容,如果未能解决你的问题,请参考以下文章

UVA11374 Airport Express

Airport Express UVA - 11374(dijkstra)

UVA11374 Airport Express

UVA11374 Airport Express

Airport Express UVA - 11374

UVA 11374 - Airport Express(最短路)