PAT (Advanced Level) 1087. All Roads Lead to Rome (30) 條條大路通羅馬 Dijkstra+DFS

Indeed there are many different tourist routes from our city to Rome. You are supposed to find your clients the route with the least cost while gaining the most happiness.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive integers N (2<=N<=200), the number of cities, and K, the total number of routes between pairs of cities; followed by the name of the starting city. The next N-1 lines each gives the name of a city and an integer that represents the happiness one can gain from that city, except the starting city. Then K lines follow, each describes a route between two cities in the format "City1 City2 Cost". Here the name of a city is a string of 3 capital English letters, and the destination is always ROM which represents Rome.

Output Specification:

For each test case, we are supposed to find the route with the least cost. If such a route is not unique, the one with the maximum happiness will be recommended. If such a route is still not unique, then we output the one with the maximum average happiness -- it is guaranteed by the judge that such a solution exists and is unique.

Hence in the first line of output, you must print 4 numbers: the number of different routes with the least cost, the cost, the happiness, and the average happiness (take the integer part only) of the recommended route. Then in the next line, you are supposed to print the route in the format "City1->City2->...->ROM".

Sample Input:
6 7 HZH
ROM 100
PKN 40
GDN 55
PRS 95
BLN 80
ROM GDN 1
BLN ROM 1
HZH PKN 1
PRS ROM 2
BLN HZH 2
PKN GDN 1
HZH PRS 1
Sample Output:
3 3 195 97
HZH->PRS->ROM
用兩個map進行城市名與id(0到N-1)的相互映射。
幸福指數這個變量不參與求最短路徑過程中的修正。可直接用一個map進行id到happy的映射,便於最後的方案比較。
用Dijkstra算法確定ROM到起點的最短路徑,在此過程中使用一個前驅數組記錄結點前驅。
用前驅數組DFS出所有路徑並存在結果數組中,該數組存儲每個結點的id。
對所有最短路徑通過結點id獲取幸福指數,進行方案比較。總幸福指數一樣時途徑城市少的路徑更優。
/*2015.7.30cyq*/
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <map>
#include <algorithm>
using namespace std;

//ifstream fin("case1.txt");
//#define cin fin

const int MAX=2147483647;
struct city{
	int num;
	int length;
	city(int n,int l):num(n),length(l){}
};
bool cmp(const city &a,const city &b){
	return a.length<b.length;
}
//從ROM開始DFS出所有最短路徑,存在result中
void dfs(vector<vector<int> > &preCity,int &start,int &end,vector<int> &path,vector<vector<int> > &result){
	path.push_back(start);
	if(start==end){
		result.push_back(path);
	}
	else{
		for(auto it=preCity[start].begin();it!=preCity[start].end();++it)
			dfs(preCity,*it,end,path,result);
	}
	path.pop_back();
}
int main(){
	int N,K;
	string s;
	cin>>N>>K>>s;
	vector<city> AB,UD;//AB爲確定集合,UD爲未確定集合
	vector<vector<int> > roads(N,vector<int>(N,0));
	map<int,string> id2str;	//城市名與id相互映射
	map<string,int> str2id;
	map<int,int> id2happy;
	id2happy[0]=0;
	str2id[s]=0;
	id2str[0]=s;
	city cur(0,0);//起點城市
	AB.push_back(cur);
	int ha;
	int D;//終點ROM的id
	for(int i=1;i<=N-1;i++){
		cin>>s>>ha;
		if(s=="ROM")
			D=i;
		str2id[s]=i;
		id2str[i]=s;
		id2happy[i]=ha;
		UD.push_back(city(i,MAX));//到起點距離爲MAX
	}
	string s1,s2;
	int a,b,c;
	for(int i=0;i<K;i++){
		cin>>s1>>s2>>c;
		a=str2id[s1];
		b=str2id[s2];
		roads[a][b]=c;
		roads[b][a]=c;
	}
	//Dijkstar
	vector<vector<int> > preCity(N);//前驅城市
	while(cur.num!=D){
		for(auto it=UD.begin();it!=UD.end();it++){
			if(roads[cur.num][(*it).num]>0){//有路
				int tmp=cur.length+roads[cur.num][(*it).num];
				if(tmp<(*it).length){
					(*it).length=tmp;
					preCity[(*it).num].clear();
					preCity[(*it).num].push_back(cur.num);
				}else if(tmp==(*it).length){
					preCity[(*it).num].push_back(cur.num);
				}
			}
		}
		sort(UD.begin(),UD.end(),cmp);
		cur=UD[0];
		UD.erase(UD.begin());
		AB.push_back(cur);
	}

	//用前驅數組dfs出ROM到起點的所有最短路徑,存儲在result中  
	vector<int> path;
	vector<vector<int> > result;
	int start=D;
	int end=0;
	dfs(preCity,start,end,path,result);
	
	//方案比較
	int COST=cur.length;//總費用
	int BEST=-1;//最好路線序號,i表示resule[i]
	int MHP=0;  //最大幸福指數
	int COUNT=0;//路徑上的城市數
	int mhp,count;
	for(int it=0;it<result.size();it++){
		mhp=0;
		count=0;
		for(auto ik=result[it].begin();ik!=result[it].end();ik++){
			count++;
			mhp+=id2happy[*ik];
		}
		if(mhp>MHP){
			MHP=mhp;
			BEST=it;
			COUNT=count;
		}else if(mhp==MHP){//總幸福相等時
			if(count<COUNT){//途徑城市少,平均幸福指數就高
				BEST=it;
				COUNT=count;
			}
		}
	}
		
	cout<<result.size()<<" "<<COST<<" "<<MHP<<" "<<MHP/(COUNT-1)<<endl;
	int len=result[BEST].size();
	cout<<id2str[result[BEST][len-1]];
	for(int i=len-2;i>=0;i--)
		cout<<"->"<<id2str[result[BEST][i]];
	return 0;
}


發佈了142 篇原創文章 · 獲贊 4 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章