Emergency(单源最短路径问题)

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 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, C​1​​ and C​2​​ - 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 c​1​​, c​2​​ 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 C​1​​ to C​2​​.

Output Specification:

For each test case, print in one line two numbers: the number of different shortest paths between C​1​​ and C​2​​, 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、在单源最短路径问题的基础上,增加了节点的权重这一项,即每个城市的救援队数量,要求最短路径的基础上,节点的权重之和越大越好。解决方法就是,新增一个N大小的数组dist_vertex[ ],表示此节点到源点的最短路径上所有节点的权重之和。这个数组随着dist[ ]的更新而更新。

2、还需要求解最短路径的数量,以前没求过,思路就是把path[ ]数组扩展一下,每个节点存一个容器,当更新节点到源点的距离时,如果要更新dist数组,则同时更新path数组,先clear容器,然后增加元素。如果遇到dist[i] = dist[V]+G[V][i]的情况,则直接add元素,不要清空,想想为啥。(因为要记录所有的最短路径)

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.ArrayList;
import java.util.LinkedList;

public class Main {

	static int N; // 节点数量
	static int M; // 边的数量
	static int S; // 起点
	static int D; // 终点
	static int num_of_shortest = 1; // 最短路径的数量
	static int num_of_teams = 0; //
	static int G[][]; // 邻接矩阵
	static int P[]; // 点的权重
	static int dist[]; // 每个点到起点的最短路径的距离
	static int dist_vertex[]; // 每个点到起点的最短路径上,点的权重
	static int path[]; // 每个点到起点的最短路径所要经过的点
	static boolean collected[]; // 判断顶点是否收录
//	static boolean pass[];				//记录每个点到起点是否存在多条路径
	static Box[] pathbox ;
	

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

		in.nextToken();
		N = (int) in.nval;
		in.nextToken();
		M = (int) in.nval;
		in.nextToken();
		S = (int) in.nval;
		in.nextToken();
		D = (int) in.nval;

		G = new int[N][N];
		P = new int[N];
		dist = new int[N];
		dist_vertex = new int[N];
		path = new int[N];
		collected = new boolean[N];
//		pass = new boolean[N];
		pathbox = new Box[N];

		for (int i = 0; i < N; i++) {
			in.nextToken();
			P[i] = (int) in.nval;
			pathbox[i] = new Box();
		}

		for (int i = 0; i < N; i++)
			for (int j = 0; j < N; j++) {
				G[i][j] = Integer.MAX_VALUE / 10;
			}
		for (int i = 0; i < M; i++) {
			in.nextToken();
			int m = (int) in.nval;
			in.nextToken();
			int n = (int) in.nval;
			in.nextToken();
			int length = (int) in.nval;
			G[m][n] = length;
			G[n][m] = length;
		}

		for (int i = 0; i < N; i++) {
			dist[i] = Integer.MAX_VALUE / 10;
			path[i] = -1;
		}
		dist[S] = 0;
		dist_vertex[S] = P[S];

		while (true) {
			int V = min_dist();
			if (V == -1)
				break;
			collected[V] = true;
			for (int i = 0; i < N; i++) {
				if (G[V][i] != Integer.MAX_VALUE/10 && collected[i] == false) {
					if (dist[i] > dist[V] + G[V][i]) {
						dist[i] = dist[V] + G[V][i];
						path[i] = V;
						pathbox[i].clear();
						pathbox[i].add(V);
						dist_vertex[i] = dist_vertex[V] + P[i];

					} else if (dist[i] == dist[V] + G[V][i]) {
						pathbox[i].add(V);
						if (dist_vertex[i] < dist_vertex[V] + P[i]) {
							path[i] = V;
							dist_vertex[i] = dist_vertex[V] + P[i];
						}
					}

				}
			}

		}

		num_of_shortest =digui(D);    //递归求解此节点到源点的所有最短路径
                                       //思路就是这个点的上一些经过的点的最短路径求和。
		System.out.println(num_of_shortest + " " + dist_vertex[D]);

	}


	private static int digui(int d) {
		// TODO Auto-generated method stub
		int temp =0;
		if(d == S)
			return 1;
		if(pathbox[d].size()==1) {
			return digui(pathbox[d].get(0));
		}
		else {
			for(int i=0;i<pathbox[d].size();i++) {
				temp+=digui(pathbox[d].get(i));
			}
			return temp;
		}
	}


	private static int min_dist() {
		// TODO Auto-generated method stub
		int temp = Integer.MAX_VALUE/10;
		int num = -1;
		for (int i = 0; i < N; i++) {
			if (collected[i] == false && dist[i] < temp) {
				temp = dist[i];
				num = i;
			}
		}
		return num;
	}
}

class Box{
	private LinkedList<Integer> al = new LinkedList<>();
	public void add(int num) {
		al.add(num);
	}
	public void clear() {
		al.clear();
	}
	public int get(int i) {
		return al.get(i);
	}
	public int size() {
		return al.size();
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值