All Roads Lead to Rome

2019/7/21 11:25:28 人评论 次浏览 分类:学习教程

 

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.

 

输入描述:

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.


 

输出描述:

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".

示例1

输入

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

输出

3 3 195 97
HZH->PRS->ROM
#include<bits/stdc++.h>
using namespace std;
unordered_map<string,int>STOI;
const int inf=0x3f3f3f3f;
const int N=210;
int G[N][N],dst[N],vst[N];
string ITOS[N];
int h[N],happy[N];//每个节点处的幸福值以及每
//个节点处的总的幸福值 
int pre[N],pathnum[N],pointnum[N];//保存路径上的前驱节点,
//最短路径的数目,最短路径上经过的城市节点的数目 
//dijkstra求最短路
int n,k;
void dijkstra(int p){
	memset(vst,0,sizeof(vst));
	for(int i=0;i<n;i++){
		dst[i]=inf;
	}
	dst[0]=0;
	while(!vst[p]){
		//printf("sum=%d\n",pathnum[p]);
		//printf("ROM=%d\n",dst[p]);
		int v=-1,min_d=inf;
		for(int j=0;j<n;j++){
			if(!vst[j]&&dst[j]<min_d){
				v=j;
				min_d=dst[j];
				}
		}
		//cout<<"v="<<v<<endl;
		if(v==-1) return;
		vst[v]=1;
			//更新参数值 
		for(int j=0;j<n;j++){
			if(G[v][j]!=inf&&j!=v&&!vst[j]&&dst[v]+G[v][j]<dst[j]){
				dst[j]=dst[v]+G[v][j];
				happy[j]=happy[v]+h[j];
				pre[j]=v;
				pointnum[j]=pointnum[v]+1;
				pathnum[j]=pathnum[v];
			}else if(G[v][j]!=inf&&j!=v&&dst[v]+G[v][j]==dst[j]){
				pathnum[j]+=pathnum[v];
				if(happy[j]<happy[v]+h[j]){
					happy[j]=happy[v]+h[j];
					pre[j]=v;
					pointnum[j]=pointnum[v]+1;
				}else if(happy[j]==happy[v]+h[j]){
					if(pointnum[v]+1<pointnum[j]){
						pre[j]=v;
						pointnum[j]=pointnum[v]+1;
					}
				}
			}
		}
	}
} 
void dfs(int v){
	if(v==0){
		cout<<ITOS[v];
		return;
	}
	dfs(pre[v]);
	cout<<"->"<<ITOS[v];
}
int main(){
	scanf("%d%d",&n,&k);
	for(int i=0;i<=n;i++){
		for(int j=0;j<=n;j++){
			if(i==j) G[i][j]=0;
			else G[i][j]=inf;
		}
	}
	cin>>ITOS[0];
	STOI.insert({ITOS[0],0});
	for(int i=1;i<n;i++){
		cin>>ITOS[i]>>h[i];
		STOI.insert({ITOS[i],i});
	}
	int x;
	string a,b;
	for(int i=1;i<=k;i++){
		cin>>a>>b>>x;
		if(x<G[STOI[a]][STOI[b]])
		    G[STOI[a]][STOI[b]]=G[STOI[b]][STOI[a]]=x;
	}
	int hh=STOI["ROM"];
	pathnum[0]=1;
	dijkstra(hh);
	printf("%d %d %d %d\n",pathnum[hh],dst[hh],happy[hh],happy[hh]/pointnum[hh]);
	dfs(hh);
	return 0;
}

 

相关资讯

    暂无相关的资讯...

共有访客发表了评论 网友评论

验证码: 看不清楚?
    -->