[C++] PAT All Roads Lead to Rome (30分)

在这里插入图片描述

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

题解

Dijkstra+DFS,在DFS中判断的时候先判断happiness之后是cost

#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <stack>
using namespace std;

const int MAXN = 210;
const int inf = 999999;

map<string,int> s2i;
map<int,string> i2s;
int G[MAXN][MAXN];
int happiness[MAXN] = {0};
int d[MAXN];
vector<int> pre[MAXN];
int path_num[MAXN];
bool visit[MAXN] = {false};
int l_cost=inf,h_happiness=-1;
double a_happiness = 0;
int n,m;
int c_index = 0;
vector<int> path_stack;
int city_from_index;
vector<int> best_path;

int string2int(string s){
	if(s2i.find(s) == s2i.end()){
		s2i[s] = c_index;
		i2s[c_index] = s;
		c_index++;
	}

	return s2i[s];
}

string int2string(int i){

	return i2s[i];
}


void dijkstra(int from){
	fill(d,d+MAXN,inf);
	fill(path_num,path_num+MAXN,0);
	d[from] = 0;
	path_num[from] = 1;

	for(int i=0;i<n;i++){
		int u=-1,min_d = inf;
		for(int j=0;j<n;j++){
			if(visit[j] == false && d[j] < min_d){
				u = j;
				min_d = d[j];
			}
		
		}
		if(u == - 1) return;

		visit[u] = true;
		for(int j = 0;j<n;j++){
			if(visit[j] == false && G[u][j] != -1){
				if(d[u] + G[u][j] < d[j]){
					d[j] = d[u] + G[u][j];
					pre[j].clear();
					pre[j].push_back(u);
					path_num[j] = path_num[u];
				
				}
				else if(d[u] + G[u][j] == d[j]){
					pre[j].push_back(u);
					path_num[j] += path_num[u];
				}
			}
			
		}
	}

}

void DFS(int s){

	path_stack.push_back(s);

	if(s == city_from_index){
		
		int temp_cost = 0, temp_happiness = 0;
		double temp_avehappiness = 0;

		for(int i=path_stack.size() - 1;i>0;i--){
			int t_f = path_stack[i],t_t = path_stack[i-1];
			temp_cost += G[t_f][t_t];
			temp_happiness += happiness[t_t];
		}

		temp_avehappiness = temp_happiness *1.0 / (path_stack.size() - 1);


		if(temp_happiness > h_happiness ){
			best_path = path_stack;
			l_cost = temp_cost;
			h_happiness = temp_happiness;
			a_happiness = temp_avehappiness;
		}
		else if(temp_happiness == h_happiness && temp_cost < l_cost){
			best_path = path_stack;
			l_cost = temp_cost;
			h_happiness = temp_happiness;
			a_happiness = temp_avehappiness;
		}
		else if(temp_happiness == h_happiness && temp_cost < l_cost && temp_avehappiness > a_happiness){
			best_path = path_stack;
			l_cost = temp_cost;
			h_happiness = temp_happiness;
			a_happiness = temp_avehappiness;
		}

		path_stack.pop_back();

		return;
	}


	for(int i=0;i<pre[s].size();i++){
		DFS(pre[s][i]);
	}

	path_stack.pop_back();

	

}


int main(){

	fill(G[0],G[0] + MAXN*MAXN,-1);

	int h,index,cost,temp_from_index,temp_to_index;
	string city_from,temp_c,temp_from,temp_to;

	cin >> n >> m;

	cin >> temp_c;

	city_from_index = string2int(temp_c);

	for(int i=0;i<n-1;i++){
		cin >> city_from >> h;
		index = string2int(city_from);
		happiness[index] = h;
	}

	for(int i=0;i<m;i++){
		cin >> temp_from >> temp_to >> cost;
		temp_from_index = string2int(temp_from);
		temp_to_index = string2int(temp_to);

		G[temp_from_index][temp_to_index] = G[temp_to_index][temp_from_index] = cost;
		G[temp_from_index][temp_to_index] = G[temp_to_index][temp_from_index] = min(G[temp_to_index][temp_from_index],cost);

	}

	dijkstra(city_from_index);

	
	index = string2int("ROM");
	DFS(index);
	
	
	printf("%d %d %d %d\n",path_num[best_path[0]],l_cost,h_happiness,int(a_happiness));
	
	for(int i=best_path.size() -1;i>=0;i--){
		cout << int2string(best_path[i]);
		if(i != 0){
			cout << "->";
		}
	}

	


	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值