国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

PAT 甲級(jí)考試【1003 Emergency】

這篇具有很好參考價(jià)值的文章主要介紹了PAT 甲級(jí)考試【1003 Emergency】。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

題目:

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?N?1),?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?c1?,?c2??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ɑ/)算法由荷蘭計(jì)算機(jī)科學(xué)家 E. W. Dijkstra 于 1956 年發(fā)現(xiàn),1959 年公開發(fā)表。是一種求解?非負(fù)權(quán)圖?上單源最短路徑的算法。

過(guò)程

將結(jié)點(diǎn)分成兩個(gè)集合:已確定最短路長(zhǎng)度的點(diǎn)集(記為?PAT 甲級(jí)考試【1003 Emergency】?集合)的和未確定最短路長(zhǎng)度的點(diǎn)集(記為?PAT 甲級(jí)考試【1003 Emergency】?集合)。一開始所有的點(diǎn)都屬于?PAT 甲級(jí)考試【1003 Emergency】?集合。

初始化?PAT 甲級(jí)考試【1003 Emergency】,其他點(diǎn)的?PAT 甲級(jí)考試【1003 Emergency】?均為?PAT 甲級(jí)考試【1003 Emergency】。

然后重復(fù)這些操作:

  1. 從?PAT 甲級(jí)考試【1003 Emergency】?集合中,選取一個(gè)最短路長(zhǎng)度最小的結(jié)點(diǎn),移到?PAT 甲級(jí)考試【1003 Emergency】?集合中。
  2. 對(duì)那些剛剛被加入?PAT 甲級(jí)考試【1003 Emergency】?集合的結(jié)點(diǎn)的所有出邊執(zhí)行松弛操作。

直到?PAT 甲級(jí)考試【1003 Emergency】?集合為空,算法結(jié)束。

時(shí)間復(fù)雜度

有多種方法來(lái)維護(hù) 1 操作中最短路長(zhǎng)度最小的結(jié)點(diǎn),不同的實(shí)現(xiàn)導(dǎo)致了 Dijkstra 算法時(shí)間復(fù)雜度上的差異。

  • 暴力:不使用任何數(shù)據(jù)結(jié)構(gòu)進(jìn)行維護(hù),每次 2 操作執(zhí)行完畢后,直接在?PAT 甲級(jí)考試【1003 Emergency】?集合中暴力尋找最短路長(zhǎng)度最小的結(jié)點(diǎn)。2 操作總時(shí)間復(fù)雜度為?PAT 甲級(jí)考試【1003 Emergency】,1 操作總時(shí)間復(fù)雜度為?PAT 甲級(jí)考試【1003 Emergency】,全過(guò)程的時(shí)間復(fù)雜度為?PAT 甲級(jí)考試【1003 Emergency】
  • 二叉堆:每成功松弛一條邊?PAT 甲級(jí)考試【1003 Emergency】,就將?PAT 甲級(jí)考試【1003 Emergency】?插入二叉堆中(如果?PAT 甲級(jí)考試【1003 Emergency】?已經(jīng)在二叉堆中,直接修改相應(yīng)元素的權(quán)值即可),1 操作直接取堆頂結(jié)點(diǎn)即可。共計(jì)?PAT 甲級(jí)考試【1003 Emergency】?次二叉堆上的插入(修改)操作,PAT 甲級(jí)考試【1003 Emergency】?次刪除堆頂操作,而插入(修改)和刪除的時(shí)間復(fù)雜度均為?PAT 甲級(jí)考試【1003 Emergency】,時(shí)間復(fù)雜度為?PAT 甲級(jí)考試【1003 Emergency】。
  • 優(yōu)先隊(duì)列:和二叉堆類似,但使用優(yōu)先隊(duì)列時(shí),如果同一個(gè)點(diǎn)的最短路被更新多次,因?yàn)橄惹案聲r(shí)插入的元素不能被刪除,也不能被修改,只能留在優(yōu)先隊(duì)列中,故優(yōu)先隊(duì)列內(nèi)的元素個(gè)數(shù)是?PAT 甲級(jí)考試【1003 Emergency】?的,時(shí)間復(fù)雜度為?PAT 甲級(jí)考試【1003 Emergency】。
  • Fibonacci 堆:和前面二者類似,但 Fibonacci 堆插入的時(shí)間復(fù)雜度為?PAT 甲級(jí)考試【1003 Emergency】,故時(shí)間復(fù)雜度為?PAT 甲級(jí)考試【1003 Emergency】,時(shí)間復(fù)雜度最優(yōu)。但因?yàn)?Fibonacci 堆較二叉堆不易實(shí)現(xiàn),效率優(yōu)勢(shì)也不夠大1,算法競(jìng)賽中較少使用。
  • 線段樹:和二叉堆原理類似,不過(guò)將每次成功松弛后插入二叉堆的操作改為在線段樹上執(zhí)行單點(diǎn)修改,而 1 操作則是線段樹上的全局查詢最小值。時(shí)間復(fù)雜度為?PAT 甲級(jí)考試【1003 Emergency】。

