1087 All Roads Lead to Rome (30分)

12 篇文章 0 订阅
6 篇文章 0 订阅

在这里插入图片描述
常规思路,Dijkstra + dfs

  • 先用Dijkstra找出totalCost最小的所有路线,用pre数组记录他们的前驱关系。
  • 用dfs从得到的路线中找出totalHappiness最大的,或者avgHappiness最大的。
  • 注意一些细节,比如输入并没有给出起点城市的happiness
#include<iostream>
#include<string>
#include<unordered_map>
#include<vector>
using namespace std;
const int INF = 0x7fffffff;
int numCity, numRoute, source, dest;
unordered_map<int, string> id2name;
unordered_map<string, int> name2id;
int id = 0;
int happiness[210];
int map[210][210];
int totalCost[210];
vector<int> pre[210];
bool visited[210];
int getId(string name) {
	if (name2id.count(name) == 0) {
		id2name[id] = name;
		name2id[name] = id++;
	}
	return name2id[name];
}
string getName(int id) {
	return id2name[id];
}
int routeCount = 0;
int maxHappiness = 0, maxAvgHappiness = 0;
vector<int> bestRoute;
void dfs(int cur, int curHappiness, int passByCount, vector<int> route) {
	if (cur == source) {
		++routeCount;
		int avgHappiness = curHappiness / (passByCount - 1);
		if (curHappiness > maxHappiness || curHappiness == maxHappiness && avgHappiness > maxAvgHappiness) {
			maxHappiness = curHappiness;
			maxAvgHappiness = avgHappiness;
			bestRoute = route;
		}
		return;
	}
	for (int i = 0; i < pre[cur].size(); i++) {
		int next = pre[cur][i];
		route.push_back(next);
		dfs(next, curHappiness + happiness[next], passByCount + 1, route);
		route.pop_back();
	}
}
int main() {
	fill(map[0], map[0] + 210 * 210, INF);
	fill(visited, visited + 210, false);
	fill(totalCost, totalCost + 210, INF);
	string startingCity;
	cin >> numCity >> numRoute >> startingCity;
	source = getId(startingCity);
	dest = getId("ROM");
	for (int i = 0; i < numCity - 1; i++) {
		string temp;
		int h;
		cin >> temp >> h;
		happiness[getId(temp)] = h;
	}
	for (int i = 0; i < numRoute; i++) {
		int cost;
		string s1, s2;
		cin >> s1 >> s2 >> cost;
		int c1 = getId(s1), c2 = getId(s2);
		map[c1][c2] = cost;
		map[c2][c1] = cost;
	}

	//Dijkstra
	totalCost[source] = 0;
	for (int i = 0; i < numCity; i++) {
		int u = -1, minCost = INF;
		for (int j = 0; j < numCity; j++) {
			if (!visited[j] && totalCost[j] < minCost) {
				u = j;
				minCost = totalCost[j];
			}
		}
		if (u == -1) {
			break;
		}
		visited[u] = true;
		for (int v = 0; v < numCity; v++) {
			if (!visited[v] && map[u][v] != INF) {
				if (totalCost[v] > totalCost[u] + map[u][v]) {
					totalCost[v] = totalCost[u] + map[u][v];
					pre[v].clear();
					pre[v].push_back(u);
				}
				else if (totalCost[v] == totalCost[u] + map[u][v]) {
					pre[v].push_back(u);
				}
			}
		}
	}
	vector<int> r;
	r.push_back(dest);
	dfs(dest, happiness[dest], 1, r);
	printf("%d %d %d %d\n", routeCount, totalCost[dest], maxHappiness, maxAvgHappiness);
	for (int i = bestRoute.size() - 1; i >= 0; i--) {
		cout << getName(bestRoute[i]);
		if (i != 0) {
			cout << "->";
		}
	}
}

二刷:
用深度优先搜索,记得在每次退出前visited.erase(cur),否则其他路径不能经过这些结点。

#include<iostream>
#include<string>
#include<unordered_set>
#include<unordered_map>
#include<vector>
using namespace std;
const int inf = 0x7fffffff;
int numCity, numRoute;
string source;
unordered_map<string, int> city2Happiness;
unordered_map<string, unordered_map<string, int> > map;
int minCost = inf, maxHappiness = 0, numPassby = inf, bestRouteCount;
vector<string> bestRoute;
unordered_set<string> visited;
void dfs(string cur, int happiness, int cost, int cityCount, vector<string> route) {
	route.push_back(cur);
	if (cur == "ROM") {
		if (cost < minCost) {
			bestRouteCount = 1;
			numPassby = cityCount;
			maxHappiness = happiness;
			minCost = cost;
			bestRoute = route;
		}
		else if (cost == minCost) {
			++bestRouteCount;
			if (maxHappiness < happiness
				|| maxHappiness == happiness
				&& (double)maxHappiness / numPassby < (double)happiness / cityCount) {
				maxHappiness = happiness;
				numPassby = cityCount;
				bestRoute = route;
			}
		}
		return;
	}
	visited.insert(cur);
	for (auto next : map[cur]) {
		if (visited.count(next.first) == 0) {
			string nextCity = next.first;
			dfs(nextCity, happiness + city2Happiness[nextCity], cost + map[cur][nextCity], cityCount + 1, route);
		}
	}
	visited.erase(cur);
}
int main() {
	cin >> numCity >> numRoute >> source;
	for (int i = 0; i < numCity - 1; i++) {
		string city;
		int h;
		cin >> city >> h;
		city2Happiness[city] = h;
	}
	for (int i = 0; i < numRoute; i++) {
		string c1, c2;
		int c;
		cin >> c1 >> c2 >> c;
		map[c1][c2] = c;
		map[c2][c1] = c;
	}
	vector<string> temp;
	dfs(source, 0, 0, 0, temp);
	printf("%d %d %d %d\n", bestRouteCount, minCost, maxHappiness, maxHappiness / numPassby);
	for (int i = 0; i < bestRoute.size(); i++) {
		if (i != 0) {
			cout << "->";
		}
		cout << bestRoute[i];
	}
	cout << endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值