zoukankan      html  css  js  c++  java
  • 数据结构实验之图论七:驴友计划

    数据结构实验之图论七:驴友计划

    Time Limit: 1000MS Memory Limit: 65536KB

    Problem Description

    做为一个资深驴友,小新有一张珍藏的自驾游线路图,图上详细的标注了全国各个城市之间的高速公路距离和公路收费情况,现在请你编写一个程序,找出一条出发地到目的地之间的最短路径,如果有多条路径最短,则输出过路费最少的一条路径。

    Input

    连续T组数据输入,每组输入数据的第一行给出四个正整数N,M,s,d,其中N(2 <= N <= 500)是城市数目,城市编号从0~N-1,M是城市间高速公路的条数,s是出发地的城市编号,d是目的地的城市编号;随后M行,每行给出一条高速公路的信息,表示城市1、城市2、高速公路长度、收费额,中间以空格间隔,数字均为整数且不超过500,输入数据均保证有解。 

    Output

    在同一行中输出路径长度和收费总额,数据间用空格间隔。 

    Example Input

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

    Example Output

    3 40

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <algorithm>
    #define MAX 0x3f3f3f
    using namespace std;
    int n, m, s, d;
    struct node
    {
        int l,w;
    }mp[550][550];
    void creat()
    {
        int u, v, l, w;
         for(int i = 0; i < n; i++)
            for(int j = 0; j < n; j++)
         {
             if(i!=j)
             {
                 mp[i][j].l = mp[i][j].w = MAX;
             }
         }
        for(int i = 0; i < m; i++)
        {
            scanf("%d%d%d%d",&u,&v,&l,&w);
            mp[u][v].l = l;
            mp[u][v].w = w;
            mp[v][u].l = l;
            mp[v][u].w = w;
        }
    
    }
    void Fory()
    {
        for(int i = 0; i < n; i++)
            for(int j = 0; j < n; j++)
              for(int k = 0; k < n; k++)
             {
               if(mp[k][i].l + mp[i][j].l < mp[k][j].l)
               {
                      mp[k][j].l = mp[k][i].l + mp[i][j].l;
                      mp[k][j].w = mp[k][i].w + mp[i][j].w;
               }
               else if(mp[k][i].l + mp[i][j].l == mp[k][j].l)
               {
                   if(mp[k][j].w > mp[k][i].w + mp[i][j].w)
                   {
                       mp[k][j].w = mp[k][i].w + mp[i][j].w;
                   }
               }
    
             }
        cout<<mp[s][d].l<<" "<<mp[s][d].w<<endl;
    }
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d%d%d%d",&n,&m,&s,&d);
            creat();
            Fory();
        }
        return 0;
    }
    


  • 相关阅读:
    解决Centos7下中文显示乱码
    Pycharm2019.2.1永久激活
    window 共享打印机
    看着前车轮胎能出库
    计算之道 (置换的玩笑)搜索
    GIS+=地理信息+云计算技术——私有云架构设计(2)网络资源规划
    串的模式匹配
    1.Linux下libevent和memcached安装
    STL--H
    【数据结构与算法】(二) c 语言链表的简单操作
  • 原文地址:https://www.cnblogs.com/CCCrunner/p/11782119.html
Copyright © 2011-2022 走看看