甲级PAT 1087 All Roads Lead to Rome (30 分)(Dijkstra,dfs)

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 recommanded. 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 recommanded 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

以后再补说明

注意

1.这里城市名称如果转化为对应的int数值的话,26*26*26=17576,再开个二维数组做邻接矩阵来存储边的信息,直接就炸掉了,所以采用两个map,一个从字符串转化为int,一个从int转化为字符串

2.在dfs中存在一条路径从起点到终点且开心值总和最大的时候,更新最大开心总和值的同时记得更新开心平均值

3.记得将所有除起始结点以外和起始结点相连通的其他结点的初始的前一结点置成起始结点的下标(这里我的起始结点为1,其余结点从2-n)

 

参考代码

#include<bits/stdc++.h>
using namespace std;
#define INF 999999
#define maxsize 220

int edge[maxsize][maxsize];
int city[maxsize],dst[maxsize],book[maxsize];
vector<vector<int> >front(maxsize);
map<string,int> m1;
map<int,string> m2;
vector<int> temppath,path;
int sumhappy=0,averhappy=0,pathnum=0;

void dfs(int x){
	int i;
	if(x==1){
		int tsumhappy=0,num=0,taverhappy;
		for(i=0;i<temppath.size();i++){
			tsumhappy+=city[temppath[i]];
			num++;
		}
		taverhappy=tsumhappy/num;
		if(tsumhappy>sumhappy){
			sumhappy=tsumhappy;
			path=temppath;
			averhappy=taverhappy;
		}
		else if(tsumhappy == sumhappy){
			if(taverhappy>averhappy){
				path=temppath; 
				averhappy=taverhappy;
			}
		}
		pathnum++;
		return;
		
	}
	temppath.push_back(x);
	for(i=0;i<front[x].size();i++){
		dfs(front[x][i]);
	}
	temppath.pop_back();
	
}


int main(){
	int n,m,i,j,k,dis,start,end,temp,maxn=0,u,v;
	string startcity,tempcity,ucity,vcity;
	cin>>n>>m>>startcity;
	memset(book,0,sizeof(book));
	for(i=1;i<=n;i++){
		for(j=1;j<=n;j++){
			edge[i][j]=INF;
		}
	}
	m1[startcity]=1;
	m2[1]=startcity;
	for(i=2;i<=n;i++){
		cin>>tempcity>>temp;
		m1[tempcity]=i;
		m2[i]=tempcity;
		city[i]=temp; 
		
	}
	for(i=0;i<m;i++){
		cin>>ucity>>vcity>>dis;
		u=m1[ucity];
		v=m1[vcity];
		edge[u][v]=edge[v][u]=dis;
	}
	end=m1["ROM"];
	
	for(i=1;i<=n;i++){
		dst[i]=edge[1][i];
		if(dst[i]!=INF) front[i].push_back(1);
	}
	book[1]=1;
	front[1].push_back(1);
	for(i=0;i<n;i++){
		int min=INF;
		for(j=2;j<=n;j++){
			if(city[j]>0 && book[j]==0 && dst[j]<min){
				min=dst[j];
				k=j;
			}
		}
		book[k]=1;
		for(j=2;j<=n;j++){
			if(book[j]==0 && edge[k][j]!=INF && city[j]>0 ){
				if(dst[j]>dst[k]+edge[k][j]){
					dst[j]=dst[k]+edge[k][j];
					front[j].clear();
					front[j].push_back(k);
				}else if(dst[j]==dst[k]+edge[k][j]){
					front[j].push_back(k);
				}
			}
		}
	}
	dfs(end);
	cout<<pathnum<<" "<<dst[end]<<" "<<sumhappy<<" "<<averhappy<<endl;
	cout<<m2[1];
	for(i=path.size()-1;i>=0;i--){
		cout<<"->"<<m2[path[i]];
	}
	return 0;
} 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值