[PAT-Advanced]A1003 Emergency (25)

总题解目录

[PAT- Advanced Level] 甲级题解目录(Advanced Level)

A1003 Emergency (25point(s))

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 C2​​ .

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

Analysis

  • 已知有n个城市(编号0~n-1)和m条路。每条路的花费,每个城市的救援小组数,起点城市和终点城市。
  • 求从起点到终点的最短路径条数以及最短路径上的救援小组数目之和。并且输出相同花费的路径中,救援小组数的最大值。
  • 本题首先找最短路径,在有多条最对路径时,选救援多的那条。
  • 用dijkstra算法。设置d[b]存放从源点到b点的花费,设置num[b]存放从源点到b点的路径数,设置teamcnt[b]存放从源点到b点一路上救援队的个数。

  • 本题其实是一个记忆题,要记住dijkstra算法的写法
  • 原写法已经很OK了,缺点是用了太多不必要的全局变量。不过这对OJ解体结果倒是无所谓。
  • 这里之所以不初始化邻接矩阵为全零,是因为和自身到自身为0的情况区分开来,就算本题没有这种刁钻的case,在实际情况中我们应该也要考虑到,设置成无限大更加符合现实中的场景。但为了更加符合,万一有一种情况真的有这么远,初始化值为-1(不可达)是最好的选择。

C++ Code1

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#define INF 1000000000
using namespace std;
const int maxn = 510;
int n, m, g[maxn][maxn], u, v, w, c1, c2;
int d[maxn], resteam[maxn], teamcnt[maxn], num[maxn];
bool vis[maxn];
void dijkstra(int s){
	fill(d, d + maxn, INF);
	d[s] = 0;
	teamcnt[s] = resteam[s];
	num[s] = 1;
	for (int i = 0; i < n; i++){
		int a = -1, min = INF;
		for (int j = 0; j < n; j++){
			if (vis[j] == 0 && d[j] < min){
				a = j;
				min = d[j];
			}
		}
		if (a == -1)	return;
		vis[a] = 1;
		for (int b = 0; b < n; b++){
			if (vis[b] == 0 && g[a][b] != INF){
				if (d[a] + g[a][b] < d[b]){
					d[b] = d[a] + g[a][b];
					teamcnt[b] = teamcnt[a] + resteam[b];
					num[b] = num[a];	// 路径延长,路径数不变
				}
				else if (d[a] + g[a][b] == d[b]){
					if (teamcnt[a] + resteam[b] > teamcnt[b])// 在路径相同时找救援队多的路
						teamcnt[b] = teamcnt[a] + resteam[b];
					num[b] += num[a];	// 出现相同长度路径,到达b的路径数=原到达b的路径数+到达a的路径数
				}
			}
		}
	}
}
int main(){
	scanf("%d %d %d %d", &n, &m, &c1, &c2);
	fill(g[0], g[0] + maxn * maxn, INF);
	for (int i = 0; i < n; i++)
		scanf("%d", &resteam[i]);
	for (int i = 0; i < m; i++){
		scanf("%d %d %d", &u, &v, &w);
		g[u][v] = w;
		g[v][u] = w;
	}
	dijkstra(c1);
	printf("%d %d\n", num[c2], teamcnt[c2]);
	return 0;
}

C++ Code2

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>

#define MAX_N 500

using namespace std;

void dijkstra(int adj[][MAX_N], int path_num_teams[], int path_num[], int city_num_teams[], int start, int n){
    int dist[MAX_N], visit[MAX_N];
	fill(dist, dist + MAX_N, 1000000);
    memset(visit, 0, sizeof(visit));

	dist[start] = 0;
	path_num_teams[start] = city_num_teams[start];
	path_num[start] = 1;

	for (int i = 0; i < n; i++){
		int a = -1, min = 1000000;
		for (int j = 0; j < n; j++){
			if (visit[j] == 0 && dist[j] < min){
				a = j;
				min = dist[j];
			}
		}
		if (a == -1)	return;
		visit[a] = 1;
		for (int b = 0; b < n; b++){
			if (visit[b] == 0 && adj[a][b] != -1){
				if (dist[a] + adj[a][b] < dist[b]){
					dist[b] = dist[a] + adj[a][b];
					path_num_teams[b] = path_num_teams[a] + city_num_teams[b];
					path_num[b] = path_num[a];	// 路径延长,路径数不变
				}
				else if (dist[a] + adj[a][b] == dist[b]){
					if (path_num_teams[a] + city_num_teams[b] > path_num_teams[b])   // 在路径相同时找救援队多的
						path_num_teams[b] = path_num_teams[a] + city_num_teams[b];
					path_num[b] += path_num[a];	// 出现相同长度路径,到达b的路径数=原到达b的路径数+到达a的路径数
				}
			}
		}
	}
}


int main(){
    // n:  the number of cities
    // m:  the number of roads
    // c1: the cities that you are currently in
    // c2: the cities that you must save
    // w:  the length of that road
    int n, m, c1, c2, u, v, w;
    int adj[MAX_N][MAX_N], city_num_teams[MAX_N];
	scanf("%d %d %d %d", &n, &m, &c1, &c2);
	fill(adj[0], adj[0] + MAX_N * MAX_N, -1);

	for (int i = 0; i < n; i++) scanf("%d", &city_num_teams[i]);
	for (int i = 0; i < m; i++){
		scanf("%d %d %d", &u, &v, &w);
		adj[u][v] = w;
		adj[v][u] = w;
	}
    
    int path_num_teams[MAX_N], path_num[MAX_N];
	dijkstra(adj, path_num_teams, path_num, city_num_teams, c1, n);

	printf("%d %d\n", path_num[c2], path_num_teams[c2]);
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值