1087 All Roads Lead to Rome (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
题目大意

有N个城市K条边,每条边的权重称为cost,每个点的权重为happiness,输入文件给定源城市,求从源城市到目标城市ROM的最短路径,若最短路径不唯一,则选择happiness最大的路径,若这个路径依然不唯一,则选择平均happiness最大的路径,输出最短路径的条数,最短路径长度,推荐路径的happiness和对应的平均happiness

分析

先通过dijkstra算法求出最短路径,用二维数组保存,然后用dfs求最短路径上的happiness,平均happiness,开始时将平均happiness设为int型,结果有一个测试点报「段错误」,最后将平均happiness设定为double类型,代码AC~~,通过查询PAT错误说明,段错误是由指针越界或者因递归层次过深导致堆栈溢出引起,依然没理解为什么改变平均happiness后段错误消失。

#include <iostream>
#include <map>
#include <vector>
using namespace std;
int happiness[210];
vector<vector<int> >a;
vector<vector<int> > path;
map<string,int> mp;
map<int,string> ansmp;
int n,k;
string city;
int rome;
int maxhap=-1;
double max_avr_hap=-1.0;
const int inf=999999;
void dfs(int u,vector<int> &tmppath,vector<int> &finalpath,int &route_num){
	tmppath.push_back(u);
	if(u==rome){
		route_num++;
		int sumhap=0;
		double avr_hap=0.0;
		for(int i=0;i<tmppath.size();i++){
			sumhap+=happiness[tmppath[i]];
		}
		avr_hap=1.0*sumhap/(tmppath.size()-1);
		if(sumhap>maxhap){
			maxhap=sumhap,max_avr_hap=avr_hap;
			finalpath=tmppath;
		}else if(sumhap==maxhap && avr_hap>max_avr_hap){
			maxhap=sumhap,max_avr_hap=avr_hap;
			finalpath=tmppath;
		}
	}else{
		for(int w=0;w<path[u].size();w++){
			dfs(path[u][w],tmppath,finalpath,route_num);
		}
	}
	tmppath.pop_back();
}
void shortpath(int u){
	vector<int> dis(n,inf);
	vector<bool> visit(n,false);
	dis[u]=0;
	for(int k=0;k<n;k++){
		int t=-1,mindis=inf;
		for(int i=0;i<n;i++){
			if(visit[i]==false && dis[i]<mindis){
				t=i;
				mindis=dis[i];
			}
		}
		if(t==-1) break;
		visit[t]=true;
		for(int v=0;v<n;v++){
			if(visit[v]==false){
				if(dis[v]>dis[t]+a[v][t]){
					path[v].clear();
					path[v].push_back(t);
					dis[v]=dis[t]+a[v][t];
				}else if(dis[v]==dis[t]+a[v][t]){
					path[v].push_back(t);
				}
			}
		}
	}
}
int main(){
	cin>>n>>k>>city;
	a.resize(n),path.resize(n);
	fill(a.begin(),a.end(),vector<int>(n,999));
	mp[city]=0,ansmp[0]=city;
	for(int i=1;i<n;i++){
		string s;
		int val;
		cin>>s>>val;
		mp[s]=i,ansmp[i]=s;
		happiness[i]=val;
		if(s=="ROM"){
			rome=i;
		}
	}
	for(int i=0;i<k;i++){
		string s1,s2;
		int val;
		cin>>s1>>s2>>val;
		a[mp[s1]][mp[s2]]=a[mp[s2]][mp[s1]]=val;
	}
	shortpath(rome);
	int route_num=0,mincost=0;
	vector<int> tmppath,finalpath;
	dfs(0,tmppath,finalpath,route_num);
	
	for(int i=0;i<finalpath.size()-1;i++) {
		// printf("finalpath=%d\n",finalpath[i] );
		mincost+=a[finalpath[i]][finalpath[i+1]];
	}
	printf("%d %d %d %d\n",route_num,mincost,maxhap,(int)max_avr_hap );
	for(int i=0;i<finalpath.size();i++){
		if(i==0) cout<<ansmp[finalpath[i]];
		else cout<<"->"<<ansmp[finalpath[i]];
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值