PAT A1087 All Roads Lead to Rome (标尺较多的复杂Dijkstra + DFS)

总结:

  • 这种题目一看就是Dijkstra + DFS找最短路径的最优解,其复杂之处就在于给出了第二标尺、第三标尺甚至第四标尺,查找最优解的过程比较麻烦。解决办法如下:
  • vector<int> path[maxn]将所有的最短路径path都记录下来
  • 开一个结构体,成员分别为每一个ans的第二标尺计算结果、第三标尺计算结果…并记录下来对应的path编号
  • ans[]数组按要求sort,最后ans[0]即为最优解
  • 按照ans[0]中记录的path编号输出对应的path
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <string>
#include <cstring>
using namespace std;
const int maxn = 201;
const int INF = 1000000000;
int d[maxn], G[maxn][maxn], happiness[maxn] = {0};
bool vis[maxn] = {false};
int N, K;
map<string, int> sti;
map<int, string> its;
vector<int> pre[maxn];
vector<int> path[maxn], tempPath;
int id = 0;

struct ANS{
	int hap, pathID;	//pathID记录下来对应的路径,以防排序后找不到对应的path
	int averageHap;
}ans[maxn];

void change(string s){
	if(sti.find(s) == sti.end()){
		sti[s] = id;
		its[id++] = s;
	}
}

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] == false && d[j] < MIN){
				u = j;
				MIN = d[j];
			}
		}
		if(u == -1)		return;
		vis[u] = true;
		for(int v = 0; v < N; v++){
			if(vis[v] == false && G[u][v] != INF){
				if(d[u] + G[u][v] < d[v]){
					d[v] = G[u][v] + d[u];
					pre[v].clear();
					pre[v].push_back(u);
				}else if(d[u] + G[u][v] == d[v]){
					pre[v].push_back(u);
				}
			}
		}
	}
}
int cnt = 0;
void DFS(int st, int v){
	if(v == st){
		tempPath.push_back(v);
		path[cnt] = tempPath;
		int h = 0, len = (int)tempPath.size();
		for(int i = len - 1; i >= 0; i--){
			int id = tempPath[i];
			h += happiness[id];
		}
		ans[cnt].hap = h;
		ans[cnt].averageHap = h / (len - 1);
		ans[cnt].pathID = cnt;
		cnt++;
		tempPath.pop_back();
		return;
	}
	tempPath.push_back(v);
	for(int i = 0; i < (int)pre[v].size(); i++){
		DFS(st, pre[v][i]);
	}
	tempPath.pop_back();
}

bool cmp(ANS a, ANS b){
	if(a.hap != b.hap)		return a.hap > b.hap;
	else 
		return a.averageHap > b.averageHap;
}

int main(){
	fill(G[0], G[0] + maxn * maxn, INF);
	string start;
	cin >> N >> K >> start;
	getchar();
	change(start);
	for(int i = 0; i < N - 1; i++){
		string str;
		int w;
		cin >> str >> w;
		getchar();
		change(str);
		happiness[sti[str]] = w;
	}
	for(int i = 0; i < K; i++){
		string str1, str2;
		int city1, city2, dis;
		cin >> str1 >> str2 >> dis;
		getchar();
		city1 = sti[str1], city2 = sti[str2];
		G[city1][city2] = G[city2][city1] = dis;
	}
	Dijkstra(0);
	DFS(0, sti["ROM"]);
	sort(ans, ans + cnt, cmp);
	cout << cnt << " " << d[sti["ROM"]] << " " << ans[0].hap << " " << ans[0].averageHap << endl;
	cout << start;
	for(int i = (int)path[ans[0].pathID].size() - 2; i >= 0; i--){
		cout << "->" << its[path[ans[0].pathID][i]];
	}
	return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值