在稀疏圖中,PAT 甲級(jí)考試【1003 Emergency】,使用二叉堆實(shí)現(xiàn)的 Dijkstra 算法較 Bellman–Ford 算法具有較大的效率優(yōu)勢(shì);而在稠密圖中,PAT 甲級(jí)考試【1003 Emergency】,這時(shí)候使用暴力做法較二叉堆實(shí)現(xiàn)更優(yōu)。

正確性證明

下面用數(shù)學(xué)歸納法證明,在?所有邊權(quán)值非負(fù)?的前提下,Dijkstra 算法的正確性2。

簡(jiǎn)單來(lái)說(shuō),我們要證明的,就是在執(zhí)行 1 操作時(shí),取出的結(jié)點(diǎn)?PAT 甲級(jí)考試【1003 Emergency】?最短路均已經(jīng)被確定,即滿足?PAT 甲級(jí)考試【1003 Emergency】。

初始時(shí)?PAT 甲級(jí)考試【1003 Emergency】,假設(shè)成立。

接下來(lái)用反證法。

設(shè)?PAT 甲級(jí)考試【1003 Emergency】?點(diǎn)為算法中第一個(gè)在加入?PAT 甲級(jí)考試【1003 Emergency】?集合時(shí)不滿足?PAT 甲級(jí)考試【1003 Emergency】?的點(diǎn)。因?yàn)?PAT 甲級(jí)考試【1003 Emergency】?點(diǎn)一定滿足?PAT 甲級(jí)考試【1003 Emergency】,且它一定是第一個(gè)加入?PAT 甲級(jí)考試【1003 Emergency】?集合的點(diǎn),因此將?PAT 甲級(jí)考試【1003 Emergency】?加入?PAT 甲級(jí)考試【1003 Emergency】?集合前,PAT 甲級(jí)考試【1003 Emergency】,如果不存在?PAT 甲級(jí)考試【1003 Emergency】?到?PAT 甲級(jí)考試【1003 Emergency】?的路徑,則?PAT 甲級(jí)考試【1003 Emergency】,與假設(shè)矛盾。

于是一定存在路徑?PAT 甲級(jí)考試【1003 Emergency】,其中?PAT 甲級(jí)考試【1003 Emergency】?為?PAT 甲級(jí)考試【1003 Emergency】?路徑上第一個(gè)屬于?PAT 甲級(jí)考試【1003 Emergency】?集合的點(diǎn),而?PAT 甲級(jí)考試【1003 Emergency】?為?PAT 甲級(jí)考試【1003 Emergency】?的前驅(qū)結(jié)點(diǎn)(顯然?PAT 甲級(jí)考試【1003 Emergency】)。需要注意的是,可能存在?PAT 甲級(jí)考試【1003 Emergency】?或?PAT 甲級(jí)考試【1003 Emergency】?的情況,即?PAT 甲級(jí)考試【1003 Emergency】?或?PAT 甲級(jí)考試【1003 Emergency】?可能是空路徑。

