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
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 fromand
输入规格:
每个输入文件包含一个测试用例。 对于每个测试用例,第一行包含4个正整数:N(≤500)-城市数量(并且城市从0到N-1编号),M-道路数量,$ C_1 $和 $ C_2 $ -当前所在的城市和必须援救的城市。
下一行包含N个整数,其中第i个整数是第i个城市中救援队的数量。
然后是M行,每行描述了一条具有三个整数$ c_1 c_2 $ 和L的道路,分别是通过一条道路连接的两个城市和该条道路的长度。 可以保证从$ C_1 C_2 $存在至少一条路径
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.
输出规格:
对于每个测试用例,在一行中打印两个数字:$ C_1 C_2 $之间的最短路的不同数量以及您可能会聚集的最大救援团队数量。 一行中的所有数字必须完全由一个空格分隔,并且在行尾不允许有多余的空格。
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
代码:dfs深度优先搜索
import java.util.Scanner;
/**
* Created by IntelliJ IDEA.
*
* @Author: 张志浩 Zhang Zhihao
* @Email: 3382885270@qq.com
* @Date: 2020/4/29
* @Time: 20:12
* @Version: 1.0
*/
public class Emergency {
static Scanner sc = new Scanner(System.in);
static int N = sc.nextInt(); //城市数量 城市编号从0到N-1
static int M = sc.nextInt(); //道路数量
static int C1 = sc.nextInt(); //当前所在城市的编号
static int C2 = sc.nextInt(); //需要救援的城市的编号
static int[] weights = new int[N];//每个城市的救援队的数量
static int[][] map = new int[N][N]; //城市之间路的长度
//城市间路被访问的状态,要是访问了就更新为true
static boolean[][] visit = new boolean[N][N];
static int minPath = Integer.MAX_VALUE; //刚开始的最小路径的小设为一个很大的值
static int anspath = 0;
static int answeights = 0;
public static void main(String[] args) {
for (int i = 0; i < N; i++) {
weights[i] = sc.nextInt(); //每个城市的救援队的数量
}
for (int i = 0; i < M; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
int z = sc.nextInt();
map[x][y] = map[y][x] = z;
}
sc.close();
dfs(C1, 0, weights[C1]);
System.out.printf("%d %d", anspath, answeights);
}
public static void dfs(int start, int sumPath, int sumWeight) {
if (start == C2) { //到达了终点C2(需要救援的城市的编号)
if (sumPath < minPath) { //如果这条路的长度小于当前最短路的长度
//更新最短路的长度
minPath = sumPath;
//将C1-C2之间的不同的路的数量更新为1
anspath = 1;
//更新总权重,即救援队总数量
answeights = sumWeight;
} else if (sumPath == minPath) { //如果这条路和最短路的长度一样
//将C1-C2之间的不同的路的数量增加1
anspath++;
//如果这条路救援队总数量比之前最短路的总数量大,则更新救援队总数量
if (sumWeight > answeights) {
answeights = sumWeight;
}
}
return;
}
//如果当前路的和的长度大于最小路长度(没到达终点,及时退出)
if (sumPath > minPath) return;
for (int i = 0; i < N; i++) { //以C1点为起点找到到其他各点的路
if (!visit[start][i] && map[start][i] != 0) {
//如果城市start和i之间的路没有被访问(无法保证start不等一i),并且路的长度不为0(保证start不等一i)
visit[start][i] = visit[i][start] = true; //更新访问状态
dfs(i, sumPath + map[start][i], sumWeight + weights[i]); //递归,将起点更新为i点
visit[start][i] = visit[i][start] = false; //更新访问状态,很重要,注意一下
}
}
}
}