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
1 import java.util.Scanner; 2 3 public class Main{ 4 static int n,m,c1,c2; 5 static int[] weight = new int[500]; 6 static int[][] map = new int[500][500]; 7 static int minpath = 9999; 8 static int anspath = 0; 9 static int answeights = 0; 10 static boolean [][] visit = new boolean[500][500]; 11 12 public static void main(String[] args){ 13 Scanner sc = new Scanner(System.in); 14 n = sc.nextInt(); 15 m = sc.nextInt(); 16 c1 = sc.nextInt(); 17 c2 = sc.nextInt(); 18 for(int i = 0; i < n; i++){ 19 weight[i] = sc.nextInt(); 20 } 21 for(int i = 0;i < m; i++){ 22 int x = sc.nextInt(); 23 int y = sc.nextInt(); 24 int z = sc.nextInt(); 25 map[x][y] = map[y][x] = z; 26 } 27 dfs(c1, 0, weight[c1]); 28 System.out.printf("%d %d", anspath, answeights); 29 } 30 static void dfs(int start, int path,int weights){ 31 if (start ==c2){ 32 if(path < minpath){ 33 minpath = path; 34 anspath = 1; 35 answeights = weights; 36 }else if(path == minpath){ 37 anspath++; 38 if(weights > answeights){ 39 answeights = weights; 40 } 41 }return; 42 } 43 if(path > minpath) return; 44 for(int i = 0;i<n;i++){ 45 if(visit[start][i] == false && map[start][i] != 0){ 46 visit[start][i] = visit[i][start] = true; 47 dfs(i, path + map[start][i], weights + weight[i]); 48 visit[start][i] = visit[i][start] = false; 49 } 50 } 51 return; 52 } 53 }
本文探讨了在城市间紧急救援场景下,如何利用特殊地图快速规划从当前城市到目标城市的最短路径,并在此过程中尽可能多地集结救援队伍。通过深度优先搜索算法,实现了路径寻找与资源最大化的双重目标。
1051

被折叠的 条评论
为什么被折叠?



