• 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), 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 N1) 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

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1111;
    #define eps 1e-8
    #define  inf  0x3fffffff
    int D[maxn][maxn];
    int T[maxn][maxn];
    int dist[maxn];
    int distT[maxn];
    bool vis[maxn];
    vector<int> preD[maxn];
    vector<int> preT[maxn];
    vector<int> tpath;
    vector<int> dpath;
    int n,m,sour,dest;;
    void dijkstra(int index){
        fill(dist,dist+maxn,inf);
        fill(vis,vis+maxn,false);
        dist[index]=0;
        for(int i=0;i<n;i++){
            int mind=inf,minv=-1;
            for(int i=0;i<n;i++){
                if(vis[i]==false&&dist[i]<mind){
                    mind=dist[i];
                    minv=i;
                }
            }
            int u=minv;
            if(u==-1){
                return ;
            }
            vis[u]=true;
            for(int v=0;v<n;v++){
                if(vis[v]==false&&D[u][v]!=inf&&dist[u]+D[u][v]<dist[v]){
                    dist[v]=dist[u]+D[u][v];
                    preD[v].clear();
                    preD[v].push_back(u);
                }
                else if(dist[u]+D[u][v]==dist[v]){
                    preD[v].push_back(u);
                }
            }
        }
        
    }
    void dijkstraT(int index){
        fill(distT,distT+maxn,inf);
        fill(vis,vis+maxn,false);
        distT[index]=0;
        for(int i=0;i<n;i++){
            int mind=inf,minv=-1;
            for(int i=0;i<n;i++){
                if(vis[i]==false&&dist[i]<mind){
                    mind=dist[i];
                    minv=i;
                }
            }
            int u=minv;
            if(u==-1){
                return ;
            }
            vis[u]=true;
            for(int v=0;v<n;v++){
                if(vis[v]==false&&T[u][v]!=inf&&distT[u]+T[u][v]<distT[v]){
                    distT[v]=T[u][v]+distT[u];
                    preT[v].clear();
                    preT[v].push_back(u);
                }
                else if(distT[u]+T[u][v]==distT[v]){
                    preT[v].push_back(u);
                }
            }
        }
    }
    vector<int> tempath;
    int minTime=inf;
    void dfs(int v){//v必须是终点,否则preD[v].size()为0,没有结果输出
        if(v==sour){
            tempath.push_back(v);
            int TimeD=0;
            for(int i=tempath.size()-2;i>=0;i--){
                TimeD+=T[tempath[i+1]][tempath[i]];//不能写成i>0;  i  i-1  ,第三个测试点过不去
            }
            if(TimeD<minTime){
                minTime=TimeD;
                dpath=tempath;
            }
            tempath.pop_back();
            return ;
        }
        tempath.push_back(v);
        for(int i=0;i<preD[v].size();i++){
            dfs(preD[v][i]);
        }
        tempath.pop_back();
    }
    vector<int> tempathT;
    int minI=inf;
    void dfs2(int u){
        if(u==sour){
            tempathT.push_back(u);
            if(tempathT.size()<minI){
                minI=tempathT.size();
                tpath=tempathT;
            }
            tempathT.pop_back();
            return ;
        }
        tempathT.push_back(u);
        for(int i=0;i<preT[u].size();i++){
            dfs2(preT[u][i]);
        }
        tempathT.pop_back();
    }
    int main(){
        fill(D[0],D[0]+maxn*maxn,inf);
        fill(T[0],T[0]+maxn*maxn,inf);
        cin>>n>>m;
        int v1,v2,ow,len,ti;
        for(int i=0;i<m;i++){
            cin>>v1>>v2>>ow>>len>>ti;
            if(ow==1){
                D[v1][v2]=len;
                T[v1][v2]=ti;
            }
            else if(ow==0){
                D[v1][v2]=D[v2][v1]=len;
                T[v1][v2]=T[v2][v1]=ti;
            }
        }
        cin>>sour>>dest;
        dijkstra(sour);
        dijkstraT(sour);
        dfs(dest);
        dfs2(dest);
        bool flag=true;
        for(int i=0;i<dpath.size();i++){
            if(dpath[i]!=tpath[i]){
                flag=false;
                break;
            }
        }
        if(flag==false){
            printf("Distance = %d: ",dist[dest]);
            for(int i=dpath.size()-1;i>=0;i--){
                if(i>0)
                printf("%d -> ",dpath[i]);
                else{
                    printf("%d
    ",dpath[i]);
                }
            }
            printf("Time = %d: ",distT[dest]);
            for(int i=tpath.size()-1;i>=0;i--){
                if(i>0){
                    printf("%d -> ",tpath[i]);
                }
                else{
                    printf("%d
    ",tpath[i]);
                }
            }
            
        }
        else{
            printf("Distance = %d;",dist[dest]);
            printf(" Time = %d: ",distT[dest]);
            for(int i=tpath.size()-1;i>=0;i--){
                if(i>0){
                    printf("%d -> ",tpath[i]);
                }
                else{
                    printf("%d
    ",tpath[i]);
                }
            }
        }
            
        return 0;
    }
  • 相关阅读:
    poj 1743 Musical Theme 后缀数组
    poj 1743 Musical Theme 后缀数组
    cf 432D Prefixes and Suffixes kmp
    cf 432D Prefixes and Suffixes kmp
    hdu Data Structure? 线段树
    关于position和anchorPoint之间的关系
    ios POST 信息
    CALayers的代码示例
    CALayers详解
    ios中得sqlite使用基础
  • 原文地址:https://www.cnblogs.com/dreamzj/p/14448722.html
Copyright © 2020-2023  润新知