首页 > 试题广场 >

Emergency (25)

[编程题]Emergency (25)
  • 热度指数:5164 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
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.

输入描述:
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.
示例1

输入

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
java版。Dijkstra求source 到各顶点的最短距离/条数/累计人数最多(点权)
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;
	}
}


发表于 2019-10-23 12:30:59 回复(0)