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.
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 1Sample Output
2 4
图论刚开始接触,还需要进一步了解。
在此使用简单的DFS(深度优先搜索)即可完成所需要求
import java.util.Scanner;
public class Main {
private static int start = 0;
private static int end = 0;
private static int n = 0;//the number of cities
private static int m = 0;//the number of roads
private static int[] visited = new int[501];
private static int[] teams = new int[501];
private static int[][] roads = new int[501][501];
private static int min_distance = Integer.MAX_VALUE;
private static int max_teams = 0;
private static int count = 0;
public static void dfs(int start, int end, int distance, int team_num){
if(start == end){
if(distance < min_distance){
count = 1;
min_distance = distance;
max_teams = team_num;
}
else if(distance == min_distance){
count++;
if(team_num > max_teams){
max_teams = team_num;
}
}
}
if(distance > min_distance)return;//在最后的测试中会出现运行超时问题,在此增加剪枝,参考他人
visited[start] = 1;//加在这里比较好,把起点设为已遍历
for(int i=0; i<n; i++){
if(visited[i] == 0 && roads[start][i] != 0){
//visited[i] = 1;
dfs(i, end, distance+roads[start][i], team_num+teams[i]);
visited[i] = 0;
}
}
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
n = s.nextInt();
m = s.nextInt();
start = s.nextInt();
end = s.nextInt();
for(int i=0; i<n; i++){
teams[i] = s.nextInt();
}
for(int i=0; i<m; i++){
int x = s.nextInt();
int y = s.nextInt();
int length = s.nextInt();
roads[x][y] = length;
roads[y][x] = length;
}
dfs(start, end, 0, teams[start]);
System.out.printf("%d %d", count, max_teams);
}
}
main方法调用成员变量和方法时,应注意相关的static的定义问题