一、题目描述
原题链接
Indeed there are many different tourist routes from our city to Rome. You are supposed to find your clients the route with the least cost while gaining the most happiness.
Input Specification:
Output Specification:
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
二、解题思路
dfs和Dijkstra算法的综合题,题目有点复杂,但是前面有一道题目和这道题很类似。对于地点,我们建立一个字符串到数字的映射和一个数字到字符串的映射,完成对地点的编号。先用Dijkstra算法找到最短路,并且记录每一个点的前一个点(这样我们就可以还原这条路)。从终点做一个dfs,当搜索到起点的时候,对记录的路径做一个循环,求出幸福感总和,更新路径即可。
三、AC代码
#include<iostream>
#include<cstdio>
#include<unordered_map>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn = 220;
const int INF = 99999999;
unordered_map<string, int> mp; //对每个地点进行编号
int G[maxn][maxn]; //邻接表
int dis[maxn]; //Dijkstra算法cost数组
int happiness[maxn]; //每个地点的幸福感指数
bool vis[maxn]; //Dijkstra算法标记是否已经访问过
vector<int> pre[maxn], temppath, path; //每个地点的前一个地点 路径
string namemap[maxn]; //每个编号对应的地点(字符串)
int maxvalue = 0, cntpath = 0; //最大幸福感,路径数目