【PAT】1087. All Roads Lead to Rome (30)

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

分析:查找所有最短路,列出最短路共有多少條,並且給出最短路中的最優解。

#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <string>
#include <map>
using namespace std;
#define INF 9999999

struct city{
	city(string _n, int _h){name=_n; happy=_h;}
	string name;
	int happy;
};
vector<city> cities;  
vector<vector<int> > cost;
vector<vector<int> > allPath;
vector<int> bestPath;
vector<int> possiblePath;
vector<bool> visited;
vector<int> dist; //記錄起點到各個點的距離 
map<string, int> name2id;
int cityNum, roadNum;
int cnt = 0;
void dijkstra(int s){
	int i;
	dist[s] = 0;
	while(true){
		int v = -1;
		for(int u=0; u<cityNum; u++){
			if(!visited[u]&&(v==-1||dist[u]<dist[v]))	v=u;
		}
		if(v==-1) break;
		visited[v]=true;
		for(int u=0; u<cityNum; u++){
			if(dist[u] > dist[v] + cost[v][u]){
				dist[u] = dist[v] + cost[v][u];
				allPath[u].clear();
				allPath[u].push_back(v);
			}else if(dist[u]==dist[v]+cost[v][u]){		 
				allPath[u].push_back(v);
			}
		}
	}
}
int maxHappy = 0;
double maxAve = 0;
int pathNum = 0; //最短路的條數 
void findBestPath(int t){
	possiblePath.push_back(t);
	if(t==0){	
		pathNum++;	
		int happy = 0;
		for(int i=0; i<possiblePath.size(); i++){
			int index = possiblePath[i];
			happy += cities[index].happy;
		}	
		if(happy > maxHappy){
			bestPath = possiblePath;
			maxHappy = happy;
			maxAve = (double)happy/(double)(possiblePath.size()-1);
		}else if(happy==maxHappy){
			double aveNow = (double)happy/(double)(possiblePath.size()-1);
			if(aveNow>maxAve){
				bestPath = possiblePath;
				maxAve = aveNow;	
			}
		}
		return;
	}
	for(int i=0; i<allPath[t].size(); i++){
		findBestPath(allPath[t][i]);	
		possiblePath.pop_back();	 
	}	
}

int main(int argc, char** argv) {
	int c, i, j, happy;
	string start, from, to, str;
	scanf("%d%d",&cityNum, &roadNum);
	cin>>start;
	cities.push_back(city(start,0));
	name2id[start] = 0;//起點 
	for(i=1; i<cityNum; i++){
		cin>>str>>happy;
		name2id[str] = i;
		cities.push_back(city(str,happy));
	}
	cost.resize(cityNum, vector<int>(cityNum,INF));
	
	for(i=0; i<roadNum; i++){
		cin>>from>>to>>c;
		int a = name2id[from];
		int b = name2id[to];	
		cost[a][b] = cost[b][a] = c;	
	}
	dist.resize(cityNum,INF);
	for(i=0; i<cityNum; i++)
	   dist[i] = cost[0][i];
		
	int s = 0;//起點
	int t = name2id["ROM"];//終點 
	visited.resize(cityNum,false);
	allPath.resize(cityNum);
	dijkstra(s);//查找從起點出發的所有最短路 
	findBestPath(t);//找到最短路中的最優解 
   	int minCost=0;
	for(i=bestPath.size()-1; i>0; i--){
    	int a = bestPath[i];
    	int b = bestPath[i-1];
    	minCost += cost[a][b];
	}		
	cout<<pathNum<<" "<<minCost<<" "<<maxHappy<<" "<<(int)maxAve<<endl;
	for(i=bestPath.size()-1; i>=0; i--){
		string name = cities[bestPath[i]].name;
		if(i!=0) 
			cout<<name<<"->";
		else
			cout<<name<<endl;
	}	
	return 0;
}

解法二:可以不用結構體

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
#define INF 99999
int N,K;
map<string, int> happy;
map<string, int> name2id;
map<int, string> id2name;
vector<vector<int> > cost;
vector<vector<int> > allPath;
vector<int> d;

void dijkstra(int s){
	allPath.resize(N);
	vector<bool> used(N,false);
	d.resize(N,INF);
	d[s] = 0;
	while(true){
		int v = -1;
		for(int u=0; u<N; u++){
			if(!used[u] && (d[u]<d[v] || v==-1))
				v = u;
		}
		if(v == -1) break;
		used[v] = true;
		for(int u=0; u<N; u++){
			if(d[u] > cost[v][u] + d[v]){
				d[u] = cost[v][u] + d[v];
				allPath[u].clear();
				allPath[u].push_back(v);
			}else if(d[u] == cost[v][u]+d[v]){
				allPath[u].push_back(v);
			}
		}
	}	
}

vector<int> possiblePath;
vector<int> bestPath;
int maxHappy=0;
double maxAve = 0;
int pathNum = 0;
void findBest(int des){
	possiblePath.push_back(des);
	if(des == 0){
		pathNum++;
		int h=0;
		double a=0;
		for(int i=0; i<possiblePath.size(); i++){
			int t = possiblePath[i];
			string str = id2name[t];
			h += happy[str];
		}
		a = (double)h/(double)(possiblePath.size()-1);
		if(h>maxHappy || (h==maxHappy&&a>maxAve)){
			maxHappy = h;
			maxAve = a;
			bestPath = possiblePath;
		}
	}
	for(int i=0; i<allPath[des].size(); i++){
		findBest(allPath[des][i]);
		possiblePath.pop_back();
	}	
}

int main(int argc, char** argv) {
	string start;
	scanf("%d%d",&N,&K);
	cost.resize(N,vector<int>(N,INF));
	cin>>start;
	name2id.insert(make_pair(start,0));
	id2name.insert(make_pair(0,start));
	happy.insert(make_pair(start,0));
	int i, val;
	string str;
	for(i=1; i<N; i++){
		cin>>str>>val;
		name2id.insert(make_pair(str,i));
		id2name.insert(make_pair(i,str));
		happy.insert(make_pair(str,val));
	}
	string from, to;
	int a, b, c;
	for(i=0; i<K; i++){
		cin>>from>>to>>c;
		a = name2id[from];
		b = name2id[to];
		cost[a][b] = cost[b][a] = c;
	}
	dijkstra(0);	
	int des = name2id["ROM"];
	findBest(des);	
	
	printf("%d %d %d %d\n", pathNum, d[des], maxHappy, (int)maxAve);
	for(i=bestPath.size()-1; i>=0; i--){
		string tmp = id2name[bestPath[i]];
		if(i==bestPath.size()-1){
			cout<<tmp;
		}else{
			cout<<"->"<<tmp;
		}
	}	
	return 0;
}



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