zoukankan      html  css  js  c++  java
  • 07-图6 旅游规划 (25 分)

    有了一张自驾旅游路线图,你会知道城市间的高速公路长度、以及该公路要收取的过路费。现在需要你写一个程序,帮助前来咨询的游客找一条出发地和目的地之间的最短路径。如果有若干条路径都是最短的,那么需要输出最便宜的一条路径。

    输入格式:

    输入说明:输入数据的第1行给出4个正整数N、M、S、D,其中N(2N500)是城市的个数,顺便假设城市的编号为0~(N1);M是高速公路的条数;S是出发地的城市编号;D是目的地的城市编号。随后的M行中,每行给出一条高速公路的信息,分别是:城市1、城市2、高速公路长度、收费额,中间用空格分开,数字均为整数且不超过500。输入保证解的存在。

    输出格式:

    在一行里输出路径的长度和收费总额,数字间以空格分隔,输出结尾不能有多余空格。

    输入样例:

    4 5 0 3
    0 1 1 20
    1 3 2 30
    0 3 4 10
    0 2 2 20
    2 3 1 20
    

    输出样例:

    3 40

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int maxn = 550;
    const int INF = 100000000;
    
    int G[maxn][maxn],cost[maxn][maxn];
    int d[maxn],c[maxn];
    bool vis[maxn] = {0};
    int n,m,st,ed;
    
    void Dijkstra(int s);
    
    int main()
    {
        int u,v;
        scanf("%d%d%d%d",&n,&m,&st,&ed);
        fill(G[0],G[0]+maxn*maxn,INF);
        
        for (int i = 0; i < m; i++)
        {
            scanf("%d%d",&u,&v);
            scanf("%d%d",&G[u][v],&cost[u][v]);
            G[v][u] = G[u][v];
            cost[v][u] = cost[u][v];
        }
        
        Dijkstra(st);
        
        printf("%d %d",d[ed],c[ed]);
        return 0;    
    }
    
    void Dijkstra(int s)
    {
        fill(d,d+maxn,INF);
        fill(c,c+maxn,INF);
        d[s] = 0;
        c[s] = 0;
        
        for (int i = 0; i < n; i++)
        {
            int u = -1, min = INF;
            for (int j = 0; j < n; j++)
            {
                if (!vis[j] && d[j] < min)
                {
                    u = j;
                    min = d[j];
                }
            }
            
            if (-1 == u)
            {
                return;
            }
            vis[u] = true;
            
            for (int v = 0; v < n; v++)
            {
                if (!vis[v] && G[u][v] != INF)
                {
                    if (d[v] > G[u][v] + d[u])
                    {
                        d[v] = G[u][v] + d[u];
                        c[v] = cost[u][v] + c[u];
                    }
                    else if(d[v] == G[u][v] + d[u])
                    {
                        if (c[v] > cost[u][v] + c[u])
                        {
                            c[v] = cost[u][v] + c[u];
                        }
                    }
                }
            }
        }
    }
     
  • 相关阅读:
    linux配置静态ip
    hadoop伪分布式搭建
    flume安装
    远程shell脚本执行工具类
    NextCloud 修改数据存储位置(以CentOS 8(apache)为例)
    CP2K入门教程转载分享
    Origin 2019b 合法获取与使用介绍——正版软件&最新最实用教程分享 (Origin下载)
    Internet Download Manager:IDM 6.3.29破解版——最快的下载工具
    关于通过IPv6地址远程登录服务器的操作说明
    安装python的第一个曲折
  • 原文地址:https://www.cnblogs.com/wanghao-boke/p/11802107.html
Copyright © 2011-2022 走看看