zoukankan      html  css  js  c++  java
  • 【刷题-PAT】A1111 Online Map (30 分)

    1111 Online Map (30 分)

    Input our current position and a destination, an online map can recommend several paths. Now your job is to recommend two paths to your user: one is the shortest, and the other is the fastest. It is guaranteed that a path exists for any request.

    Input Specification:

    Each input file contains one test case. For each case, the first line gives two positive integers N (2≤N≤500), and M, being the total number of streets intersections on a map, and the number of streets, respectively. Then M lines follow, each describes a street in the format:

    V1 V2 one-way length time
    

    where V1 and V2 are the indices (from 0 to N−1) of the two ends of the street; one-way is 1 if the street is one-way from V1 to V2, or 0 if not; length is the length of the street; and time is the time taken to pass the street.

    Finally a pair of source and destination is given.

    Output Specification:

    For each case, first print the shortest path from the source to the destination with distance D in the format:

    Distance = D: source -> v1 -> ... -> destination
    

    Then in the next line print the fastest path with total time T:

    Time = T: source -> w1 -> ... -> destination
    

    In case the shortest path is not unique, output the fastest one among the shortest paths, which is guaranteed to be unique. In case the fastest path is not unique, output the one that passes through the fewest intersections, which is guaranteed to be unique.

    In case the shortest and the fastest paths are identical, print them in one line in the format:

    Distance = D; Time = T: source -> u1 -> ... -> destination
    

    Sample Input 1:

    10 15
    0 1 0 1 1
    8 0 0 1 1
    4 8 1 1 1
    3 4 0 3 2
    3 9 1 4 1
    0 6 0 1 1
    7 5 1 2 1
    8 5 1 2 1
    2 3 0 2 2
    2 1 1 1 1
    1 3 0 3 1
    1 4 0 1 1
    9 7 1 3 1
    5 1 0 5 2
    6 5 1 1 2
    3 5
    

    Sample Output 1:

    Distance = 6: 3 -> 4 -> 8 -> 5
    Time = 3: 3 -> 1 -> 5
    

    Sample Input 2:

    7 9
    0 4 1 1 1
    1 6 1 1 3
    2 6 1 1 1
    2 5 1 2 2
    3 0 0 1 1
    3 1 1 1 3
    3 2 1 1 2
    4 5 0 2 2
    6 5 1 1 2
    3 5
    

    Sample Output 2:

    Distance = 3; Time = 4: 3 -> 2 -> 5
    

    分析:dijkstra + dfs,先找出最短路径,再dfs出最短路径,根据题目中tie的处理方式选择出最终的路径

    #include<iostream>
    #include<cstdio>
    #include<vector>
    #include<string>
    #include<unordered_map>
    #include<set>
    #include<queue>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    const int nmax = 510, inf = (1 << 31) - 1;
    struct node{
        int v, len, t;
    };
    vector<node>G[nmax];
    int d[nmax], td[nmax], t[nmax], preV[nmax];
    vector<int>pret[nmax];
    bool visv[nmax] = {false}, vist[nmax] = {false};
    void dij(int s, int n){
        fill(d, d + nmax, inf);
        fill(td, td + nmax, inf);
        fill(t, t + nmax, inf);
        d[s] = 0, td[s] = 0, t[s] = 0;
        for(int i = 0; i < n; ++i){
            int u = -1, MIN = inf;
            for(int j = 0; j < n; ++j){
                if(visv[j] == false && d[j] < MIN){
                    MIN = d[j];
                    u = j;
                }
            }
            if(u == -1)return;
            visv[u] = true;
            for(int j = 0; j < G[u].size(); ++j){
                int v = G[u][j].v;
                if(visv[v] == false){
                    if(d[u] + G[u][j].len < d[v]){
                        d[v] = d[u] + G[u][j].len;
                        td[v] = td[u] + G[u][j].t;
                        preV[v] = u;
                    }else if(d[u] + G[u][j].len == d[v] && td[u] + G[u][j].t < td[v]){
                        td[v] = td[u] + G[u][j].t;
                        preV[v] = u;
                    }
                }
            }
            u = -1, MIN = inf;
            for(int j = 0; j < n; ++j){
                if(vist[j] == false && t[j] < MIN){
                    MIN = t[j];
                    u = j;
                }
            }
            if(u == -1)return;
            vist[u] = true;
            for(int j = 0; j < G[u].size(); ++j){
                int v = G[u][j].v;
                if(vist[v] == false){
                    if(t[u] + G[u][j].t < t[v]){
                        t[v] = t[u] + G[u][j].t;
                        pret[v].clear();
                        pret[v].push_back(u);
                    }else if(t[u] + G[u][j].t == t[v]){
                        pret[v].push_back(u);
                    }
                }
            }
        }
    }
    vector<int>pathv;
    void dfs1(int s, int e){
        pathv.push_back(s);
        if(s == e)return;
        dfs1(preV[s], e);
    }
    vector<int>patht, temp;
    int intermin = inf;
    void dfs2(int s, int e){
        temp.push_back(s);
        if(s == e){
            if(temp.size() < intermin){
                intermin = temp.size();
                patht = temp;
            }
            return;
        }
    
        for(int i = 0; i < pret[s].size(); ++i){
            dfs2(pret[s][i], e);
            temp.pop_back();
        }
    }
    void Print(vector<int> &path){
        for(int i = path.size() - 1; i >= 0; --i){
            printf("%d", path[i]);
            if(i > 0)printf(" -> ");
            else printf("
    ");
        }
    }
    int main(){
        #ifdef ONLINE_JUDGE
        #else
        freopen("input.txt", "r", stdin);
        #endif // ONLINE_JUDGE
        int n, m;
        scanf("%d%d", &n, &m);
        for(int i = 0; i < m; ++i){
            int v1, v2, tag, len, t;
            scanf("%d%d%d%d%d", &v1, &v2, &tag, &len, &t);
            G[v1].push_back({v2, len, t});
            if(tag == 0)G[v2].push_back({v1, len, t});
        }
        int s, e;
        scanf("%d%d", &s, &e);
        dij(s, n);
        dfs1(e, s);
        dfs2(e, s);
        bool flag = false;
        if(patht.size() == pathv.size()){
            int i = 0;
            while(i < patht.size() && pathv[i] == patht[i])i++;
            if(i == patht.size())flag = true;
        }
        printf("Distance = %d", d[e]);
        if(flag == false){
            printf(": ");
            Print(pathv);
        }else{
            printf("; ");
        }
        printf("Time = %d: ", t[e]);
        Print(patht);
        return 0;
    }
    
  • 相关阅读:
    hdu 1175
    hdu 2197
    STL的学习
    数据结构之线性结构栈
    Linux下Fork与Exec使用
    散列技术之哈希
    检索之二分检索
    检索之顺序检索
    程序员该怎样放松?8个好网站推荐
    外部碎片和内部碎片的区别
  • 原文地址:https://www.cnblogs.com/vinnson/p/10845047.html
Copyright © 2011-2022 走看看