pta--1003 Emergency(dfs求最短路径)

1003 Emergency (25)(25 分)

https://pintia.cn/problem-sets/994805342720868352/problems/994805523835109376

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

【分析】emmm代码不是自己写的,但是忘了是从谁的博客看到的了,,,自己在代码上加了思路,有几步还不是太明白,以后要再看看

#include<bits/stdc++.h>
using namespace std;
const int N=505;
int matrix[N][N];//两个城市之间的距离 
int city[N];//每个城市拥有的救援队的数目(每个顶点的权重) 
bool visit[N];//某个城市是否访问过 
int m,n,c1,c2,st,ed,l;//要输入的参数,因为函数里要使用,所以定义为全局变量比较方便 
int sc=0,mc=0;//最短路径数和最多能聚集的救援队数 
long long minl=LLONG_MAX;
/*
length:到当前城市的距离
mans:到当前城市能够聚集的救援队的数目
cid:当前城市的编号 
*/
void dfs(int length,int mans,int cid)
{
	//找到目的地更新计数
	if(cid==ed)
	{
		if(length>minl)return;//如果该路长度大于最短路径,函数结束 
		if(length<minl)	//如果小于最短路径,重新赋值 
		{
			sc=1;//最短路径数重新赋值为1 
			minl=length;
			mc=mans;
		}
		else {
			++sc;//如果两路长度相等,则最短路径数增加 
			if(mc<mans)mc=mans;//如果改路可以聚集到更多的救援队,mc重新赋值 
		}
		return;
	 } 
	if(length>minl)return;//如果距离大于最小距离,此路放弃,肯定不会存在最短路径 
	for(int i=0;i<n;i++)//循环n个城市 
	{
		if(matrix[cid][i]==-1||visit[i])continue;//如果该城市与当前城市无通路或已经访问过 
		visit[i]=true;
		dfs(length+matrix[cid][i],mans+city[i],i);//递归,为什么要递归???/////////////////////////////////////// 
		visit[i]=false;//
	 } 
}
int main()
{
	memset(matrix,-1,sizeof(matrix));//-1表示两个城市之间无通路
	memset(visit,0,sizeof(visit));
	cin>>n>>m>>st>>ed;
	for(int i=0;i<n;i++)
		cin>>city[i];
	for(int i=0;i<m;i++)
	{
		cin>>c1>>c2>>l;
		matrix[c1][c2]=matrix[c2][c1]=l;
	 } 
	 visit[st]=true;
	 dfs(0,city[st],st);
	 visit[st]=false;
	 cout<<sc<<" "<<mc<<endl;
	 return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38735931/article/details/81486121