PAT 甲级考试【1003 Emergency】

这篇具有很好参考价值的文章主要介绍了PAT 甲级考试【1003 Emergency】。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

题目:

As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible.

Input Specification:

Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (500) - the number of cities (and the cities are numbered from 0 to N1), M - the number of roads, C1 and C2 - the cities that you are currently in and that you must save, respectively. The next line contains N integers, where the i-th integer is the number of rescue teams in the i-th city. Then M lines follow, each describes a road with three integers c1c2 and L, which are the pair of cities connected by a road and the length of that road, respectively. It is guaranteed that there exists at least one path from C1 to C2.

Output Specification:

For each test case, print in one line two numbers: the number of different shortest paths between C1 and C2, and the maximum amount of rescue teams you can possibly gather. All the numbers in a line must be separated by exactly one space, and there is no extra space allowed at the end of a line.

Sample Input:

5 6 0 2
1 2 1 5 3
0 1 1
0 2 2
0 3 1
1 2 1
2 4 1
3 4 1

Sample Output:

2 4

 

Dijkstra 算法

Dijkstra(/ˈdikstrɑ/或/ˈdɛikstrɑ/)算法由荷兰计算机科学家 E. W. Dijkstra 于 1956 年发现,1959 年公开发表。是一种求解 非负权图 上单源最短路径的算法。

过程

将结点分成两个集合:已确定最短路长度的点集(记为 PAT 甲级考试【1003 Emergency】 集合)的和未确定最短路长度的点集(记为 PAT 甲级考试【1003 Emergency】 集合)。一开始所有的点都属于 PAT 甲级考试【1003 Emergency】 集合。

初始化 PAT 甲级考试【1003 Emergency】,其他点的 PAT 甲级考试【1003 Emergency】 均为 PAT 甲级考试【1003 Emergency】

然后重复这些操作:

  1. 从 PAT 甲级考试【1003 Emergency】 集合中,选取一个最短路长度最小的结点,移到 PAT 甲级考试【1003 Emergency】 集合中。
  2. 对那些刚刚被加入 PAT 甲级考试【1003 Emergency】 集合的结点的所有出边执行松弛操作。

直到 PAT 甲级考试【1003 Emergency】 集合为空,算法结束。

时间复杂度

有多种方法来维护 1 操作中最短路长度最小的结点,不同的实现导致了 Dijkstra 算法时间复杂度上的差异。

  • 暴力:不使用任何数据结构进行维护,每次 2 操作执行完毕后,直接在 PAT 甲级考试【1003 Emergency】 集合中暴力寻找最短路长度最小的结点。2 操作总时间复杂度为 PAT 甲级考试【1003 Emergency】,1 操作总时间复杂度为 PAT 甲级考试【1003 Emergency】,全过程的时间复杂度为 PAT 甲级考试【1003 Emergency】
  • 二叉堆:每成功松弛一条边 PAT 甲级考试【1003 Emergency】,就将 PAT 甲级考试【1003 Emergency】 插入二叉堆中(如果 PAT 甲级考试【1003 Emergency】 已经在二叉堆中,直接修改相应元素的权值即可),1 操作直接取堆顶结点即可。共计 PAT 甲级考试【1003 Emergency】 次二叉堆上的插入(修改)操作,PAT 甲级考试【1003 Emergency】 次删除堆顶操作,而插入(修改)和删除的时间复杂度均为 PAT 甲级考试【1003 Emergency】,时间复杂度为 PAT 甲级考试【1003 Emergency】
  • 优先队列:和二叉堆类似,但使用优先队列时,如果同一个点的最短路被更新多次,因为先前更新时插入的元素不能被删除,也不能被修改,只能留在优先队列中,故优先队列内的元素个数是 PAT 甲级考试【1003 Emergency】 的,时间复杂度为 PAT 甲级考试【1003 Emergency】
  • Fibonacci 堆:和前面二者类似,但 Fibonacci 堆插入的时间复杂度为 PAT 甲级考试【1003 Emergency】,故时间复杂度为 PAT 甲级考试【1003 Emergency】,时间复杂度最优。但因为 Fibonacci 堆较二叉堆不易实现,效率优势也不够大1,算法竞赛中较少使用。
  • 线段树:和二叉堆原理类似,不过将每次成功松弛后插入二叉堆的操作改为在线段树上执行单点修改,而 1 操作则是线段树上的全局查询最小值。时间复杂度为 PAT 甲级考试【1003 Emergency】

