PAT甲级JAVA版 1003 Emergency(25 分)

1003 Emergency (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 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, and - 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 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 to .

Output Specification:

For each test case, print in one line two numbers: the number of different shortest paths between and , 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

1003 紧急情况 (25 分)

作为一个城市的紧急救援队队长,你会得到一张你国家的特殊地图。这张地图显示了几个分散的城市与一些道路相连。每个城市的救援队数量和每一对城市之间每条道路的长度在地图上都有标记。当有来自其他城市的紧急电话给你时,你的工作就是尽快地把你的人带到那个地方,同时,在路上获得尽可能多的救援队。

输入规格

每个输入文件包含一个测试用例。对于每个测试用例,第一行包含4个正整数:N(≤500)-城市数量(城市编号为0到N−1),M-道路数,和--分别是您当前所在的城市和必须拯救的的城市。下一行包含N个整数,其中第个整数是第城市的救援队数目。接下来有M行,每行用三个整数、和L描述一条道路,三个整数分别是那条道路相连的一对城市编号和那条道路的长度。数据保证了至少有一个路径能从。

输出规格

对于每个测试用例,在一行中打印两个数字:在和之间的不同最短路径的数目,以及您可能收集到的最大救援队数量。一行中的所有数字都必须用一个空格隔开,在一行的末尾不允许有额外的空格。

输入样例

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

输出样例

2 4

代码

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    private static Scanner sc;

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        sc = new Scanner(System.in);
        final int inf = 99999999;
        int n, m, c1, c2;
        int[] dis = new int[510];
        Arrays.fill(dis, inf);

        int[][] e = new int[510][510];
        // Arrays.fill(e, dis);
        for (int i = 0; i < e.length; i++) {
            for (int j = 0; j < e.length; j++) {
                e[i][j] = inf;
            }
        }
        int[] weight = new int[510];
        int[] num = new int[510];
        int[] w = new int[510];
        boolean[] visit = new boolean[510];
        Arrays.fill(visit, false);
        n = sc.nextInt();
        m = sc.nextInt();
        c1 = sc.nextInt();
        c2 = sc.nextInt();
        for (int i = 0; i < n; i++) {
            weight[i] = sc.nextInt();
        }

        int a, b, c;
        for (int i = 0; i < m; i++) {
            a = sc.nextInt();
            b = sc.nextInt();
            c = sc.nextInt();
            e[a][b] = e[b][a] = c;
        }
        sc.close();
        dis[c1] = 0;
        w[c1] = weight[c1];
        num[c1] = 1;

        for (int i = 0; i < n; i++) {
            int u = -1, min = inf;
            for (int j = 0; j < n; j++) {
                if (visit[j] == false && dis[j] < min) {
                    u = j;
                    min = dis[j];
                }
            }
            if (u == -1)
                break;
            visit[u] = true;
            for (int v = 0; v < n; v++) {
                if (visit[v] == false && e[u][v] != inf) {
                    if (dis[u] + e[u][v] < dis[v]) {
                        dis[v] = dis[u] + e[u][v];
                        num[v] = num[u];
                        w[v] = w[u] + weight[v];
                    } else if (dis[u] + e[u][v] == dis[v]) {
                        num[v] = num[v] + num[u];
                        if (w[u] + weight[v] > w[v])
                            w[v] = w[u] + weight[v];
                    }
                }
            }
        }

        System.out.print(num[c2] + " " + w[c2]);
    }
}

你可能感兴趣的:(PAT甲级JAVA版 1003 Emergency(25 分))