1003. Emergency (25)

Posted 来一点音乐

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

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

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
DFS
开始两个测试点没过发现:
1:若1城市跟2城市之间有路则2城市与1城市之间也有路
2:运行超时,判断路径不是最短就跳出即可。

 1 import java.util.Scanner;
 2 
 3 
 4 public class Main {
 5     static int n,m,c1,c2;
 6     static int[] a;
 7     static int[][] b;
 8     static int mix = Integer.MAX_VALUE;
 9     static int max;
10     static int sum3;
11     static boolean[] flag;
12     public static void main(String[] args) {
13         Scanner input = new Scanner(System.in);
14         n = input.nextInt();
15         m = input.nextInt();
16         c1 = input.nextInt();
17         c2 = input.nextInt();
18         a = new int[n];
19         for(int i=0;i<n;i++){
20             a[i] = input.nextInt();
21         }
22         b = new int[n][n];
23         for(int i=0;i<m;i++){
24             int a1 = input.nextInt();
25             int a2 = input.nextInt();
26             int a3 = input.nextInt();
27             b[a1][a2] = a3;//路径
28             b[a2][a1] = a3;//路径
29         }
30         flag = new boolean[n];
31         f(c1,0,a[c1]);
32         System.out.println(sum3+" "+max);
33     }
34     public static void f(int i,int sum,int sum1){
35         if(sum>mix){
36             return;//不是最短的跳出
37         }
38         if(i==c2){
39             if(sum<mix){
40                 mix = sum;
41                 sum3 = 1;
42                 max = sum1;
43             }else if(sum==mix){
44                 sum3++;
45                 if(max<sum1){
46                     max = sum1;
47                 }
48             }
49             return ;
50         }
51         flag[i] = true;
52         for(int j=0;j<n;j++){
53             if(b[i][j]!=0&&!flag[j]){
54                 f(j,sum+b[i][j],sum1+a[j]);
55             }
56         }
57         flag[i] = false;
58     }
59 
60 }

 



以上是关于1003. Emergency (25)的主要内容,如果未能解决你的问题,请参考以下文章

1003 Emergency (25)(25 point(s))

1003 Emergency (25 分)

PAT甲级1003 Emergency (25分)

1003. Emergency (25)

1003 Emergency(25 分)

1003. Emergency (25)