zoukankan      html  css  js  c++  java
  • [uva] 10099

    10099 - The Tourist Guide

    题目页:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1040

    打不开的可以上国内的:http://acm.sdibt.edu.cn/JudgeOnline/problem.php?id=1080

    之前使用 最小生成树的 Kruskal 算法 + 广度优先搜索

    改进之后用了 用动态规划技术实现的所有节点对的最短路径问题的 Floyd-Warshall 算法,不仅代码量急剧减少,简化了逻辑

    此题的坑之一:Mr.G.自己也算一个人……… 反映到代码里是63行

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    #include <stdio.h>
    #include <stdlib.h>
     
    int number_points;
    int map_values[110][110];
     
    void floyd()
    {
        for (int a = 1; a <= number_points; a++)
        {
            for (int b = 1; b <= number_points; b++)
            {
                for (int x = 1; x <= number_points; x++)
                {
                    int ax = map_values[a][x];
                    int xb = map_values[x][b];
     
                    int smaller = ax < xb ? ax : xb;
                    if (map_values[a][b] < smaller)
                    {
                        map_values[a][b] = smaller;
                    }
                }
            }
        }
    }
     
     
    int main()
    {
        int count = 1;
        int number_ways;
        scanf("%d%d", &number_points, &number_ways);
        while(number_points != 0 && number_ways != 0)
        {
            // 0. 初始化图
            for (int y = 1; y <= number_points; y++)
            for (int x = 1; x <= number_points; x++)
            {
                map_values[x][y] = 0;
            }
     
            // 1. 读取边
            for (int i = 0; i < number_ways; i++)
            {
                int x, y, values;
                scanf("%d%d%d", &x, &y, &values);
                map_values[x][y] = values;
                map_values[y][x] = values;
            }
     
            // 2. 读取起始节点和顾客数量
            int start, end, number_customer;
            scanf("%d%d%d", &start, &end, &number_customer);
     
            // 3. floyd 算法
            floyd();
     
            // 4. 获取值
            int max_value = map_values[start][end];
     
            // 5. 输出结果
            max_value--; // Mr. G. 自己也算上
     
            int remainder = number_customer % max_value;
            printf("Scenario #%d Minimum Number of Trips = ", count);
            if (remainder)
                printf("%d ", number_customer / max_value + 1);
            else
                printf("%d ", number_customer / max_value);
     
            // 6. 读取下一行数据
            scanf("%d%d", &number_points, &number_ways);
            count++;
        }
     
        return 0;
    }
  • 相关阅读:
    软件设计7个原则
    vue.js 样式绑定 font-size 问题
    实例理解scala 隐式转换(隐式值,隐式方法,隐式类)
    著名端口整理(常用服务的默认端总结)
    .NET Core Web API 实现大文件分片上传
    ngnix反向代理tomcat,ssl证书配置及自定义错误页面
    微信登录闪退
    gradle如何配置阿里云的中央仓库
    HashMap底层实现和原理
    关于Java中String类的hashCode方法
  • 原文地址:https://www.cnblogs.com/night-ride-depart/p/5065054.html
Copyright © 2011-2022 走看看