(Java版)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, 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​.

每个输入文件包含一个测试用例。对于每个测试用例,第一行包含4个正整数:N(≤500)-城市数量(城市编号从0到N−1),M-道路数量,C1和C
2-您当前所在的城市和您必须保存的城市。下一行包含N个整数,其中第i个整数是第i个城市的救援队数量。接下来是M条线,每条线用三个整数c1、c2和L描述一条道路,这三个整数分别是由一条道路连接的一对城市和该道路的长度。保证存在至少一条从C1到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.

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

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

思路:深度优先搜索,每次相互独立的找最短路径 

import java.util.Scanner;

class Main{
    static int n; // 城市数量
    static int m;//道路数量
    static int root ;//起始位置
    static int end ;//终止位置,即所需要救援的城市
    static int [][]graph;//存储无向图
    static int []weight;//记录每个城市的救援队数量
    static int [][]visited;//记录每个城市是否访问过
    static int min_path = Integer.MAX_VALUE;//记录最短路径值
    static int path_count;//记录最短路径值一样,线路不一样的路径数量
    static int weight_count;//最终的救援队的数量(最大)
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        m = sc.nextInt();
        root = sc.nextInt();
        end = sc.nextInt();
        graph = new int[n][n];
        weight = new int[n];
        visited = new int[n][n];
        for (int i = 0; i < n; i++) {
            weight[i] = sc.nextInt();

        }
        for (int i = 0; i < m; i++) {
            int x = sc.nextInt();
            int y = sc.nextInt();
            int weight = sc.nextInt();
            graph[x][y] = graph[y][x] = weight;
        }
        dfs(root,0,weight[root]);
        System.out.println(path_count + " " + weight_count);
    }

    private static void dfs(int start, int sumpath, int sum_weight) {
        if( start == end)//如果已经到达终点
        {
            if(sumpath < min_path)//更新最短路径值
            {
                min_path = sumpath;
                path_count = 1;//数量一定要重置为1;
                weight_count = sum_weight;
            }
            else if(sumpath == min_path)//如果存在多条路
            {
                path_count++;
                if(sum_weight > weight_count)//更新最大救援队数量
                {
                    weight_count = sum_weight;
                }
            }
            return ;
        }
        if(sumpath > min_path) return ;//如果当前路径的值大于最小路径,没必要继续往下进行
        for(int i = 0 ; i < n ;i++)//遍历图
        {
            if(visited[start][i] == 0 && graph[start][i] != 0)//没访问过的并且有路存在
            {
                visited[start][i] = visited[i][start] = 1;//标记为访问过的
                dfs(i,sumpath + graph[start][i],sum_weight + weight[i]);//递归
                visited[start][i] = visited[i][start] = 0;//回溯,非常重要!!!!一定要回溯,因为每次遍历是相互独立的找,所以一定要回溯
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值