因?yàn)樵?PAT 甲級(jí)考試【1003 Emergency】?結(jié)點(diǎn)之前加入的結(jié)點(diǎn)都滿足?PAT 甲級(jí)考試【1003 Emergency】,所以在?PAT 甲級(jí)考試【1003 Emergency】?點(diǎn)加入到?PAT 甲級(jí)考試【1003 Emergency】?集合時(shí),有?PAT 甲級(jí)考試【1003 Emergency】,此時(shí)邊?PAT 甲級(jí)考試【1003 Emergency】?會(huì)被松弛,從而可以證明,將?PAT 甲級(jí)考試【1003 Emergency】?加入到?PAT 甲級(jí)考試【1003 Emergency】?時(shí),一定有?PAT 甲級(jí)考試【1003 Emergency】。

下面證明?PAT 甲級(jí)考試【1003 Emergency】?成立。在路徑?PAT 甲級(jí)考試【1003 Emergency】?中,因?yàn)閳D上所有邊邊權(quán)非負(fù),因此?PAT 甲級(jí)考試【1003 Emergency】。從而?PAT 甲級(jí)考試【1003 Emergency】。但是因?yàn)?PAT 甲級(jí)考試【1003 Emergency】?結(jié)點(diǎn)在 1 過(guò)程中被取出?PAT 甲級(jí)考試【1003 Emergency】?集合時(shí),PAT 甲級(jí)考試【1003 Emergency】?結(jié)點(diǎn)還沒有被取出?PAT 甲級(jí)考試【1003 Emergency】?集合,因此此時(shí)有?PAT 甲級(jí)考試【1003 Emergency】,從而得到?PAT 甲級(jí)考試【1003 Emergency】,這與?PAT 甲級(jí)考試【1003 Emergency】?的假設(shè)矛盾,故假設(shè)不成立。

因此我們證明了,1 操作每次取出的點(diǎn),其最短路均已經(jīng)被確定。命題得證。

注意到證明過(guò)程中的關(guān)鍵不等式?PAT 甲級(jí)考試【1003 Emergency】?是在圖上所有邊邊權(quán)非負(fù)的情況下得出的。當(dāng)圖上存在負(fù)權(quán)邊時(shí),這一不等式不再成立,Dijkstra 算法的正確性將無(wú)法得到保證,算法可能會(huì)給出錯(cuò)誤的結(jié)果。

實(shí)現(xiàn)

這里同時(shí)給出?PAT 甲級(jí)考試【1003 Emergency】?的暴力做法實(shí)現(xiàn)和?PAT 甲級(jí)考試【1003 Emergency】?的優(yōu)先隊(duì)列做法實(shí)現(xiàn)

PAT 甲級(jí)考試:

-----dfs+dijkstra算法。

  1. 先求出最短路徑
  2. 再計(jì)算出最短路徑數(shù)目并計(jì)算出路徑上點(diǎn)權(quán)值的最大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);
            }
        }

    }
}

?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-711391.html

