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.
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<br/>1 2 1 5 3<br/>0 1 1<br/>0 2 2<br/>0 3 1<br/>1 2 1<br/>2 4 1<br/>3 4 1
2 4
import java.util.*; import java.io.*; public class Main { private static int N, M, C1, C2; private static int[][] graph; private static int[] teams, dist, routes, rescues; private static boolean[] visited; private final static int INF = Integer.MAX_VALUE; public static void main(String[] args) throws IOException { // TODO Auto-generated method stub BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] line = br.readLine().split("\\s+"); N = Integer.parseInt(line[0]); M = Integer.parseInt(line[1]); C1 = Integer.parseInt(line[2]); C2 = Integer.parseInt(line[3]); graph = new int[N][N]; teams = new int[N]; dist = new int[N]; routes = new int[N]; rescues = new int[N]; visited = new boolean[N]; line = br.readLine().split("\\s+"); for (int i = 0; i < N; ++i) { teams[i] = Integer.parseInt(line[i]); } for (int[] row : graph) { Arrays.fill(row, INF); } for (int i = 0; i < M; ++i) { line = br.readLine().split("\\s+"); int v1 = Integer.parseInt(line[0]); int v2 = Integer.parseInt(line[1]); int w = Integer.parseInt(line[2]); graph[v1][v2] = w; graph[v2][v1] = w; } Arrays.fill(dist, INF); dist[C1] = 0; routes[C1] = 1; rescues[C1] = teams[C1]; for (int i = 0; i < N; ++i) { int u = -1, minDist = INF; for (int j = 0; j < N; ++j) { if (!visited[j] && dist[j] < minDist) { u = j; minDist = dist[j]; } } if (u == -1) { break; } visited[u] = true; for (int v = 0; v < N; ++v) { if (!visited[v] && graph[u][v] != INF) { if (dist[u] + graph[u][v] < dist[v]) { dist[v] = dist[u] + graph[u][v]; routes[v] = routes[u]; rescues[v] = rescues[u] + teams[v]; } else if (dist[u] + graph[u][v] == dist[v]) { routes[v] += routes[u]; if (rescues[u] + teams[v] > rescues[v]) { rescues[v] = rescues[u] + teams[v]; } } } } } System.out.format("%d %d\n", routes[C2], rescues[C2]); } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * Dijkstra求source 到各顶点的最短距离/条数/累计人数最多(点权) */ public class Main { // 顶点数 private static int num; // 图 private static int[][] g; // 顶点是否已访问 private static boolean[] v; // 顶点s到各顶点的最短路径长度 private static int[] d; // 顶点s到各顶点的最短路的条数 private static int[] count; // 各顶点人数 private static int[] p; // 顶点s到各顶点的路径累计人数 private static int[] sum; // 顶点s到各顶点的最短路径初始值(极大值) private static int INF = 1_000_000_000; public static void main(String[] args) throws IOException { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); String line1 = bf.readLine(); String[] lineas1 = line1.split(" "); num = Integer.valueOf(lineas1[0]); int roads = Integer.valueOf(lineas1[1]); int source = Integer.valueOf(lineas1[2]); int destination = Integer.valueOf(lineas1[3]); String line2 = bf.readLine(); String[] lineas2 = line2.split(" "); p = new int[num]; // 初始化各顶点人数 for (int i = 0; i < num; i++) { p[i] = Integer.valueOf(lineas2[i]); } // 初始化顶点s到各顶点的路径累计人数 sum = new int[num]; sum[source] = p[source]; // 初始化图 g = new int[num][num]; while (roads-- > 0) { String line = bf.readLine(); String[] lineas = line.split(" "); int i = Integer.valueOf(lineas[0]); int j = Integer.valueOf(lineas[1]); int length = Integer.valueOf(lineas[2]); g[i][j] = length; g[j][i] = length; } bf.close(); // 初始化顶点source到各顶点的路径 d = new int[num]; count = new int[num]; for (int i = 0; i < num; i++) { if (i == source) { d[i] = 0; count[i] = 1; } else { d[i] = INF; } } v = new boolean[num]; // Dijsktra int nums = num; while (nums-- > 0) { int u = minAndNotVisited(d, v); if (u == -1){ break; } v[u] = true; // 更新从source出发经u到u相连的顶点v的距离/最短路径条数/累计人数 for (int j = 0; j < num; j++) { if (g[u][j] > 0 && !v[j]) { if(d[u] + g[u][j] < d[j]) { d[j] = d[u] + g[u][j]; count[j] = count[u]; sum[j] = sum[u] + p[j]; } else if (d[u] + g[u][j] == d[j]) { count[j] = count[u] + count[j]; if((sum[u] + p[j])>sum[j]) { sum[j] = sum[u] + p[j]; } } } } } System.out.print(count[destination]+" "+sum[destination]); } /** * 获取未访问过且离source最近的顶点u,返回-1说明找不到和source相连接的且未访问过的顶点 */ private static int minAndNotVisited(int[] d, boolean[] v) { int u = -1; int min = INF; for (int i = 0; i < num; i++) { if (d[i] < min && !v[i]) { u = i; min = d[i]; } } return u; } }