PTA 1003

PTA 1003

1003. Emergency (25)-PAT甲级真题(Dijkstra算法)

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

Output Specification:

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.

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

题目大意: n个城市m条路,每个城市有救援小组,所有的边的边权已知。给定起点(c1)和终点(c2),求从起点到终点的最短路径的数量以及最短路径上的救援小组数目之和。如果有多条就输出点权(城市救援小组数目)最大的那个

思路:dijkstra算法求单源最短路径问题,在最短路径上不仅存在边权,又存在点权,求点权最大和最短路径的数量,可以重新开2个数组在记录。

dijkstra算法:

步骤:

  1. 为路径数组e ,dis赋值为INF后 ,再为e输入对应点之间的路径长度,保证没有赋值的点之间的长度为无限大
  2. 寻找到起点dis数组中到已经存在路径中的最短路径的点u
  3. 标记点u为true,表示u已经找到了与c1之间的最短路径
  4. 以u为中转站,更新未找到最短路径点之间的更短的路径
#include<bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
int n, m, c1, c2;
int teamcount[505]; // 各站点队伍的数量
int e[505][505]	 ;  //点与点之间的距离
int w[505], num[505], dis[505];
bool visited[505];

int main() {
	cin >> n >> m >> c1 >> c2;
	fill(dis, dis + 505, INF);
	fill(e[0], e[0] + 505 * 505, INF);

	for(int i = 0; i < n; i++) {
		cin >> teamcount[i];
	}
	int a, b, c;
	for(int i = 0; i < m; i++) {
		cin >> a >> b >> c;
		e[a][b] = e[b][a] = c;
	}

	w[c1] = teamcount[c1];
	dis[c1] = 0;
	num[c1] = 1;

	for(int i = 0; i < n; i++) {
		int minn = INF, u = -1;
		for(int j = 0; j < n; j++) {
			if(!visited[j] && dis[j] < minn) {
				u = j;
				minn = dis[j];
			}
		}
//		找完了
		if(u == -1) break;
		visited[u] = true;
		for(int k = 0; k < n; k++) {
			if(!visited[k] && e[u][k] != INF) {
				if(e[u][k] + dis[u] < dis[k]) {
					dis[k] = e[u][k] + dis[u];
					w[k] = w[u] + teamcount[k];
					num[k] = num[u];
				} else if(e[u][k] + dis[u] == dis[k]) {
					if(w[u] + teamcount[k] > w[k]){
						w[k] = w[u] + teamcount[k];	
					}
					num[k] += num[u];
				}

			}
		}

	}
	
	cout << num[c2] << " " << w[c2];

}
posted @ 2025-09-01 16:14  yaohaha  阅读(9)  评论(0)    收藏  举报
返回顶端