【PTA】1003 Emergency (25分)

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, 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 c​1, c2and 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 C1to C2
​​ .

Output Specification:

For each test case, print in one line two numbers: the number of different shortest paths between C1
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

//图论问题 求最短路径的条数和最大的一条权值路径。
#include<iostream>
#include<algorithm>
#include<set>
#include<cstdio>
int inf =99999999;
using namespace std;
int N,M,C1,C2;
int map[600][600];
int pe[600];
int len[600];
int ans[600];
int vis[600];
int num[600];
int main(){
    cin>>N>>M>>C1>>C2;
    for(int i=0;i<600;i++)
    for(int j=0;j<600;j++)
       map[i][j]=inf;
	for(int i=0;i<N;i++) {
	cin>>pe[i];
	ans[i]=pe[i];
	}
	for(int i=0;i<M;i++){
		int a,b,c;
		cin>>a>>b>>c;
		map[a][b]=min(map[a][b],c);
		map[b][a]=min(map[b][a],c);
	}
	for(int i=0;i<N;i++)
	{
		len[i]=map[C1][i];
	}
	len[C1]=0;
	int MID,MIN;
	ans[C1]=pe[C1];
	num[C1]=1;
	for(int i=0;i<N;i++)
	{
	    MIN=99999999;MID=-1;
		for(int j=0;j<N;j++)  //找没有被标记的最短点 
		{
			if(len[j]<MIN&&len[j]!=inf&&vis[j]==0)
			{
				MIN=len[j];
				MID=j;
				
			}
		}
		if(MID==-1)
		continue;
		vis[MID]=1;
		for(int k=0;k<N;k++)
		{
			if(map[MID][k]!=inf&&vis[k]==0)
			{
				if(len[MID]+map[MID][k]<len[k])
				{
					len[k]=len[MID]+map[MID][k];
					num[k]=num[MID];
					ans[k]=ans[MID]+pe[k];
				}
				else if(len[MID]+map[MID][k]==len[k])
				{
					num[k]=num[k]+num[MID];    //路径条数更新
					if(ans[MID]+pe[k]>ans[k])  //判断要不要更新 权值
					{
						ans[k]=ans[MID]+pe[k];
					}
				}
			}
		}
	}
	cout<<num[C2]<<" "<<ans[C2]<<endl;
	return 0;
} 
发布了23 篇原创文章 · 获赞 0 · 访问量 368

猜你喜欢

转载自blog.csdn.net/qq_43328587/article/details/105438030