zoukankan      html  css  js  c++  java
  • 1111 Online Map (30)(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 -> v~1~ -> ... -> destination

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

    Time = T: source -> w~1~ -> ... -> 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 -> u~1~ -> ... -> 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
    

    给出一些streets的端点v1,v2,如果是one-way的,即单程,那么只能从v1到v2,如果不是单程的,也可以从v2到v1,找出最短的路(不唯一,那就找出其中时间最短的)和时间最短的(不唯一,就找出
    经过地点最少的),如果说这两个路径一样,就只输出一次,按照题目给定的格式。
    代码:
    #include <stdio.h>
    #include <string.h>
    #define inf 0x3f3f3f3f
    int m,n,source,destination,a,b,w,l,t;
    int length[501][501],times[501][501],dis[501],cost[501],cost1[501],num[501],vis[501],path1[501],path2[501];
    void getpath1(int x) {
        if(x != source) {
            getpath1(path1[x]);
            printf(" -> ");
        }
        printf("%d",x);
    }
    void getpath2(int x) {
        if(x != source) {
            getpath2(path2[x]);
            printf(" -> ");
        }
        printf("%d",x);
    }
    int equals(int x) {
        if(path1[x] != path2[x])return 0;
        else if(x == source)return 1;
        return equals(path1[x]);
    }
    int main() {
        scanf("%d%d",&n,&m);
        for(int i = 0;i < n;i ++) {
            for(int j = 0;j < n;j ++) {
                length[i][j] = times[i][j] = inf;
            }
            dis[i] = cost[i] = cost1[i] = inf;
            path1[i] = path2[i] = -1;
        }
        for(int i = 0;i < m;i ++) {
            scanf("%d%d%d%d%d",&a,&b,&w,&l,&t);
            if(w) {
                length[a][b] = l;
                times[a][b] = t;
            }
            else {
                length[a][b] = length[b][a] = l;
                times[a][b] = times[b][a] = t;
            }
        }
        scanf("%d%d",&source,&destination);
        dis[source] = cost[source] = cost1[source] = 0;
        while(1) {
            int t = -1,mi = inf;
            for(int i = 0;i < n;i ++) {
                if(!vis[i] && mi > dis[i]) {
                    mi = dis[i];
                    t = i;
                }
            }
            if(t == -1)break;
            vis[t] = 1;
            for(int i = 0;i < n;i ++) {
                if(vis[i] || length[t][i] == inf)continue;
                if(dis[i] > dis[t] + length[t][i]) {
                    path1[i] = t;
                    dis[i] = dis[t] + length[t][i];
                    cost1[i] = cost1[t] + times[t][i];
                }
                else if(dis[i] == dis[t] + length[t][i] && cost1[i] > cost1[t] + times[t][i]) {
                    cost1[i] = cost1[t] + times[t][i];
                    path1[i] = t;
                }
            }
        }
        memset(vis,0,sizeof(vis));
        while(1) {
            int t = -1,mi = inf;
            for(int i = 0;i < n;i ++) {
                if(!vis[i] && mi > cost[i]) {
                    mi = cost[i];
                    t = i;
                }
            }
            if(t == -1)break;
            vis[t] = 1;
            for(int i = 0;i < n;i ++) {
                if(vis[i] || times[t][i] == inf)continue;
                if(cost[i] > cost[t] + times[t][i]) {
                    path2[i] = t;
                    cost[i] = cost[t] + times[t][i];
                    num[i] = num[t] + 1;
                }
                else if(cost[i] == cost[t] + times[t][i] && num[i] > num[t] + 1) {
                    num[i] = num[t] + 1;
                    path2[i] = t;
                }
            }
        }
        printf("Distance = %d",dis[destination]);
        if(!equals(destination)) {
            printf(": ");
            getpath1(destination);
            printf("
    ");
        }
        else {
            printf("; ");
        }
        printf("Time = %d: ",cost[destination]);
        getpath2(destination);
    }
    View Code
  • 相关阅读:
    闲来无事研究研究.Net中的异步编程
    Sql Server 因为触发器问题导致数据库更新报错“在触发器执行过程中引发了错误,批处理已中止”的问题处理
    c# 连接Redis报错:WRONGTYPE Operation against a key holding the wrong kind of value:类型搞混弄出的错误
    VS2013 调试时出现“表达式计算器中发生内部错误”的问题解决办法
    WCF优化的几个常规思路
    UWP汉堡菜单
    C#注册系统全局快捷键
    CXF详细介绍
    hadoop默认3个核心配置文件说明
    在虚拟机配置hive
  • 原文地址:https://www.cnblogs.com/8023spz/p/9300733.html
Copyright © 2011-2022 走看看