A1087 All Roads Lead to Rome (30分)

#include<cstdio>
#include<stdlib.h>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<set>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
using namespace std;
//A1087
const int maxn=300;
const int INF=1e9;
int n,k;
string startcity;
map<string,int> strToint;//将城市名转为int
map<int,string> intTostr;//将int转为城市名

int weight[maxn]={0};//开始城市为0 
int graph[maxn][maxn];
bool visit[maxn]={false};
int d[maxn];

vector<int> pre[maxn];//存储每个节点的所有最优路径的前驱节点 

void findMin(int start){ //计算从start到每个居民点的最短距离
	//初始化d
	memset(visit,false,sizeof(visit)); 
	fill(d,d+maxn,INF);
	d[start]=0;
	
	for(int i=0;i<n;i++){//节点从0-n-1 
		//找到当前未访问节点中的最小值
		int pos=-1,min=INF;
		for(int j=0;j<n;j++){
			if(visit[j]==false&&d[j]<min){
				min=d[j];
				pos=j;
			}
		} 
		if(pos==-1)return;
		visit[pos]=true;
		
		for(int j=0;j<n;j++){
			if(visit[j]==false && graph[pos][j]!=INF){
				//进行最短路的替换 
				if(d[j]>d[pos]+graph[pos][j]){
					d[j]=d[pos]+graph[pos][j];
					pre[j].clear();
					pre[j].push_back(pos);
					
				}else if(d[j]==d[pos]+graph[pos][j]){
					//如果想等:说明多出一条最优路径
					pre[j].push_back(pos); 
				}
			}
		} 
	}
	
}
//DFS(aim) aim是目标城市:ROM:strToint[ROM] 
vector<int> temp,ans;//记录临时最优路径和最终的最优路径 
int ansHappy=-1;
int ansAver=-1;
int numPath=0;
void DFS(int index){
	temp.push_back(index);
	if(index==0){
		//到达起点==叶子结点
		//计算最优值
		numPath++;//说明最优路径+1 
		int tempHappy=0;
		int tempAver=0;
		for(int i=temp.size()-1;i>=0;i--){
//			cout<<intTostr[temp[i]]<<"->";
			tempHappy+=weight[temp[i]];
			//cout<<"weight["<<temp[i]<<"]"<<weight[temp[i]]<<endl; 
		}
		
		tempAver=tempHappy/(temp.size()-1);//计算平均值 :路径上的节点个数 
//		cout<<"tempHappy="<<tempHappy<<"tempAver="<<tempAver<<endl;
		if(tempHappy>ansHappy){
			ansHappy=tempHappy;
			ansAver=tempAver;//虽然不涉及到平均值,但是还是需要更新的 
			ans=temp;
		}else if(tempHappy==ansHappy&& tempAver>ansAver){
			//两个的cost一样,点权之和也一样,计算平均值
			ans=temp;
			ansAver=tempAver;
		}
		temp.pop_back();
		return; 
	}
	
	//遍历所有的前驱节点
	for(int i=0;i<pre[index].size();i++){
		int precity=pre[index][i];//前一个城市的index值 
		DFS(precity); 
	} 
	temp.pop_back();
	
}
int main()
{

	fill(graph[0],graph[0]+maxn*maxn,INF);
	cin>>n>>k>>startcity;
//开始城市的赋值	
	strToint[startcity]=0;
	intTostr[0]=startcity;
	weight[0]=0;
	for(int i=1;i<=n-1;i++){
		//记录除了开始节点以外其他节点的weight
		string city;
		int happy;
		cin>>city>>weight[i];
//		cout<<city<<":"<<weight[i]<<endl;
		strToint[city]=i;
		intTostr[i]=city; 
	}
	string city1,city2;
	int cost;
	for(int i=0;i<k;i++){
		cin>>city1>>city2>>cost;
		int c1=strToint[city1];
		int c2=strToint[city2];
		graph[c1][c2]=graph[c2][c1]=cost;
	}
	
	findMin(0);
	int aim=strToint["ROM"];
//	cout<<"aim="<<aim<<endl;
	DFS(aim);// aim是目标城市:ROM:strToint[ROM] 
	cout<<numPath<<" "<<d[aim]<<" "<<ansHappy<<" "<<ansAver<<endl;
	
	for(int i=ans.size()-1;i>=0;i--){
		if(i!=ans.size()-1)printf("->");
		cout<<intTostr[ans[i]];
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值