1003. Emergency (25)

本文介绍了一个基于迪杰斯特拉算法的紧急救援路径寻找程序。该程序能在特定地图上找到两个城市间的最短路径,并计算沿途能集结的最大救援队伍人数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1003. Emergency (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

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

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 21 2 1 5 30 1 10 2 20 3 11 2 12 4 13 4 1

Sample Output

2 4


# include<iostream>
# include<vector>
using namespace std;
const int INF=1000000;
int price[501][501];
int people[501];
int dist[501];
int isread[501];
int path1[501];
int count1[501];
void dijstra(int b,int N){
	int begin=b;
	int i,j;
	int fir=0;
	dist[b]=0;
	path1[b]=people[b];
	while(1){
		if(fir==0)
		fir=1;
		else{
			int m=INF;
			for(i=N-1;i>=0;i--){
				if(isread[i]==0&&dist[i]<m){
					m=dist[i];
					begin=i;
				}
			}
		}
		if(isread[begin]==1)
		break;
		//cout<<"&"<<begin<<"&";
		isread[begin]=1;
		for(i=0;i<N;i++){
			if(price[begin][i]!=INF&&isread[i]==0){
				if(price[begin][i]+dist[begin]<dist[i]){
					dist[i]=price[begin][i]+dist[begin];
					count1[i]=count1[begin];
					path1[i]=path1[begin]+people[i];
					//cout<<i<<"="<<begin<<endl;
					
				}
				else if(price[begin][i]+dist[begin]==dist[i]){
					count1[i]+=count1[begin];
					if(path1[begin]+people[i]>path1[i])
					path1[i]=path1[begin]+people[i];
					//cout<<i<<"+="<<begin<<endl;
				}
			}
		}
	}
	
}
int main(){
	int N,M,C1,C2;
	cin>>N>>M>>C1>>C2;
	int i,j;
		int temp1,temp2,temp3;
	for(i=0;i<N;i++){
		cin>>people[i];
		dist[i]=INF;
	}
	for(i=0;i<N;i++)
	for(j=0;j<N;j++)
	price[i][j]=INF;
	for(i=0;i<M;i++){
		cin>>temp1>>temp2>>temp3;
		price[temp1][temp2]=temp3;
		price[temp2][temp1]=temp3;
		
	}
	for(i=0;i<N;i++){
		isread[i]=0;
		path1[i]=0;
		count1[i]=1;
		
	}
/*	for(i=0;i<N;i++){
	cout<<endl;
	for(j=0;j<N;j++)
	cout<<price[i][j]<<" ";
}*/
	dijstra(C1,N);
	cout<<count1[C2]<<" "<<path1[C2]<<endl;
	//cout<<count1[0];
} 
感想:只要人数不出现负数,dijstra贪心就可以解决一切问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值