1087 All Roads Lead to Rome (30 point(s))

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条边,每个点有相应权值,从源点s出发到目的地ROM。输出最短路径的条数、最短路径长度、选择打印路径上的点权之和、路径上的点权平均值,并打印这条路径。(打印的条路径满足路径长度最短,路径点权之和最大,平均点权最大)。

分析:Dijkstra算法遍历过程附加了一些条件而已。

Dijkstra算法:

Dijkstra() {
  初始化;
  for(循环n次) {
    u = 使dis[u]最小的还未被访问的顶点的编号;
    记u为确定值;
    for(从u除法能到达的所有顶点v){
      for(v未被访问 && 以u为中介点使s到顶点v的最短距离更优)
        优化dis[v];
    }
  }
}

附加条件:

   1 )要求在最短路径上有多条时要求路径上的点权之和最大 ,开c[ ]数组记录路径点权之和

   2 )问有多少条最短路径 ,

    增加一个数组Routes[ ],Routes[s] = 1,其余Routes[u] = 0,表示从起点s到达顶点u的最短路径的条数为num[u]

   3 )打印路径 ,开个pre[ ]数组记录满足上述条件的前驱即可,然后递归打印即可。

   4)求平均点权,需要记录路径上的点数 ,开num[ ]记录下

因此我们在使用Dijkstra算法过程中注意维护 weight[maxn](点权),G[maxn][maxn](存图) c[maxn], dis[maxn],Routes[maxn],num[maxn],pre[maxn]数组即可。

完整代码

#include<bits/stdc++.h>
using namespace std;
const int maxn=210;
const int inf=10000000;
map<string ,int>stringtoint;
map<int,string>inttostring;
int weight[maxn],G[maxn][maxn]; //
int c[maxn], dis[maxn],Routes[maxn],num[maxn],pre[maxn];
bool vis[maxn];
int N,K;

void Dijkstra(int s){
	c[s]=weight[s];
	dis[s]=0;
	Routes[s]=1;
//	vis[s]=true;///?????????
//  开始标记个锤子,开始标记进不去了for循环了 
	for(int i=0;i<N;i++){
		int u=-1,min=inf;
		for(int j=0;j<N;j++){
			if(vis[j]==0 && dis[j]<min){ //找最小中转点 
				min=dis[j];
				u=j; 
			}
		}
		if(u==-1) break;// 单点,退出
		vis[u]=true; 
		for(int v=0;v<N;v++){
			if(vis[v]==false && G[u][v]!=inf ){
				if(dis[v]>dis[u]+G[u][v]){ //距离优先级最高,出现中转后距离变小 
					dis[v]=dis[u]+G[u][v]; //刷新距离 
					c[v]=c[u]+weight[v];  //刷新幸福感 
					Routes[v]=Routes[u];//路线刷新 
					num[v]=num[u]+1;//城市结点数++;
					pre[v]=u;//记录前驱 
				}else if(dis[v]==dis[u]+G[u][v]){
					Routes[v]+=Routes[u];//距离相同路线累加 
					if(c[v]<c[u]+weight[v]){ //次优先级幸福感
					    c[v]=c[u]+weight[v];
					    num[v]=num[u]+1;
				    	pre[v]=u; 
				    }else if(c[v]==c[u]+weight[v]){ //再次级优先级看平均幸福感 
					 	double v1=(double) (c[u]+weight[v])/(num[u]+1);
					 	double v2=(double) c[v]/num[v];
					 	if(v1>v2){
					 //		Routes[v]+=Routes[u];//路线累计放此处可能进不了if条件 结果少算了。 
					        num[v]=num[u]+1;//城市结点数++;
				    	    pre[v]=u; 	
						 }
					 }
				}
			}
		}
	}
}

void print(int s){
	if(s==0){
		cout<<inttostring[s];
		return ;
	}
	print(pre[s]);
	cout<<"->"<<inttostring[s]; 
}					
			
int main(){
	string s1,s2,start;
	int val;
	cin>>N>>K>>start;
	stringtoint[start]=0;
	inttostring[0]=start;
	//初始化数组
	fill(dis,dis+maxn,inf);
	fill(G[0],G[0]+maxn*maxn,inf);// 注意与一维写法区别 
	for(int i=1;i<N;i++){ 
		cin>>s1>>weight[i];
		stringtoint[s1]=i;
	    inttostring[i]=s1;
	}
	for(int i=0;i<K;i++){
		cin>>s1>>s2>>val;
		int a=stringtoint[s1],b=stringtoint[s2];
		G[a][b]=val;
		G[b][a]=val;
	}
	Dijkstra(0);
	string s="ROM";
	int aims=stringtoint[s];/
	printf("%d %d %d %d\n",Routes[aims],dis[aims], c[aims],c[aims]/num[aims]); 
	print(aims); cout<<endl; 
	return 0;
}

上一篇:1127 ZigZagging on a Tree (30 point(s)) (中序、后序建树and dfs深搜)

https://blog.csdn.net/qq_41317652/article/details/89080809

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZCAIHUI_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值