1087. All Roads Lead to Rome

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.搞明白每个记录型数组的更新条件,以及如何去更新(何时覆盖何时添加)

          2.做好城市名string和int互映射

#include<stack>
#include<vector>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<queue>
#include<map>
#include<string>
#include<stdio.h>
using namespace std;
const int maxv=201;
int INF=1e8;
/* 
  dijstra
  注意城市和id之间的对应 方法:设map
  第一标尺:距离
  第二标尺:点权
  第三标尺:平均点权
*/
map<string,int> getid;
map<int,string> getname;
int	weight[maxv],g[maxv][maxv],n;
//分别记录最短道路条数,点权,最短距离,路径上的点的个数
int num[maxv]={0},w[maxv]={0},d[maxv],counts[maxv]={0};
bool vis[maxv]={false};
int father[maxv];//

void dijstra(int s){
	fill(d,d+maxv,INF);
	fill(father,father+maxv,-1);
	num[s]=1;
	w[s]=weight[s];
	d[s]=0;
	counts[s]=1;

	for(int j=0;j<n;j++){
		int u=-1,min=INF;
		for(int i=0;i<n;i++){
			if(!vis[i] && d[i]<min){
				u=i;
				min=d[i];
			}
		}
		vis[u]=true;
		for(int i=0;i<n;i++){
			//注意选择父路径的时候何时利用第二标尺
			if(!vis[i] && d[u]+g[u][i]<d[i]){
				d[i]=d[u]+g[u][i];
				father[i]=u;
				num[i]=num[u];
				w[i]=w[u]+weight[i];
				counts[i]=counts[u]+1;
			}
			else if(!vis[i] && d[u]+g[u][i]==d[i]){
				if(weight[i]+w[u]>w[i]){
					father[i]=u;
					w[i]=weight[i]+w[u];
					counts[i]=counts[u]+1;
				}
				else if(weight[i]+w[u]==w[i]){//经过的点越少,则平均幸福度越大
					if(counts[u]+1<counts[i]){
						counts[i]=counts[u]+1;
						father[i]=u;
					}
				}
				num[i]+=num[u];
			}
		}
	}
}

bool flag=false;
void getpath(int e){//注意起点的根节点是-1
	if(e==-1)	return;
	getpath(father[e]);
	if(!flag){
		cout<<getname[e];
		flag=true;
	}	
	else cout<<"->"<<getname[e];
}

int main()
{
	//freopen("E:\\learn\\inputs.txt","r",stdin);
	for(int i=0;i<maxv;i++){
		for(int j=0;j<maxv;j++)
			g[i][j]=INF;
	}
	int m;
	string st,temp;
	cin>>n>>m>>st;
	for(int i=0;i<n-1;i++){
		cin>>temp;
		getid[temp]=i;
		getname[i]=temp;
		cin>>weight[i];
	}
	getid[st]=n-1;
	getname[n-1]=st;
	for(int i=0;i<m;i++){
		string s,t;
		int w,u,v;
		cin>>s>>t>>w;
		u=getid[s];
		v=getid[t];
		g[u][v]=g[v][u]=w;
	}

	int sid=getid[st];
	int eid=getid["ROM"];
	dijstra(sid);
	cout<<num[eid]<<" "<<d[eid]<<" "<<w[eid]<<" "<<w[eid]/(counts[eid]-1)<<endl;
	getpath(eid);
	cout<<endl;
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值