国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 广德县| 二连浩特市| 正宁县| 卓尼县| 新化县| 曲阳县| 大兴区| 丰都县| 象山县| 宣武区| 宝兴县| 正定县| 丰台区| 金川县| 镇江市| 屏边| 霍州市| 启东市| 岚皋县| 汝州市| 贡觉县| 乌拉特前旗| 长泰县| 香格里拉县| 西平县| 贵定县| 郁南县| 古交市| 扎鲁特旗| 肇东市| 仙居县| 洪湖市| 西乡县| 资兴市| 延川县| 泽普县| 密山市| 秭归县| 周至县| 慈利县| 磐安县|