UVA11374 Airport Express

题意:n个点,m条边,两种边,第二种边只能走其中的一条,问起始位置到终点的最短距离

题解:模板题,只要枚举每一条边,预处理两边的最短路,Dijkstra单源最短路

#include <bits/stdc++.h>
#define ll long long
#define maxn 100010
#define INF 1e9+7
using namespace std;
struct edge{
    int from,to,dist;
};
struct node{
    int d,u;
    bool operator<(const node& a) const{
        return d>a.d;
    }
};
struct dij{
    int n,m;
    vector<int >G[maxn];//从0开始
    vector<edge>edges;
    bool done[maxn];
    int d[maxn]; //距离
    int p[maxn]; //上一条边,端点判断
    void init(int x){//边数,初始化
        n = x;
        for(int i=0;i<n;i++) G[i].clear();
        edges.clear();
    }
    void add(int a,int b,int w){
        edges.push_back((edge){a,b,w});
        m = edges.size();
        G[a].push_back(m-1);
    }
    void dijstra(int st){//计算最短路
        for(int i=0;i<n;i++) d[i] = INF;
        memset(done, 0, sizeof(done));
        d[st] = 0;
        priority_queue<node>q;
        q.push((node){0, st});
        while(!q.empty()){
            node fi = q.top();q.pop();
            int u = fi.u;
            if(done[u]) continue;
            done[u] = 1;
            for(int i=0;i<G[u].size();i++){
                edge& e = edges[G[u][i]];
                if(e.dist+d[u]<d[e.to]){
                    d[e.to] = d[u]+e.dist;
                    p[e.to] = G[u][i];
                    q.push((node){d[e.to], e.to});
                }
            }
        }
    }
}ds, de;
int s,e;
void dfs1(int a){
    if(a == s){
        cout<<a+1;
        return ;
    }
    edge e = ds.edges[ds.p[a]];
    dfs1(e.from);
    cout<<" "<<a+1;
}
void dfs2(int a){
    if(a == e){
        cout<<" "<<a+1;
        return ;
    }
    edge e = de.edges[de.p[a]];
    cout<<" "<<a+1;
    dfs2(e.from);
}
int main(){
    int n, m, k, a, b, c, aaa=0;
    while(cin>>n>>s>>e){
        if(aaa++) cout<<"\n";
        s--,e--;
        ds.init(n);de.init(n);
        cin>>m;
        for(int i=0;i<m;i++){
            cin>>a>>b>>c;
            a--,b--;
            ds.add(a, b, c);ds.add(b, a, c);
            de.add(a, b, c);de.add(b, a, c);
        }
        ds.dijstra(s); de.dijstra(e);
        cin>>k;
        int u=-1,v=-1, ans = ds.d[e];
        for(int i=0;i<k;i++){
            cin>>a>>b>>c;
            a--,b--;
            if(ds.d[a]+c+de.d[b] < ans){
                ans = ds.d[a]+c+de.d[b];
                u = a;
                v = b;
            }
            if(ds.d[b]+c+de.d[a] < ans){
                ans = ds.d[b]+c+de.d[a];
                u = b;
                v = a;
            }
        }
        if(u == -1){
            dfs1(e);cout<<endl;
            cout<<"Ticket Not Used\n";
            cout<<ans<<endl;
        }
        else{
            dfs1(u);dfs2(v);cout<<endl;
            cout<<u+1<<"\n"<<ans<<endl;
        }
    }
    //fclose(stdout);
    return 0;
}
时间: 2024-12-19 13:52:38

UVA11374 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(优先队列优化dijstra + 枚举)

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

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

BNUOJ 19792 Airport Express

Airport Express Time Limit: 1000ms Memory Limit: 131072KB This problem will be judged on UVA. Original ID: 1137464-bit integer IO format: %lld      Java class name: Main In a small city called Iokh, a train service, Airport-Express, takes residents t

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(dijkstra)

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