到了這里,關(guān)于PAT 甲級(jí)考試【1003 Emergency】的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • PAT甲級(jí)圖論相關(guān)題目

    PAT甲級(jí)圖論相關(guān)題目

    PAT甲級(jí)圖論相關(guān)題目: 分?jǐn)?shù) 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日
    瀏覽(20)
  • A-2 LRU-K(攀拓(PAT)- 程序設(shè)計(jì)(甲級(jí))2023年春季考試仿真卷)

    A-2 LRU-K 分?jǐn)?shù) 25 作者 陳越 單位 浙江大學(xué) 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日
    瀏覽(40)
  • Ubuntu開機(jī)顯示recovering journal,進(jìn)入emergency mode

    Ubuntu開機(jī)顯示recovering journal,進(jìn)入emergency mode

    在一次正常的shutdown -r now之后,服務(wù)器啟動(dòng)不起來(lái)了,登錄界面顯示 recovering journal ,主要報(bào)錯(cuò)信息如下所示: 報(bào)這個(gè)錯(cuò)誤多數(shù)情況下是因?yàn)?etc/fstab文件的錯(cuò)誤。注意一下是不是加載了外部硬盤、存儲(chǔ)器或者是網(wǎng)絡(luò)共享空間,在重啟時(shí)沒有加載上導(dǎo)致的。 接下來(lái)的操作方式

    2024年02月04日
    瀏覽(87)
  • PAT 甲級(jí)【1010 Radix】

    PAT 甲級(jí)【1010 Radix】

    本題范圍long型(35)^10 枚舉radix范圍上限pow(n/a0,1/m)上,考慮上限加1.范圍較大。使用二分查找枚舉 代碼如下 本頁(yè)面將簡(jiǎn)要介紹二分查找,由二分法衍生的三分法以及二分答案。 二分查找(英語(yǔ):binary search),也稱折半搜索(英語(yǔ):half-interval search)、對(duì)數(shù)搜索(英語(yǔ):logar

    2024年02月08日
    瀏覽(20)
  • Linux系統(tǒng)開機(jī)出現(xiàn) “welcome to emergency mode!”已解決

    Linux系統(tǒng)開機(jī)出現(xiàn) “welcome to emergency mode!”已解決

    1.問題出現(xiàn)原因及描述 在我編寫完 /etc/fstab文件之后 ? 當(dāng)我嘗試為linux系統(tǒng)增加一個(gè)新的分區(qū)時(shí),在永久掛載之后,重啟系統(tǒng)發(fā)現(xiàn),進(jìn)入了如下界面, 出現(xiàn) \\\"Authorization not available. Check if polkit service is running or see debug message for more informationd\\\" \\\"welcome to emergency mode!\\\" 發(fā)現(xiàn)就算輸入

    2024年02月05日
    瀏覽(16)
  • 【Linux】重啟后進(jìn)入了緊急模式&應(yīng)急模式(emergency mode)

    【Linux】重啟后進(jìn)入了緊急模式&應(yīng)急模式(emergency mode)

    將/etc/fstab/掛載/home/參數(shù)defaults寫錯(cuò) 一般在編輯/etc/fstab后都會(huì)去執(zhí)行mount -a 這里可以看到執(zhí)行后并未出現(xiàn)錯(cuò)誤 那么咱們重啟測(cè)試一下 可以看到如圖所示出現(xiàn)的錯(cuò)誤信息 執(zhí)行重啟,重啟后在grub界面按e鍵進(jìn)入編輯界面 在末行添加init=/bin/bash 進(jìn)入單用戶執(zhí)行ctrl +X 掛載/分區(qū),可

    2024年02月11日
    瀏覽(26)
  • pat甲級(jí) 1022 Digital Library

    A Digital Library contains millions of books, stored according to their titles, authors, key words of their abstracts, publishers, and published years. Each book is assigned an unique 7-digit number as its ID. Given any query from a reader, you are supposed to output the resulting books, sorted in increasing order of their ID\\\'s. Input Specification: Each inp

    2024年04月15日
    瀏覽(23)
  • 1028 List Sorting (PAT甲級(jí))

    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日
    瀏覽(16)
  • 1111 Online Map (PAT甲級(jí))

    這道題我讀題不仔細(xì)導(dǎo)致踩了個(gè)大坑,一個(gè)測(cè)試點(diǎn)過(guò)不了卡了好幾個(gè)小時(shí):第二個(gè)dijkstra算法中,題目要求是“In case the fastest path is not unique, output the one that passes through the fewest intersections”,我卻想當(dāng)然地認(rèn)為在fastest path is not unique的時(shí)候,判斷標(biāo)準(zhǔn)是最短距離…… Input our

    2024年02月07日
    瀏覽(15)
  • 1021 Deepest Root (PAT甲級(jí))

    1021. Deepest Root (25)-PAT甲級(jí)真題(圖的遍歷,dfs,連通分量的個(gè)數(shù))_柳婼的博客-CSDN博客 柳婼的解法在這里,兩次dfs,還是挺好玩的。 我的解法比較暴力,就是先用并查集算連通分量(這個(gè)其實(shí)還是dfs來(lái)算會(huì)更方便),如果只有一個(gè)連通分量,那deepest root一定在僅有一條arc的

    2024年02月15日
    瀏覽(16)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包