PAT 1087 All Roads Lead to Rome[图论][迪杰斯特拉+dfs]
1087 All Roads Lead to Rome (30)(30 分)
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:
Each input file contains one test case. For each case, the first line contains 2 positive integers N (2<=N<=200), the number of cities, and K, the total number of routes between pairs of cities; followed by the name of the starting city. The next N-1 lines each gives the name of a city and an integer that represents the happiness one can gain from that city, except the starting city. Then K lines follow, each describes a route between two cities in the format "City1 City2 Cost". Here the name of a city is a string of 3 capital English letters, and the destination is always ROM which represents Rome.
Output Specification:
For each test case, we are supposed to find the route with the least cost. If such a route is not unique, the one with the maximum happiness will be recommended. If such a route is still not unique, then we output the one with the maximum average happiness -- it is guaranteed by the judge that such a solution exists and is unique.
Hence in the first line of output, you must print 4 numbers: the number of different routes with the least cost, the cost, the happiness, and the average happiness (take the integer part only) of the recommended route. Then in the next line, you are supposed to print the route in the format "City1->City2->...->ROM".
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遍历出最优路径。
//提交一次代码,
段错误:您的程序发生段错误,可能是数组越界,堆栈溢出(比如,递归调用层数太多)等情况引起
case通过率为70.00%
提交到pat上提示运行时错误和段错误。
知道出错在什么地方了,dfs里,当s=0时,没有进行return ;导致递归调用层数太多,段错误!
#include <iostream>
#include <vector>
#include<map>
#include<string>
#include <algorithm>
using namespace std;
#define inf 99999
map<string,int> name2id;
map<int,string> id2name; int happy[];
int edge[][];
int dist[];//距离
vector<int> pre[];//前驱结点
int cnt=;//最短路径个数。
int happiness=,mavg=;//每个节点到
vector<int> path,temppath;
int vis[];
int d;//目的地
void dfs(int s){
temppath.push_back(s);
if(s==){
cnt++;
int h=;
//起始点是什么意思?
for(int i=;i<temppath.size()-;i++){
h+=happy[temppath[i]];
}
if(h>happiness){
happiness=h;
mavg=h/(temppath.size()-);
path=temppath;
}else if(h==happiness){
if(h/(temppath.size()-)>mavg){
mavg=h/(temppath.size()-);
path=temppath;
}
}
temppath.pop_back();
return ;
}
for(int i=;i<pre[s].size();i++)
dfs(pre[s][i]);
temppath.pop_back();
} int main() {
int n,m;
string s;
cin>>n>>m>>s;
//初始化
//fill(edge[0],edge[0]+n*n,inf);不能这样初始化,因为它是201*201的。!!
fill(edge[],edge[]+*,inf);
fill(dist,dist+,inf);
//将开始城市设置为0,
name2id[s]=;
id2name[]=s;
happy[]=;
string str;
int hap;
for(int i=;i<n;i++){
cin>>str>>hap;
name2id[str]=i;
id2name[i]=str;
happy[i]= hap;
}
string s1,s2;
int tm;
for(int i=;i<m;i++){
cin>>s1>>s2>>tm;
edge[name2id[s1]][name2id[s2]]=tm;
edge[name2id[s2]][name2id[s1]]=tm;
}
// for(int i=0;i<n;i++){
// for(int j=0;j<n;j++)
// cout<<edge[i][j]<<" ";
// cout<<'\n';
// }
d=name2id["ROM"];
dist[]=;
for(int i=;i<n;i++){
int u=-,minn=inf;
for(int j=;j<n;j++)
if(!vis[j]&&dist[j]<minn){
u=j;
minn=dist[j];
}
if(u==-||u==d)break;
vis[u]=;
for(int j=;j<n;j++){
if(!vis[j]&&edge[u][j]!=inf){
if(dist[j]>dist[u]+edge[u][j]){
dist[j]=dist[u]+edge[u][j];
pre[j].clear();
pre[j].push_back(u);
}else if(dist[j]==dist[u]+edge[u][j])
pre[j].push_back(u);
}
}
} dfs(d);
cout<<cnt<<" "<<dist[d]<<" "<<happiness<<" "<<mavg<<'\n';
cout<<s;
for(int i=path.size()-;i>=;i--)
cout<<"->"<<id2name[path[i]];
return ;
}
PAT 1087 All Roads Lead to Rome[图论][迪杰斯特拉+dfs]的更多相关文章
- PAT 1087 All Roads Lead to Rome
PAT 1087 All Roads Lead to Rome 题目: Indeed there are many different tourist routes from our city to ...
- 【PAT甲级】1087 All Roads Lead to Rome (30 分)(dijkstra+dfs或dijkstra+记录路径)
题意: 输入两个正整数N和K(2<=N<=200),代表城市的数量和道路的数量.接着输入起点城市的名称(所有城市的名字均用三个大写字母表示),接着输入N-1行每行包括一个城市的名字和到达该 ...
- PAT甲级1087. All Roads Lead to Rome
PAT甲级1087. All Roads Lead to Rome 题意: 确实有从我们这个城市到罗马的不同的旅游线路.您应该以最低的成本找到您的客户的路线,同时获得最大的幸福. 输入规格: 每个输入 ...
- [图的遍历&多标准] 1087. All Roads Lead to Rome (30)
1087. All Roads Lead to Rome (30) Indeed there are many different tourist routes from our city to Ro ...
- PAT 甲级 1087 All Roads Lead to Rome(SPFA+DP)
题目链接 All Roads Lead to Rome 题目大意:求符合题意(三关键字)的最短路.并且算出路程最短的路径有几条. 思路:求最短路并不难,SPFA即可,关键是求总路程最短的路径条数. 我 ...
- PAT 甲级 1087 All Roads Lead to Rome
https://pintia.cn/problem-sets/994805342720868352/problems/994805379664297984 Indeed there are many ...
- PAT (Advanced Level) 1087. All Roads Lead to Rome (30)
暴力DFS. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...
- PAT甲级练习 1087 All Roads Lead to Rome (30分) 字符串hash + dijkstra
题目分析: 这题我在写的时候在PTA提交能过但是在牛客网就WA了一个点,先写一下思路留个坑 这题的简单来说就是需要找一条最短路->最开心->点最少(平均幸福指数自然就高了),由于本题给出的 ...
- 1087 All Roads Lead to Rome
Indeed there are many different tourist routes from our city to Rome. You are supposed to find your ...
随机推荐
- 利用Metrics+influxdb+grafana构建监控平台(转)
转自http://www.jianshu.com/p/fadcf4d92b0e 这里再配合Influxdb和Grafana可以构建一个非常漂亮的实时监控界面. Grafana监控界面 采集数据(Met ...
- 前端开发利器 Emmet 介绍与基础语法教程
在前端开发的过程中,编写 HTML.CSS 代码始终占据了很大的工作比例.特别是手动编写 HTML 代码,效率特别低下,因为需要敲打各种“尖括号”.闭合标签等.而现在 Emmet 就是为了提高代码编写 ...
- Fiddler 教程(转载,鉴于原作者关闭了访问fiddler系列文章)
阅读目录 Fiddler的基本介绍 Fiddler的工作原理 同类的其它工具 Fiddler如何捕获Firefox的会话 Fiddler如何捕获HTTPS会话 Fiddler的基本界面 Fiddler ...
- VSCode集成TypeScript编译
先安装github客户端和nodeJS客户端吧,直接去官网下载,nodeJS客户端安装完就集成了npm; 查看是否成功: git version node -v npm-v 安装TypeScript ...
- vmware下Ubuntu屏幕分辨率设置
1.查看现有设备 xrandr -q 输出如下: Screen 0: minimum 1 x 1, current 800 x 600, maximum 8192 x 8192 Virtual1 co ...
- vue--自定义验证指令
参考文档: https://cn.vuejs.org/v2/guide/custom-directive.html https://www.cnblogs.com/ilovexiaoming/p/68 ...
- 微信小程序---示例DEMO
转:CSDN的文章: https://blog.csdn.net/rolan1993/article/details/73467867 不错的DEMO: https://github.com/skyv ...
- &与&&, |与||区别
&和|称为短逻辑符,&&及||称为长逻辑符.长逻辑符只比较左边和右边的第一个元素,而短逻辑符会比较所有的 > a<-c(TRUE, FALSE, TRUE, FAL ...
- POJ-2018 Best Cow Fences(二分加DP)
Best Cow Fences Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 10174 Accepted: 3294 Desc ...
- 2017年TOP100summit开幕在即, 15位大咖担任联席主席甄选最值得学习的100个研发案例
从万维网到物联网,从信息传播到人工智能,20年间软件研发行业趋势发生了翻天覆地的变化.大数据.云计算.AI等新兴领域逐渐改变我们的生活方式,Devops.容器.深度学习.敏捷等技术方式和工作理念对软件 ...