在稀疏图中,PAT 甲级考试【1003 Emergency】,使用二叉堆实现的 Dijkstra 算法较 Bellman–Ford 算法具有较大的效率优势;而在稠密图中,PAT 甲级考试【1003 Emergency】,这时候使用暴力做法较二叉堆实现更优。

正确性证明

下面用数学归纳法证明,在 所有边权值非负 的前提下,Dijkstra 算法的正确性2

简单来说,我们要证明的,就是在执行 1 操作时,取出的结点 PAT 甲级考试【1003 Emergency】 最短路均已经被确定,即满足 PAT 甲级考试【1003 Emergency】

初始时 PAT 甲级考试【1003 Emergency】,假设成立。

接下来用反证法。

设 PAT 甲级考试【1003 Emergency】 点为算法中第一个在加入 PAT 甲级考试【1003 Emergency】 集合时不满足 PAT 甲级考试【1003 Emergency】 的点。因为 PAT 甲级考试【1003 Emergency】 点一定满足 PAT 甲级考试【1003 Emergency】,且它一定是第一个加入 PAT 甲级考试【1003 Emergency】 集合的点,因此将 PAT 甲级考试【1003 Emergency】 加入 PAT 甲级考试【1003 Emergency】 集合前,PAT 甲级考试【1003 Emergency】,如果不存在 PAT 甲级考试【1003 Emergency】 到 PAT 甲级考试【1003 Emergency】 的路径,则 PAT 甲级考试【1003 Emergency】,与假设矛盾。

于是一定存在路径 PAT 甲级考试【1003 Emergency】,其中 PAT 甲级考试【1003 Emergency】 为 PAT 甲级考试【1003 Emergency】 路径上第一个属于 PAT 甲级考试【1003 Emergency】 集合的点,而 PAT 甲级考试【1003 Emergency】 为 PAT 甲级考试【1003 Emergency】 的前驱结点(显然 PAT 甲级考试【1003 Emergency】)。需要注意的是,可能存在 PAT 甲级考试【1003 Emergency】 或 PAT 甲级考试【1003 Emergency】 的情况,即 PAT 甲级考试【1003 Emergency】 或 PAT 甲级考试【1003 Emergency】 可能是空路径。

因为在 PAT 甲级考试【1003 Emergency】 结点之前加入的结点都满足 PAT 甲级考试【1003 Emergency】,所以在 PAT 甲级考试【1003 Emergency】 点加入到 PAT 甲级考试【1003 Emergency】 集合时,有 PAT 甲级考试【1003 Emergency】,此时边 PAT 甲级考试【1003 Emergency】 会被松弛,从而可以证明,将 PAT 甲级考试【1003 Emergency】 加入到 PAT 甲级考试【1003 Emergency】 时,一定有 PAT 甲级考试【1003 Emergency】

下面证明 PAT 甲级考试【1003 Emergency】 成立。在路径 PAT 甲级考试【1003 Emergency】 中,因为图上所有边边权非负,因此 PAT 甲级考试【1003 Emergency】。从而 PAT 甲级考试【1003 Emergency】。但是因为 PAT 甲级考试【1003 Emergency】 结点在 1 过程中被取出 PAT 甲级考试【1003 Emergency】 集合时,PAT 甲级考试【1003 Emergency】 结点还没有被取出 PAT 甲级考试【1003 Emergency】 集合,因此此时有 PAT 甲级考试【1003 Emergency】,从而得到 PAT 甲级考试【1003 Emergency】,这与 PAT 甲级考试【1003 Emergency】 的假设矛盾,故假设不成立。

因此我们证明了,1 操作每次取出的点,其最短路均已经被确定。命题得证。

