PAT-1003-最短路径问题

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 c1 , 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 C1 to C2.

Output Specification:
For each test case, print in one line two numbers: the number of different shortest paths between C1 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

谷歌中文翻译:
作为城市的紧急救援团队负责人,您将获得一张您所在国家的特殊地图。该地图显示了一些通过道路连接的分散城市。地图上标出了每个城市的救援队数量以及每对城市之间的每条道路的长度。当从其他城市接到您的紧急电话时,您的工作是尽快带领您的士兵前往该地点,同时,在途中尽可能多地动手。

输入规格:
每个输入文件包含一个测试用例。对于每个测试用例,第一行包含4个正整数:N(≤500)-城市数量(并且城市从0到N-1编号),M-道路数量,C 1和C 2 -当前所在的城市和必须保存的城市。下一行包含N个整数,其中第i个整数是第i个城市中救援队的数量。然后是M条线,每条线描述了一条具有三个整数c1,c2和L的道路,它们分别是由一条道路和该条道路的长度相连的一对城市。确保从C1到C2至少存在一条路径。

输出规格:
对于每个测试用例,在一行中打印两个数字:C1和C2之间不同的最短路径数量以及可以收集的最大救援团队数量。一行中的所有数字必须完全由一个空格分隔,并且在行尾不允许有多余的空格。

解题思路:
首先是dijkstra的基础,可能会出现多条相同路径长度的最短路径,需要一个数组来记录可能存在的数量;同时在原先只考虑边权的基础上加入点权的思考,需要考虑路径相同时选择点权累计和最大的记录。
掌握好dijkstra模板才是做题的关键,需要灵活变动。

#include<bits/stdc++.h>
using namespace std;

const int INF = 100000000;
const int MAX_N = 510;				

int N; // 城市数量
int M; // 道路数量
int s, d; // 源地点 目的地点 

int e[MAX_N][MAX_N]; // 两城市间边权  
bool vis[MAX_N]; // 该城市是否被访问过 
int dis[MAX_N]; // 当前更新完的路径长度 
int num[MAX_N]; // 记录多少条路径长度相同的最短路径 
int w[MAX_N];  //  记录到各点的所用的医疗资源之和 
int weight[MAX_N]; // 各城市拥有的医疗资源(点权) 

void input() {
	cin >> N >> M >> s >> d;
	for(int i = 0; i < N; i++) {
		cin >> weight[i];
	}
	fill(e[0], e[0] + MAX_N * MAX_N, INF);
	fill(dis, dis + MAX_N, INF);
	int x, y, z; // 输入起始城市,结束城市及其路径权值
	for(int i = 0; i < M; i++) {
		cin >> x >> y >> z;
		//无向边 
		e[x][y] = z;
		e[y][x] = z;
	} 
}

void dijkstra(int s, int d) {
	//第一步,先将起点记录并将相关参数写入 
	dis[s] = 0;
	w[s] = weight[s];
	num[s] = 1;
	//第二步,核心代码-遍历 
	for(int i = 0; i < N; i++) { //此处改成while(true)也可以,关键是在遍历 
		int u = -1; //标记 
		int minn = INF; //初始化最大值 
		//找出还没遍历的点中距离最小的 
		for(int j = 0; j < N; j++) {
			if(vis[j] == false && dis[j] < minn) {
				u = j;
				minn = dis[j]; 
			}
		}
		if(u == -1) break; 
		vis[u] = true; 
		//将每个点进行更新
		for(int v = 0; v < N; v++) {
			if(vis[v] == false && e[u][v] != INF) {
				if(dis[u] + e[u][v] < dis[v]) {
					//更新相应点的最短路径
					dis[v] = dis[u] + e[u][v];
					num[v] = num[u];
					w[v] = w[u] + weight[v]; 
				} else if(dis[u] + e[u][v] == dis[v]) {
					//出现相同长度的路径时的处理
					num[v] = num[v] + num[u];
					if(w[u] + weight[v] > w[v]) {
						w[v] = w[u] + weight[v];
					} 
				}
			}
		} 
	}
	cout << num[d] << " " << w[d]; 
}

int main() {
	input();
	dijkstra(s, d); 
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值