Emergency



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.

输入样例

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

输出样例

2 4

题目大意:救援小组在某城市发生灾害后要以最快速度赶往该城市,并将沿途经过的城市中的救援小组召集起来。

每次输入包含一个测试样例,输入第一行输入citynum,roadnum,now,destination,分别为城市数量,道路数量,初始位置,目的地。第二行输入citynum个数字,表示每个城市救援队的数目。接下来的road行,每行给出三个数字node1,node2,length,表示城市之间道路长度。注意:由题知任何两个城市之间均可达

要求输出不同最短路径的数目、能召集到的最大小组数

思路:用二维数组graph保存城市之间的连通关系以及连通城市之间道路长度,从出发城市进行深搜,记录路径长度len、召集小组数,若到达目的地且len>minlen则直接返回寻找下一条路径,否则分两种情况来进行记录(此处主要想记录不同最短路径的数目):(1)如果len<minlen,则将统计数据sum置为1(表示该最短路径长度出现了一次),minlen=len;(2)如果len=minlen,则将sum++(表示当前最短路径长度之前出现过)。至于小组最大数很容易记录

code:

#include<iostream>
using namespace std;
const int maxn=291000000;
int minlen=maxn,maxpeo=0,sum=0;
int len=0,peo=0;
int citynum,roadnum,now,destination;
int graph[505][505];
int book[505];
int peonum[505];
void Initial(){
	for(int i=0;i<505;i++){
		for(int j=0;j<505;j++){
			graph[i][j]=maxn;
		}
	}
}
void Dfs(int cur){
	if(cur==destination){//到达目的地
	    if(len<minlen){
	    	minlen=len;
	    	maxpeo=peo;
	    	sum=1;
		}
		else if(len==minlen){
		    sum++;
		    if(peo>=maxpeo)maxpeo=peo;
		}
		return ;
	}
	for(int i=0;i<citynum;i++){
		if(graph[cur][i]!=maxn&&book[i]==0){//有路可走 
			book[i]=1;
			len+=graph[cur][i];
			peo+=peonum[i];
			Dfs(i);
			book[i]=0;
			len-=graph[cur][i];
			peo-=peonum[i];
		}
	}
}
int main(){
	int node1,node2,length,pnum;
	Initial();
	cin>>citynum>>roadnum>>now>>destination;
	for(int i=0;i<citynum;i++){
		cin>>peonum[i];
	}
	for(int i=0;i<roadnum;i++){
		cin>>node1>>node2>>length;
		graph[node1][node2]=graph[node2][node1]=length;
	}
	//尝试深搜
	book[now]=1;
	peo+=peonum[now];
	Dfs(now); 
	cout<<sum<<" "<<maxpeo;
	return 0;
}


猜你喜欢

转载自blog.csdn.net/yx970326/article/details/80188809