注意到证明过程中的关键不等式 PAT 甲级考试【1003 Emergency】 是在图上所有边边权非负的情况下得出的。当图上存在负权边时,这一不等式不再成立,Dijkstra 算法的正确性将无法得到保证,算法可能会给出错误的结果。

实现

这里同时给出 PAT 甲级考试【1003 Emergency】 的暴力做法实现和 PAT 甲级考试【1003 Emergency】 的优先队列做法实现

PAT 甲级考试:

-----dfs+dijkstra算法。

  1. 先求出最短路径
  2. 再计算出最短路径数目并计算出路径上点权值的最大team和
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {
    static int n, m;
    static int source, target;

    static int[] number;
    static int[][] matrix = new int[500][500];

    static int[] distance;

    static int shorts;

    static int maxpeople = 0;

    static boolean[] traved;

    static List<Integer>[] adj;

    static int ways = 0;

    @SuppressWarnings("unchecked")
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String[] words = br.readLine().split("\\s+");
        n = Integer.valueOf(words[0]);
        m = Integer.valueOf(words[1]);
        source = Integer.valueOf(words[2]);
        target = Integer.valueOf(words[3]);

        number = new int[n];
        words = br.readLine().split("\\s+");
        for (int i = 0; i < n; i++) {
            number[i] = Integer.valueOf(words[i]);
        }
        adj = new ArrayList[n];
        for (int i = 0; i < n; i++) {
            adj[i] = new ArrayList<>();
        }
        for (int i = 0; i < m; i++) {
            int c1, c2, l;
            words = br.readLine().split("\\s+");
            c1 = Integer.valueOf(words[0]);
            c2 = Integer.valueOf(words[1]);
            l = Integer.valueOf(words[2]);

            matrix[c1][c2] = l;
            matrix[c2][c1] = l;

            adj[c1].add(c2);
            adj[c2].add(c1);
        }
        //bfs;


        distance = new int[n];
        Arrays.fill(distance, Integer.MAX_VALUE / 2);
        distance[source] = 0;

        PriorityQueue<Integer> queue = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return distance[o1] - distance[o2];
            }
        });

        queue.add(source);
        boolean[] visited = new boolean[n];

        while (!queue.isEmpty()) {
            int city = queue.poll();

            if( visited[city]) continue;

            if (distance[city] >= Integer.MAX_VALUE / 2) {
                break;
            }

            visited[city] = true;

            for (int adjcity : adj[city]) {
                if (distance[adjcity] > distance[city] + matrix[city][adjcity]) {
                    distance[adjcity] = distance[city] + matrix[city][adjcity];
                    queue.add(adjcity);
                }
            }
        }

        shorts = distance[target];
        //深度搜索
        traved = new boolean[n];
        traved[source] = true;
        travel(source, 0, number[source], 0);
        System.out.println(allpath.size() + " " + maxpeople);
    }

    static Set<List<Integer>> allpath = new HashSet<>();
    static List<Integer> path = new ArrayList<>();

    static public void travel(int city, int d, int people, int depth) {
        if (city == target && d == shorts) {
            List<Integer> sortedpath = new ArrayList<>();
            for (int x : path) {
                sortedpath.add(x);
            }
            Collections.sort(sortedpath);
            if (!allpath.contains(sortedpath)) {
                ways++;
                allpath.add(sortedpath);
            }

            if (people > maxpeople) {
                maxpeople = people;
            }
            return;
        }

        if (d > shorts) {
            return;
        }

        for (int u : adj[city]) {
            if (!traved[u]) {
                traved[u] = true;
                path.add(u);
                travel(u, d + matrix[city][u], people + number[u], depth + 1);
                traved[u] = false;
                int x = path.remove(depth);
            }
        }

    }
}

 文章来源地址https://www.toymoban.com/news/detail-711391.html

