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
分析:

(1)本题考试的时间偶以为dij就可以搞定,但是求最短路径数的时候就很纠结了。

(2)实际上dfs可以搞定,但是本例参照别人的程序,同时用了dfs和dij。dij的最用仅仅是求出最短路径,然后作为dfs的一个条件判断罢了。可能这样思路会更清晰。

(3)本题没有特殊的语法注意,关键是思路和细节要清晰完整,需要多练。可以分成输入输出,dij,dfs三部分来调试。



#include <iostream>
#include<string>
#include<vector>
#include<map>
using namespace std;
#define MAX 8888888
#define SIZE 205
int n,k;
int dest;
vector<int> resPath;
map<string,int> hash1;
map<int,string> hash2;
int happiness[SIZE];
int graph[SIZE][SIZE];
int visit[SIZE];
int dij[SIZE];
int shortest;
int pathnumber=0,sumhap=0,sumcost=0,avghap=0;
void dijkstra (int n);
void dfs (int begin, int end, vector<int> &path, int rescost, int reshap);
int main (){
	cin>>n>>k;
	string city1,city2,city3;
	int tmp1,tmp2,tmp3;
	int index1=0;
	cin>>city1;
	hash1[city1]=index1;
	hash2[index1]=city1;
	happiness[index1]=0;
	int i,j;
	for (i=1;i<n;i++){
		cin>>city1>>tmp1;
		if (city1=="ROM") dest=i;
		hash1[city1]=i;
		hash2[i]=city1;
		happiness[i]=tmp1;
	}
	for (i=0;i<n;i++){
		dij[i]=MAX;
		for (j=0;j<n;j++){
			graph[i][j]=MAX;
		}
	}
	for (i=0;i<k;i++){
		cin>>city2>>city3>>tmp1;
		tmp2=hash1[city2];
		tmp3=hash1[city3];
		if (tmp1<graph[tmp2][tmp3]){
			graph[tmp2][tmp3]=tmp1;
			graph[tmp3][tmp2]=tmp1;
		}
	}
	dijkstra (0);
	shortest=dij[dest];
	for (i=0;i<n;i++){
		visit[i]=0;
	}
	vector<int> myPath(1,0);
	visit[0]=1; //!!
	dfs(0,dest,myPath,0,0);

	cout<<pathnumber<<" "<<shortest<<" "<<sumhap<<" "<<avghap<<endl;
	cout<<hash2[0];
	for (i=1;i<resPath.size();i++){
		cout<<"->"<<hash2[resPath[i]];
	}

	return 0;
}
void dijkstra (int number){
	int i,j;
	int x=number;
	for (i=0;i<n;i++){
		visit[i]=0;
		dij[i]=graph[i][x];
	}
	dij[x]=0;
	visit[x]=1;

	for (i=0;i<n;i++){
		int mmin=MAX;
	    for (j=0;j<n;j++){
			if (!visit[j]&&dij[j]<mmin){
			mmin=dij[j];
			x=j;
		    } 
	    }
	    visit[x]=1;
		for (j=0;j<n;j++){
			if (!visit[j]&&dij[j]>dij[x]+graph[j][x]){
				dij[j]=dij[x]+graph[j][x];
			}
		}

	}
}
void dfs (int begin, int end, vector<int> &path, int rescost, int reshap){
	if (rescost>shortest) return;
	if (begin==end){
		pathnumber++;
		if (reshap<sumhap) return;
		if (reshap>sumhap){
			resPath=path;
			sumhap=reshap;
			avghap=sumhap/(resPath.size()-1);
			return;
		}
		if (reshap==sumhap){
			int tmpavg=sumhap/(path.size()-1);
			if (tmpavg>avghap){
				resPath=path;
			    sumhap=reshap;
			    avghap=sumhap/(resPath.size()-1);
			}
			return;
		}
	}
	int i,j;
	for (i=0;i<n;i++){
		if (!visit[i]&&graph[begin][i]!=MAX){
			path.push_back(i);
			visit[i]=1;
			dfs(i,end,path,rescost+graph[begin][i],reshap+happiness[i]);
			path.pop_back();
			visit[i]=0;
		}
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值