1087 All Roads Lead to Rome经典dijkstra算法(较难)

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

这里要更新的数组数据d,c,cnt,num,pre

#include<cstdio>
#include<string>
#include<map>
#include<iostream>
using namespace std;
const int maxn=200;
const int INF=1000000000;
int N, K;
map<string, int>stringToInt;
map<int, string>intToString;//给城市标上对应的序号
int city[maxn], G[maxn][maxn];//city存每个城市的开心,图G存路径
int c[maxn], visit[maxn], d[maxn], num[maxn];//从起点开始c[i]存到i的开心数,visit标志是否访问,d存最短距离,num存最短路的条数
int pre[maxn], cnt[maxn];pre存上一个点是什么,cnt存这条路上有几个城市
void dijkstra(int s){
	fill(d, d+maxn, INF);//学会使用数组初始化用fill,头文件#include<algorithm>
	c[s]=city[s];
	d[s]=0;
	num[s]=1;
	for(int i=0; i<N; i++){
		int u=-1, MIN=INF;
		for(int j=0; j<N; j++){//选择未访问点中距离最小的点改成已经访问
			if(visit[j]==0&&d[j]<MIN){
				MIN=d[j];
				u=j;
			}
		}
		if(u==-1)break;
		visit[u]=1;
		for(int v=0; v<N; v++){//对新改的这个点,遍历所有未访问点,看是否有最短路的更新
			if(visit[v]==0&&G[u][v]!=INF){
				if(d[u]+G[u][v]<d[v]){
					d[v]=d[u]+G[u][v];
					c[v]=c[u]+city[v];
					cnt[v]=cnt[u]+1;
					num[v]=num[u];
					pre[v]=u;
				}else if(d[u]+G[u][v]==d[v]){
					num[v]+=num[u];
					if(city[v]+c[u]>c[v]){
						c[v]=c[u]+city[v];
						cnt[v]=cnt[u]+1;
						pre[v]=u;	
					}else if(city[v]+c[u]==c[v]){
						double uAvg=1.0*(c[u]+city[v])/(cnt[u]+1);
						double vAvg=1.0*c[v]/cnt[v];
						if(uAvg>vAvg){
							cnt[v]=cnt[u]+1;
							pre[v]=u;
						}
					}
				}
			}
		}
	}
}
void dfs(int d){//用递归来输出路径
	if(d==0){
		cout<<intToString[d];
		return;
	}
	dfs(pre[d]);
	cout<<"->"<<intToString[d];
}
int main(){
	string s1, s2, s;
	int value;
	scanf("%d%d", &N, &K);
	for(int i=0; i<=N; i++)pre[i]=i;
	fill(G[0], G[0]+maxn*maxn, INF);
	cin>>s;
	stringToInt[s]=0; intToString[0]=s;
	for(int i=1; i<N; i++){
		cin>>s1>>value;
		stringToInt[s1]=i;
		intToString[i]=s1;
		city[i]=value;
	}
	for(int i=0; i<K; i++){
		cin>>s1>>s2>>value;
		int a=stringToInt[s1], b=stringToInt[s2];
		G[a][b]=G[b][a]=value;
	}
	dijkstra(0);
	int D=stringToInt["ROM"];
	printf("%d %d %d %d\n", num[D], d[D], c[D], c[D]/cnt[D]);
	dfs(D);
	return 0;

}

注意:dijkstra是将起点到其他所有点的最短路都找出,并且,相同距离的最短路,根据最大开心和最大平均开心来更新c[v],cnt[v],num[v],pre[v],
最后注意更新num的时候是用num[v]+=num[u],cnt[v]=cnt[u]+1;是通过前面来更新后面的。

代码来源:https://blog.csdn.net/qq_36589706/article/details/87897730

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值