PAT1087 All Roads Lead to Rome (30)(最短路径+dfs+回溯)
题意:
有N个城市,M条无向边,从某个给定的起始城市出发,前往名为ROM的城市。每个城市(除了起始城市)都有一个点权(称为幸福值),和边权(每条边所需的花费)。求从起点到ROM所需要的最少花费,并输出其路径。如果路径有多条,给出幸福值最大的那条。如果仍然不唯一,选择路径上的城市平均幸福值最大的那条路径。
思路:
这种题,如果简单一点,要求没那么多的情况,可以直接在dijkstra中写用dp的思想顺便求出幸福值最大的情况,但是这题还有平均幸福值,所以只能先用dijkstra求出最小花费的所有路径,再利用dfs回溯进行求解。
#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=250;
int road[maxn][maxn];
int cost[maxn];
int dis[maxn];
bool vis[maxn];
int n,m;
map<string,int> m1;
map<int,string> m2;
vector<int> pre[maxn];
vector<int> temppath,path;
int maxhap,pathnum;
double avghap;void dijkstra(int start){fill(dis,dis+maxn,inf);fill(vis,vis+maxn,false);dis[start]=0;for(int i=0;i<n;i++){int u=-1,minn=inf;for(int j=0;j<=n;j++){if(!vis[j]&&dis[j]<minn){minn=dis[j];u=j;}}vis[u]=true;for(int v=0;v<=n;v++){if(!vis[v]){if(dis[v]>dis[u]+road[u][v]){dis[v]=dis[u]+road[u][v];pre[v].clear();pre[v].push_back(u);}else if(dis[v]==dis[u]+road[u][v]){pre[v].push_back(u);}}}}
}void dfs(int v){temppath.push_back(v);if(v==1){int sum=0;for(int i=0;i<temppath.size();i++){sum+=cost[temppath[i]];}double avg=(double)sum/(temppath.size()-1);if(sum>maxhap){maxhap=sum;path=temppath;avghap=avg;}else if(sum==maxhap&&avg>avghap){avghap=avg;path=temppath;}pathnum++;return;}for(int i=0;i<pre[v].size();i++){int u=pre[v][i];dfs(u);temppath.pop_back();}
}int main(){string start,temp;cin>>n>>m>>start;m1[start]=1;m2[1]=start;for(int i=1;i<n;i++){cin>>temp>>cost[i+1];m1[temp]=i+1;m2[i+1]=temp;}fill(road[0],road[0]+maxn*maxn,inf);string sa,sb;int d;for(int i=0;i<m;i++){cin>>sa>>sb>>d;road[m1[sa]][m1[sb]]=d;road[m1[sb]][m1[sa]]=d;}dijkstra(1);dfs(m1["ROM"]);printf("%d %d %d %d\n", pathnum, dis[m1["ROM"]], maxhap, (int)avghap);for (int i = path.size() - 1; i >= 1; i--) {cout << m2[path[i]] << "->";}cout << "ROM" << endl;return 0;
}
转载于:https://www.cnblogs.com/seasonal/p/10343614.html
PAT1087 All Roads Lead to Rome (30)(最短路径+dfs+回溯)相关推荐
- pat 1087. All Roads Lead to Rome (30)
pat 1087. All Roads Lead to Rome (30) 时间限制 200 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...
- pat 1087. All Roads Lead to Rome (30) 解答
这题和之前一题紧急救援有点像 这里用的变形的dijkstra 每个节点设置一个vector,遇到相同最短路的,就在vector记录来自哪个节点: 这样 dijkstra结束后,就可以从终点往前推出 所 ...
- 1087 All Roads Lead to Rome (30 分)
题目链接: 题目详情 - 1087 All Roads Lead to Rome (30 分) (pintia.cn)https://pintia.cn/problem-sets/9948053427 ...
- PAT甲级1087 All Roads Lead to Rome (30分):[C++题解]dijkstra求单源最短路综合、最短路条数、保存路径
文章目录 题目分析 题目链接 题目分析 来源:acwing 分析: 首先这是一道dijkstra算法求最短路的题目,不过此题较为复杂.首先需要将字符串城市名映射成数字,这里使用hash table 名 ...
- 【PAT甲级 单源最短路径】1087 All Roads Lead to Rome (30 分)
字符串索引图,使用了map映射,用了一些不常用的知识,能这么写出来很开心 这么写可以使得map的默认值为INF而不是值初始化的0 struct defaultCost{int cost;default ...
- All Roads Lead to Rome (30)
题目描述 Indeed there are many different tourist routes from our city to Rome. You are supposed to find ...
- 1087 All Roads Lead to Rome (30 分)【难度: 一般 / Dijkstra】
https://pintia.cn/problem-sets/994805342720868352/problems/994805379664297984 PAT这种类型地题出了很多次了. 思路: 先 ...
- PAT甲级1002 All Roads Lead to Rome
All Roads Lead to Rome (30) 时间限制 1000 ms 内存限制 65536 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小) 题目描述 Inde ...
- PAT A1087 All Roads Lead to Rome
1087 All Roads Lead to Rome 分数 30 作者 CHEN, Yue 单位 浙江大学 Indeed there are many different tourist route ...
最新文章
- python在线编译-在线编译python
- MATLAB实战系列(九)-遗传算法(GA)求解旅行商问题(TSP)思路解析
- VTK:图片之ImageContinuousErode3D
- leetcode 303. 区域和检索 - 数组不可变
- 小程序 background-image背景图片设置的注意点
- java map reduce 原理_MapReduce实现原理详解
- python零基础8分钟基础入门
- jQuery中的 ||
- PhpSpreadsheet如何读取excel文件
- 全网首发:无线网桥的延迟太大,有时达到10秒以上
- ios富文本的简单使用 AttributedString
- Atitit pg10分区 总结 1.1. create table tmp_log ( 	1 1.2. -创建索引	1 1.3. 查看表 in pgadmin4	2 2. 二 分区表管理	2 2.1
- 成员变量和局部变量详解
- javascript去除空格
- 弹性力学第五版pdf_弹性力学txt-弹性力学pdf-谁知我电子书
- DevCon 命令与批处理自动禁用启用网卡
- Java 金额转换帮助类(元转分)
- 计算机英语videos啥意思,video是什么意思_video翻译_读音_用法_翻译
- delphi删除文本内容_文本编辑器EmEditor快捷键指令
- 云原生一站式DevOps平台----云效