PAT甲级1087 All Roads Lead to Rome (30分)|C++实现

一、题目描述

原题链接
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
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值