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

capital
n. 首都; 国都; 资本; 资金; 启动资金; 财富; 财产;
adj. 死刑的; 大写的; 顶好的; 极好的;
有点模糊

关键是字符串和序号的转换吧,2个map互相转换,
刷了5道题了,这类题都差不多熟悉了,还是在最短路径的基础上,有其他权值,
这道题幸福,平均幸福
你可以只用dijkstra,在判断路径的时候判断幸福,平均幸福,记录径路,
还可以用dijkstra+dfs的做法,这样思维比较简单。
我觉得注意点还有平均数是不包括刚开始的城市的(废话

===================================================================
dijkstra

#include<iostream>
#include<cstring>
#include<vector>
#include<map>
const int maxn = 205;
const int INF = 0x3f3f3f3f;
using namespace std;
bool vis[maxn]={false};
map<string,int> cti;
map<int,string> itc;
int d[maxn],weight[maxn],w[maxn],G[maxn][maxn],num[maxn],p[maxn],pre[maxn];
int N,K;
void dijkstra(int s){
	fill(d,d+maxn,INF);
	//for(int i=0;i<N;i++) pre[i] = i;
	d[s]=0;
	w[s]=weight[s];
	num[s]=1;
	for(int i=0;i<N;i++){
		int u=-1,min=INF;
		for(int j=0;j<N;j++){
			if(!vis[j] && d[j] < min){
				u=j;
				min=d[j];
			}
		}
		if(u == -1) return ;
		vis[u] = true;
		for(int i=0;i<N;i++){
			if(!vis[i] && G[u][i] !=INF){
				if(d[u] + G[u][i] < d[i]){
					pre[i] = u;
					d[i]=d[u] + G[u][i];
					w[i]=w[u] + weight[i];
					num[i] = num[u];
					p[i]=p[u]+1;
				}else if(d[u] + G[u][i] == d[i] ){
					num[i]+=num[u];
					if(w[i] < w[u] + weight[i]){
						w[i]=w[u]+weight[i];
					    pre[i]=u;
					    p[i]=p[u]+1;
					}
					else if(w[i] == w[u]+weight[i]){
						double uavg=1.0* (w[u] + weight[i]) / (p[u]+1);
						double iavg=1.0* w[i] / p[i];
						if(uavg > iavg){
							p[i]=p[u]+1;
							pre[i]=u;
						}
					}					
				}
			}
		}
	}
}
void DFS(int v){
	if(v == 0){
		cout<<itc[v];
		return;
	}
	DFS(pre[v]);
	cout<<"->"<<itc[v];
}
int main(){
	string c,s;
	int ww;
	cin>>N>>K>>c;
	cti[c]=0;
	itc[0]=c;
	fill(G[0],G[0]+maxn*maxn,INF);
	for(int i=1;i<=N-1;i++){
		cin>>c>>weight[i];
		cti[c]=i;
		itc[i]=c;
	}
	for(int i=0;i<K;i++){
		cin>>c>>s>>ww;
		int cid=cti[c],sid=cti[s];
		G[cid][sid]=ww;
		G[sid][cid]=ww;
	}
	dijkstra(0);
	int roma=cti["ROM"];
	cout<<num[roma]<<' '<<d[roma]<<' '<<w[roma]<<' '<<(int)(w[roma]/p[roma])<<endl;
	DFS(roma);
	return 0;
} 

===================================================================
dijkstra+DFS

#include<iostream>
#include<cstring>
#include<vector>
#include<map>
const int maxn = 205;
const int INF = 0x3f3f3f3f;
using namespace std;
bool vis[maxn]={false};
map<string,int> cti;
map<int,string> itc;
int d[maxn],weight[maxn],G[maxn][maxn];
vector<int> pre[maxn];
vector<int> path,tempath;
int N,K,maxhappy,maxavg,num;
void dijkstra(int s){
	fill(d,d+maxn,INF);
	d[s]=0;
	for(int i=0;i<N;i++){
		int u=-1,min=INF;
		for(int j=0;j<N;j++){
			if(!vis[j] && d[j] < min){
				u=j;
				min=d[j];
			}
		}
		if(u == -1) return;
		vis[u]=true;
		for(int i=0;i<N;i++){
			if(!vis[i] && G[u][i] != INF){
			  if(d[u] + G[u][i] < d[i]){
			  	d[i] = d[u] + G[u][i];
				pre[i].clear();
				pre[i].push_back(u);
			  }else if( d[u] + G[u][i] == d[i]){
				pre[i].push_back(u);
			}	
			}
		}
	}
}
void DFS(int v){
	if(v == 0){
		num++;
		tempath.push_back(v);
		int tempmax=0,tempavg=0;
		for(int i = tempath.size() - 2;i>=0;i--){
			tempmax+=weight[tempath[i]];
		}
		tempavg= 1.0*tempmax/ (tempath.size()-1) ;
		if(tempmax > maxhappy) {
			maxhappy = tempmax;
			maxavg=tempavg;
			path=tempath;
		}
		else if(tempmax == maxhappy && maxavg < tempavg){
			maxavg = tempavg;
			path = tempath;
		}
		tempath.pop_back();
		return;
	}
	tempath.push_back(v);
	for(int i=0;i<pre[v].size();i++){
		DFS(pre[v][i]); 
	} 	 
	tempath.pop_back();
}
int main(){
	fill(G[0],G[0]+maxn*maxn,INF);
	string c,s;
	cin>>N>>K>>c;
	cti[c]=0;
	itc[0]=c;
	int w;
	for(int i=1;i<=N-1;i++){
		cin>>c>>weight[i];
		cti[c]=i;
		itc[i]=c;
	}
	for(int i=0;i<K;i++){
		cin>>c>>s>>w;
		int cid=cti[c],sid=cti[s];
		G[cid][sid]=w;
		G[sid][cid]=w;
	}
	dijkstra(0);
	int roma=cti["ROM"];
	DFS(roma);
	cout<<num<<' '<<d[roma]<<' '<<maxhappy<<' '<<(int)maxavg<<endl;
	for(int i=path.size()-1;i>=0;i--){
		if(i) cout<<itc[path[i]]<<"->";
		else cout<<itc[path[i]];
	}
	return 0;
} 

敲的我脑壳疼,在看了别人思路之后还是敲了很长时间,写了函数,main函数没调用,emmmm,这道题最主要的收获还是map吧,虽说知道但是不大会用。最短路径感觉差不多了,虽说大部分不是自己独立做出来的,但是慢慢来吧,我也想自己主动做出来,但是目前能力不打允许,还是先借鉴优秀的做法,慢慢积累吧。接下来弄图的遍历,连通图,并查集。

参考:
算法笔记实战指南P377

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值