到了这里,关于PAT 甲级考试【1003 Emergency】的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • PAT甲级图论相关题目

    PAT甲级图论相关题目

    PAT甲级图论相关题目: 分数 25 As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some o

    2024年01月21日
    浏览(10)
  • A-2 LRU-K(攀拓(PAT)- 程序设计(甲级)2023年春季考试仿真卷)

    A-2 LRU-K 分数 25 作者 陈越 单位 浙江大学 Least Recently Used (LRU) cache scheme is to remove the least recently used frame (the one hasn\\\'t been used for the longest amount of time) when the cache is full and a new page is referenced which is not there in cache. LRU-K is a variant of the LRU algorithm, where K represents the number of recent uses

    2024年02月08日
    浏览(21)
  • Ubuntu开机显示recovering journal,进入emergency mode

    Ubuntu开机显示recovering journal,进入emergency mode

    在一次正常的shutdown -r now之后,服务器启动不起来了,登录界面显示 recovering journal ,主要报错信息如下所示: 报这个错误多数情况下是因为/etc/fstab文件的错误。注意一下是不是加载了外部硬盘、存储器或者是网络共享空间,在重启时没有加载上导致的。 接下来的操作方式

    2024年02月04日
    浏览(43)
  • PAT 甲级【1010 Radix】

    PAT 甲级【1010 Radix】

    本题范围long型(35)^10 枚举radix范围上限pow(n/a0,1/m)上,考虑上限加1.范围较大。使用二分查找枚举 代码如下 本页面将简要介绍二分查找,由二分法衍生的三分法以及二分答案。 二分查找(英语:binary search),也称折半搜索(英语:half-interval search)、对数搜索(英语:logar

    2024年02月08日
    浏览(11)
  • 【Linux】重启后进入了紧急模式&应急模式(emergency mode)

    【Linux】重启后进入了紧急模式&应急模式(emergency mode)

    将/etc/fstab/挂载/home/参数defaults写错 一般在编辑/etc/fstab后都会去执行mount -a 这里可以看到执行后并未出现错误 那么咱们重启测试一下 可以看到如图所示出现的错误信息 执行重启,重启后在grub界面按e键进入编辑界面 在末行添加init=/bin/bash 进入单用户执行ctrl +X 挂载/分区,可

    2024年02月11日
    浏览(14)
  • Linux系统开机出现 “welcome to emergency mode!”已解决

    Linux系统开机出现 “welcome to emergency mode!”已解决

    1.问题出现原因及描述 在我编写完 /etc/fstab文件之后   当我尝试为linux系统增加一个新的分区时,在永久挂载之后,重启系统发现,进入了如下界面, 出现 \\\"Authorization not available. Check if polkit service is running or see debug message for more informationd\\\" \\\"welcome to emergency mode!\\\" 发现就算输入

    2024年02月05日
    浏览(9)
  • 1028 List Sorting (PAT甲级)

    Excel can sort records according to any column. Now you are supposed to imitate this function. Input Specification: Each input file contains one test case. For each case, the first line contains two integers N (≤105) and C, where N is the number of records and C is the column that you are supposed to sort the records with. Then N lines follow, eac

    2024年02月15日
    浏览(12)
  • 1021 Deepest Root (PAT甲级)

    1021. Deepest Root (25)-PAT甲级真题(图的遍历,dfs,连通分量的个数)_柳婼的博客-CSDN博客 柳婼的解法在这里,两次dfs,还是挺好玩的。 我的解法比较暴力,就是先用并查集算连通分量(这个其实还是dfs来算会更方便),如果只有一个连通分量,那deepest root一定在仅有一条arc的

    2024年02月15日
    浏览(11)
  • 1114 Family Property (PAT甲级)

    This time, you are supposed to help us collect the data for family-owned property. Given each person\\\'s family members, and the estate(房产)info under his/her own name, we need to know the size of each family, and the average area and number of sets of their real estate. Input Specification: Each input file contains one test case. For each case, the fir

    2024年02月06日
    浏览(10)
  • 1072 Gas Station (PAT甲级)

    A gas station has to be built at such a location that the minimum distance between the station and any of the residential housing is as far away as possible. However it must guarantee that all the houses are in its service range. Now given the map of the city and several candidate locations for the gas station, you are supposed to give the best recommendatio

    2024年02月09日
    浏览(14)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包