PAT Advanced 1087 All Roads Lead to Rome (30分) [Dijkstra算法 + DFS,最短路径]

题目

Indeed there are many diferent 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 diferent 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

题目分析

已知点权(幸福指数)、边权(消费),求消费最少最短路径,若有多条消费最短路径,取平均消费最少的,若仍有多条最短路径,取幸福指数最大的

解题思路

dijkstra求所有消费最短路径,并将消费最短路径记录在vector pre[maxn]中
dfs求平均消费最少且幸福指数最大的路径,求出消费最短路径的条数

Code

#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
const int maxn=210;
const int inf=0x7fffffff;
unordered_map<string,int> sti;
unordered_map<int,string> its;
int e[maxn][maxn],visit[maxn],weight[maxn],dist[maxn];
int n,maxvalue,maxavg,cntpath;
vector<int> pre[maxn],temppath,path;
void dfs(int v) {
	if(v==1) { //已遍历到出发点
		temppath.push_back(v);
		int value =0; //统计总幸福指数 
		for(int i=0;i<temppath.size();i++){
			value+=weight[temppath[i]];
		}
		double tempavg=1.0*value/(temppath.size()-1);
		if(value>maxvalue){ //更新最大幸福指数路线 
			maxvalue=value;
			maxavg=tempavg;
			path=temppath;
		}else if(value==maxvalue&&tempavg>maxavg){ //多个最大幸福指数路线,比较平均消费 
			maxavg=tempavg;
			path=temppath;
		}
		temppath.pop_back();
		cntpath++;
		return;
	}
	temppath.push_back(v);
	for(int i=0; i<pre[v].size(); i++) {
		dfs(pre[v][i]);
	}
	temppath.pop_back();
}
void dijkstra(int s) {
	// 1 initialize
	fill(dist,dist+n+1,inf); //initialize dist
	dist[s]=0;
	for(int i=1; i<=n; i++) { //每次收集一个结点,最大需要收集n次
		// 2 find min
		int u=-1,min=inf;
		for(int j=1; j<=n; j++) {
			if(visit[j]==false&&dist[j]<min) {
				min=dist[j];
				u=j;
			}
		}
		if(u==-1||u==sti["ROM"])break; //已结束收集,或者已找到所求顶点的最短路径
		visit[u]=true; //标记为已被收集
		// 3 update
		for(int j=1; j<=n; j++) {
			if(e[u][j]==0||visit[j]==true)continue; //边权为0,或者已被收集,跳过
			if(dist[j]>dist[u]+e[u][j]) {
				dist[j]=dist[u]+e[u][j];
				pre[j].clear();
				pre[j].push_back(u);
			} else if(dist[j]==dist[u]+e[u][j]) {
				pre[j].push_back(u);
			}
		}
	}
}
int main(int argc,char * argv[]) {
	int k;
	scanf("%d %d",&n,&k);
	string sc;
	cin>>sc;
	sti[sc]=1;
	its[1]=sc;
	for(int i=2; i<=n; i++) {
		cin>>sc>>weight[i];
		sti[sc]=i;
		its[i]=sc;
	}
	string sa,sb;
	int ct;
	for(int i=1; i<=k; i++) {
		cin>>sa>>sb>>ct;
		e[sti[sa]][sti[sb]]=e[sti[sb]][sti[sa]]=ct;
	}
	dijkstra(1);
	int rom=sti["ROM"];
	dfs(rom);
	printf("%d %d %d %d\n",cntpath,dist[rom],maxvalue,(int)maxavg);
	for(int i=path.size()-1;i>=1;i--){
		cout<<its[path[i]]<<"->";
	}
	cout<<"ROM";
	return 0;
}

posted @ 2020-03-17 19:42  JamieHou  阅读(107)  评论(0编辑  收藏  举报