PAT (Advanced Level) 1003. Emergency (25) 最短路径 DFS

来源:互联网 发布:曹云金 吐槽大会 知乎 编辑:程序博客网 时间:2024/05/03 08:19

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 1
Sample Output
2 4
Dijkstra最短路径算法,每个城市结点记录城市号、医疗队数量、到起点城市的总距离、到起点城市的总医疗队数量。
每次对未确定的城市集合进行排序,取出最小的进行处理,判断用该城市作为其他未确定城市的前驱是否更优并更新相应数据。
使用一个二维数组vector<vector<int> > preCity(N);记录每个城市结点的前驱城市,第i个元素记录i号城市的所有前驱城市。最短路径建立后,从终点城市开始对该数组进行dfs可以获得总的最短路径方案数。
/*2015.7.19cyq*/#include <iostream>#include <vector>#include <fstream>#include <algorithm>using namespace std;//ifstream fin("case1.txt");//#define cin finconst int MAX=2147483647;struct city{int cityNum;//城市序号int team;//医疗队数量 int totalLength;//到起点城市的最短路程int totalTeam;  //路径上的所有Teamcity(int num,int team,int len,int tteam):cityNum(num),team(team), totalLength(len),totalTeam(tteam){}bool operator < (const city&a) const{if(totalLength<a.totalLength)return true;else if(totalLength==a.totalLength){if(totalTeam>a.totalTeam)return true;}return false;}};void dfs(vector<vector<int> > &a,int &start,int &end,int &count){if(start==end){count++;}else{for(auto it=a[start].begin();it!=a[start].end();it++){dfs(a,(*it),end,count);}}}int main(){int N,M,C1,C2;cin>>N>>M>>C1>>C2;vector<city> UD;vector<vector<int> > preCity(N);//第i个元素记录i号城市的所有前驱城市int x;city cur(C1,0,0,0);//当前城市,一开始为起点城市for(int i=0;i<N;i++){cin>>x;if(i!=C1)UD.push_back(city(i,x,MAX,0));//未确定的城市else{cur.team=x;cur.totalTeam=x;}}vector<vector<int> > roads(N,vector<int>(N,-1));int a,b,c;while(M--){cin>>a>>b>>c;roads[a][b]=c;roads[b][a]=c;}while(cur.cityNum!=C2){//当前城市不为终点城市for(auto it=UD.begin();it!=UD.end();it++){if(roads[cur.cityNum][(*it).cityNum]>0){//有路int tmpL=cur.totalLength+roads[cur.cityNum][(*it).cityNum];if(tmpL<(*it).totalLength){(*it).totalLength=tmpL;(*it).totalTeam=cur.totalTeam+(*it).team;preCity[(*it).cityNum].clear();preCity[(*it).cityNum].push_back(cur.cityNum);//前驱城市为新的这一个}else if(tmpL==(*it).totalLength){preCity[(*it).cityNum].push_back(cur.cityNum);//前驱城市数目增加int tmpT=cur.totalTeam+(*it).team;if(tmpT>(*it).totalTeam){(*it).totalTeam=tmpT;}}}}sort(UD.begin(),UD.end());cur=UD[0];UD.erase(UD.begin());}//回溯最小路径数目int count=0;dfs(preCity,C2,C1,count);cout<<count<<" "<<cur.totalTeam;return 0;}


0 0
原创粉丝点击