[PAT A1003]Emergency

[PAT A1003]Emergency

题目描述

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

输出格式

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.

输入样例

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

输出样例

2 4

解析

1.题目大意:首先输入n,m,c1,c2,分别表示结点的个数(编号0~N-1),边数,起点和终点,然后下一行输入m个数,分别表示每个结点的救援队个数;然后接下来m行,输入的是起点,终点和路径长度。结果要求我们输出从c1到c2有几条最短路径,并且输入能够到达的救援队的个数。

2.题目考察单源最短路径,即使用迪杰斯特拉算法处理问题:

#include<iostream>
#include<algorithm>
#include<climits>
using namespace std;
const int maxn = 510;
const int INF = INT_MAX;    //设置最大长度
int Graph[maxn][maxn];      //图的邻接表表示
int num[maxn], dis[maxn], cnt[maxn], weight[maxn]; //某一点的救援队个数,最短路径长数组,起点到某点的最短路径数组,和记录结点到起点的点权和数组
bool visit[maxn];  //visit数组
int n, m, sts, des;  //n,m和起点,终点
void Dijkstra(int s);
int main()
{
	fill(dis, dis + maxn, INF);
	fill(Graph[0], Graph[0] + maxn * maxn, INF);
	scanf("%d%d%d%d", &n, &m, &sts, &des);
	for (int i = 0; i < n; i++)scanf("%d", &num[i]);
	for (int i = 0; i < m; i++) {
		int c1, c2, len;
		scanf("%d%d%d", &c1, &c2, &len);
		Graph[c1][c2] = Graph[c2][c1] = len;
	}
	Dijkstra(sts);
	printf("%d %d", cnt[des], weight[des]);
	return 0;
}
void Dijkstra(int s) {
	dis[s] = 0;   //设置起始点距离=0
	cnt[s] = 1;   //从起始点到起始点的最短路径个数为1
	weight[s] = num[s];
	for (int i = 0; i < n; i++) {  //寻找当前路径最短的结点
		int u = -1, min = INF;
		for (int j = 0; j < n; j++) {
			if (visit[j] == false && dis[j] < min) {
				min = dis[j];
				u = j;
			}
		}
		if (u == -1) return; //没找到,返回
		visit[u] = true;  //找到了,该结点visit设为true
		for (int j = 0; j < n; j++) {
			if (visit[j] == false && Graph[u][j] != INF) { //如果某结点没有被访问 且 从u到该点有路径
				if (dis[u] + Graph[u][j] < dis[j]) { //比原先的路径长度要小
					weight[j] = weight[u] + num[j];  //那么该结点的点权和为之前的加上该点的
					dis[j] = dis[u] + Graph[u][j];
					cnt[j] = cnt[u];            //!这里路径条数继承自之前结点的cnt
				}
				else if (dis[u] + Graph[u][j] == dis[j]) {
					if (weight[u] + num[j] > weight[j]) { //如果点权和更大,覆盖之前的
						weight[j] = weight[u] + num[j];
					}
					cnt[j] += cnt[u]; //找到的路径数+1
				}
			}
		}
	}
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值