常规思路,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;
}