国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

PAT A 1003. Emergency (25)

2019-11-08 01:59:41
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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 1Sample Output
2 4
//題目分析
應(yīng)用dijkstra最短路徑算法。用vector存儲(chǔ)多條最短路徑,通過(guò)遞歸遍歷各條路徑
//代碼
#include <iostream>#include <vector>using namespace std;#define MAX 100000000int reNum[501]={0};int road[501][501]={{0}};int way[501]={0};vector<int> path[501];int collect[501]={0};int dist[502];int pathCount(int now){	if(now==-1){		return 1;	}	int sum=0;	for(int i = 0;i<path[now].size();i++){		sum+=pathCount(path[now][i]);	}// 迭代器遍歷//	for(vector<int>::const_iterator it = path[now].cbegin();it!=path[now].cend();it++){//		sum+=pathCount(*it);//	}	return sum;}int pathSum(int now){	int total=reNum[now];	int max=0;	if(now==-1){		return 0;	}	for(int i=0;i<path[now].size();i++){		int temp=pathSum(path[now][i]);		if(max<temp){			max=temp;		}	}	return total+max;}int main(){	int N,M,C1,C2;	cin>>N>>M>>C1>>C2;	for(int i=0;i<N;i++){		scanf("%d",&reNum[i]);	}	for(int m=0;m<M;m++){		int x,y,dis;		scanf("%d%d%d",&x,&y,&dis);		road[x][y]=dis;		road[y][x]=dis;	}	for(int i=0;i<N;i++){		if(road[C1][i]!=0){			dist[i]=road[C1][i];			way[i]=1;			path[i].push_back(C1);		}else{			dist[i]=MAX;		}	}	// dijkstra算法	path[C1].push_back(-1);	collect[C1]=1;	dist[N]=MAX;	while(1){		int min = N;		for(int i=0;i<N;i++)			if(collect[i]==0&&dist[i]<dist[min])				min=i;		if(min==N)			break;		collect[min]=1;		for(int i=0;i<N;i++){			if(collect[i]==0&&road[min][i]!=0){				if(road[min][i]+dist[min]<dist[i]){					dist[i]=road[min][i]+dist[min];					path[i].clear();					path[i].push_back(min);				}else if(road[min][i]+dist[min]==dist[i]){					path[i].push_back(min);				}			}		}	}	cout<<pathCount(C2)<<' '<<pathSum(C2);	system("pause");	return 0;}
//結(jié)果
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 赞皇县| 永顺县| 岐山县| 巴彦淖尔市| 时尚| 通许县| 乌兰县| 博白县| 咸阳市| 册亨县| 静海县| 张家口市| 奉贤区| 临海市| 邢台市| 北京市| 普格县| 巢湖市| 宜兰县| 祁阳县| 龙井市| 博客| 井陉县| 西乌珠穆沁旗| 石渠县| 乐亭县| 凤庆县| 开远市| 汶上县| 乌兰察布市| 财经| 湘乡市| 金门县| 高邮市| 铁岭市| 吉安县| 屏山县| 当涂县| 英超| 固安县| 崇文区|