【PAT甲级】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 Nintegers, 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

 【翻译】:输入四个数,分别代表,图的节点数,变数,起点和终点,接下来一行是每个节点所在城市有的消防队的数量,接下来输入每一条边的两个端点及其之间的距离,这道题要求用尽量少的步数到达终点,并且经过的节点的消防队数量总和尽可能大。输出最短路径数量和在最短路径情况下最多消防队数量

运用dijikstra算法

#include <iostream>
#define MAXN 510
#define INF 1e7
using namespace std;
int n,m,c1,c2;
int e[MAXN][MAXN];
int w[MAXN];
int wei[MAXN];//最短路径时最大消防队数量 
int dis[MAXN];//最短路径数组 
int book[MAXN];//标记是否访问数组 
int num[MAXN];//num[i]从出发点到i的最短路径条数 

int main(){
	int u,v;
	cin>>n>>m>>c1>>c2;//点,边,起点,终点 
	fill(e[0],e[0] + MAXN * MAXN, INF);
	fill(dis,dis + MAXN, INF);
	//初始化--------------------------- 
	for(int i = 0; i < n; i++)//每一个城市有的消防队数量 
		cin>>w[i];
	for(int i = 0; i < m; i++){
		int x,y,t;
		cin>>x>>y>>t;
		e[x][y] = t;
		e[y][x] = t;
	}
	dis[c1] = 0;
	wei[c1] = w[c1];
	num[c1] = 1;//计算消防队的数量 
	//输入--------------------------------- 
	//dijikstra
	for(int i = 0; i < n; i++){
		int min = INF; u = -1;
		for(int j = 0; j < n; j++){
			if(book[j] == 0 && dis[j] < min){//找出dis数组中未被标记过的最小值 
				min = dis[j];
				u = j;
			}
		}
		if(u == -1) break;//都访问过了 
		book[u] = 1;
		for(int v = 0; v < n; v++){
			if(book[v] == 0 && e[u][v] != INF){
				if(dis[v] > dis[u] + e[u][v]){//找一个没访问过的点,通过u松弛 
					dis[v] = dis[u] + e[u][v];
					num[v] = num[u];//更新到v点的最短距离 
					wei[v] = wei[u] + w[v];//更新消防队数量 
				}else if(dis[v] == dis[u] + e[u][v]){//通过u距离一样,比较消防队数量 
					num[v] = num[v] + num[u];//距离一样但是可以增加消防队数量,果断选这条路
					//最短路径条数增加 
					if(wei[v] < wei[u] + w[v]){
						wei[v] = wei[u] + w[v];//增加消防队数量 
					}
				}
			}
		}
	}
	
	cout<<num[c2]<<" "<<wei[c2];
	//c1到c2的最短路径和最短情况下最多消防队	
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值