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]的更多相关文章

  1. 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 ...

  2. 【PAT甲级】1087 All Roads Lead to Rome (30 分)(dijkstra+dfs或dijkstra+记录路径)

    题意: 输入两个正整数N和K(2<=N<=200),代表城市的数量和道路的数量.接着输入起点城市的名称(所有城市的名字均用三个大写字母表示),接着输入N-1行每行包括一个城市的名字和到达该 ...

  3. PAT甲级1087. All Roads Lead to Rome

    PAT甲级1087. All Roads Lead to Rome 题意: 确实有从我们这个城市到罗马的不同的旅游线路.您应该以最低的成本找到您的客户的路线,同时获得最大的幸福. 输入规格: 每个输入 ...

  4. [图的遍历&多标准] 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 ...

  5. PAT 甲级 1087 All Roads Lead to Rome(SPFA+DP)

    题目链接 All Roads Lead to Rome 题目大意:求符合题意(三关键字)的最短路.并且算出路程最短的路径有几条. 思路:求最短路并不难,SPFA即可,关键是求总路程最短的路径条数. 我 ...

  6. PAT 甲级 1087 All Roads Lead to Rome

    https://pintia.cn/problem-sets/994805342720868352/problems/994805379664297984 Indeed there are many ...

  7. PAT (Advanced Level) 1087. All Roads Lead to Rome (30)

    暴力DFS. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...

  8. PAT甲级练习 1087 All Roads Lead to Rome (30分) 字符串hash + dijkstra

    题目分析: 这题我在写的时候在PTA提交能过但是在牛客网就WA了一个点,先写一下思路留个坑 这题的简单来说就是需要找一条最短路->最开心->点最少(平均幸福指数自然就高了),由于本题给出的 ...

  9. 1087 All Roads Lead to Rome

    Indeed there are many different tourist routes from our city to Rome. You are supposed to find your ...

随机推荐

  1. c++ 单步查看汇编代码【转】

    form here 用gdb 查看汇编代码, 采用disassemble 和 x 命令. nexti, stepi 可以单步指令执行 如下例: ---------------------------- ...

  2. 【linux系列】yum安装报错 no mirrors to try

    执行以下命令去重新生成缓存 yum clean all yum makecache 更换源重新下载repo文件 重新生成缓存

  3. javaweb基础 02--javaweb基础概念

    1.WEB资源 * 静态web资源:指web页面中供人们浏览的数据始终是不变(如 html 页面). * 动态web资源:指web页面中供人们浏览的数据是由程序产生的,不同时间点访问web页面看到的内 ...

  4. sendfile Linux函数

    现在流行的 web 服务器里面都提供sendfile 选项用来提高服务器性能,那到底 sendfile 是什么,怎么影响性能的呢? sendfile 实际上是 Linux 2.0+ 以后的推出的一个系 ...

  5. OpenStack Networking – FlatManager and FlatDHCPManager

    最好的分析FlatDHCPManager的源文,有机会把这篇翻译了 =========================== Over time, networking in OpenStack has ...

  6. JavaScript怎样学

    嘿,我最近接到一个 Web 项目,不过老实说,我这两年没怎么接触 Web 编程,听说 Web 技术已经发生了一些变化.听说你是这里对新技术最了解的 Web 开发工程师? 准确地说,我是一名「前端工程师 ...

  7. 3944: Sum[杜教筛]

    3944: Sum Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3471  Solved: 946[Submit][Status][Discuss] ...

  8. Android 源码下载,国内 镜像

    AOSP(Android) 镜像使用帮助 https://lug.ustc.edu.cn/wiki/mirrors/help/aosp 首先下载 repo 工具. mkdir ~/bin PATH=~ ...

  9. jenkins - svn: E170001报错的原因以及解决方案

    1. 什么问题What? 使用Jenkins配置的svn拉取项目,Jenkins报错:svn: E170001; Your credentials to connect to the reposito ...

  10. 微信公众号关联(小游戏 小程序 跳转 盒子 wx.navigateToMiniProgram)

    参考: 公众号关联小程序 关联公众号 关联后,登录小游戏,可在设置-关联设置中看到关联的公众号 在小游戏中使用wx.navigateToMiniProgram wx.navigateToMiniPro ...