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 ...
随机推荐
- 【读书笔记-数据挖掘概念与技术】数据仓库与联机分析处理(OLAP)
之前看了认识数据以及数据的预处理,那么,处理之后的数据放在哪儿呢?就放在一个叫“数据仓库”的地方. 数据仓库的基本概念: 数据仓库的定义——面向主题的.集成的.时变的.非易失的 操作数据库系统VS数据 ...
- 【贪心】PAT 1033. To Fill or Not to Fill (25)
1033. To Fill or Not to Fill (25) 时间限制 10 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 ZHANG, Gu ...
- jquery.sparkline.js简介
jQuery线状图插件Sparkline 官网地址:http://omnipotent.net/jquery.sparkline/ 文档地址:http://omnipotent.net/jquery. ...
- Esper学习之十三:EPL语法(九)
距离上一篇博客已经有很多个月的时间了,由于工作的原因,没怎么腾出手来写博客.再加上自己已计划算法学习为第一要务,更新博客的事情暂时就放缓了脚步.所以各位童鞋大可不必等我的博客,先把文档看起来,有什么不 ...
- java(3) 面向对象
1.super关键字 * 使用super关键字调用父类的成员变量和成员方法.具体格式: super.成员变量 super.成员方法([参数1,参数2...]) * 使用super关键字调用父类的构造方 ...
- ThreadLocal的实现和使用场景
ThreadLocal 内部实现.应用场景和内存泄漏 深入理解线程局部变量:ThreadLocal <Java源码分析>:ThreadLocal /ThreadLocalMap Threa ...
- split陷阱
如果split最后一个为空,则要这么写 String[] lines=line.split(",",-1);
- 8.21 js
2018-8-21 20:05:43 2018-8-21 20:56:30 明天再看!!!! 今天空闲多看了书 <百年孤独> <苏东坡传> 打印结果 shanghai js的 ...
- mysql 小于等于0 不包含null
SELECT count(*) FROM test2 WHERE num<=1;
- Zookeeper可以干什么
在Zookeeper的官网上有这么一句话:ZooKeeper is a centralized service for maintaining configuration information, n ...