1087 All Roads Lead to Rome

#include<bits/stdc++.h>
using namespace std;
const int MAXN=202;
const int INF=1000000000;
int weight[MAXN]={};
map<string,int> sti;
map<int,string> its;
int G[MAXN][MAXN];
int d[MAXN];
int n,k;
int maxhap=-1,num;
double maxavg=0;
vector<int> pre[MAXN];
vector<int> path,temppath;
bool vis[MAXN]={false};
void dij(int v){
	fill(d,d+MAXN,INF);
	d[v]=0;
	for(int i=0;i<n;i++){
		int k=-1;int MIN=INF;
		for(int j=0;j<n;j++){
			if(vis[j]==false&&d[j]<MIN){
				k=j;MIN=d[j];
			}
		}
		if(k==-1) return;
		vis[k]=true;
		for(int j=0;j<n;j++){
			if(vis[j]==false&&G[k][j]!=INF){
				if(d[k]+G[k][j]<d[j]){
					d[j]=d[k]+G[k][j];
					pre[j].clear();
					pre[j].push_back(k);
				}else if(d[k]+G[k][j]==d[j]){
					pre[j].push_back(k);
				}
			}
		}
	}
}
void dfs(int ed){
	if(ed==0){
		temppath.push_back(ed);num++;
		int temphap=0;double tempavg=0;
		for(int i=temppath.size()-1;i>=0;i--){
			temphap+=weight[temppath[i]];
		}
		tempavg=1.0*temphap/(temppath.size()-1);
		if(temphap>maxhap){
			maxhap=temphap;
			maxavg=tempavg;
			path=temppath;
		}else if(temphap==maxhap&&maxavg<tempavg){
			maxavg=tempavg;
			path=temppath;
		}
		temppath.pop_back();
		return;
	}
	temppath.push_back(ed);
	for(int i=0;i<pre[ed].size();i++){
		dfs(pre[ed][i]);
	}
	temppath.pop_back();
}
int main()
{
	//freopen("in.txt","r",stdin);
	cin>>n>>k;
	string start;cin>>start;
	int j=0;
	sti[start]=j;weight[sti[start]]=0;
	its[j]=start;j++;
	for(int i=0;i<n-1;i++){
		string temp;int t;
		cin>>temp>>t;
		sti[temp]=j;its[j]=temp;j++;
		weight[sti[temp]]=t;
	}
	fill(G[0],G[0]+MAXN*MAXN,INF);
	for(int i=0;i<k;i++){
		string _start,end;int cos;
		cin>>_start>>end>>cos;
		int t1=sti[_start];
		int t2=sti[end];
		G[t1][t2]=cos;G[t2][t1]=cos;
	}
	dij(0);
	dfs(sti["ROM"]);
	cout<<num<<' '<<d[sti["ROM"]]<<' '<<maxhap<<' '<<(int)maxavg<<endl;
	for(int i=path.size()-1;i>=0;i--){
		if(i==path.size()-1) cout<<its[path[i]];
		else cout<<"->"<<its[path[i]];
	}
	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值