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 ...
随机推荐
- 浏览器 User Agent字符串列表
http://www.73207.com/useragent/ http://www.73207.com/useragent/pages/internet-2520explorer/index.htm ...
- C陷阱与缺陷读书笔记
2.1理解函数声明 这一章仔细分析了(*(void(*)())0)();这条语句的含义,并且提到了typedef的一种函数指针类型定义的用法. 我们经常用到的typedef用法是用于指定结构体的类型, ...
- H.264 White Paper学习笔记(一)总览
H.264 White Paper对于264编码器的原理讲的比较透彻,在阅读学习的时候收获很大,这份文献网上有很多了,也有不少人翻译,不过想要理解更清楚我觉得还是得看英文原版的. 首先看一下白皮书里给 ...
- java.util.concurrent.Future Basics
Hereby I am starting a series of articles about future concept in programming languages (also known ...
- C++ vs Python向量运算速度评测
本文的起源来自最近一个让我非常不爽的事. 我最近在改一个开源RNN工具包currennt(http://sourceforge.net/projects/currennt/),想用它实现RNNLM功能 ...
- HTML表格的运用
表格是用<table>标签定义的,表格被划分为行(使用<标签>),html 难度比C语言,JAVA,Oracle容易,但是要会灵活运用并把命令记住,不断测试一些结果 ,编程还是 ...
- Centos7.2修改时区
设置时区同样, 在 CentOS 7 中, 引入了一个叫 timedatectl 的设置设置程序. 用法很简单: # timedatectl # 查看系统时间方面的各种状态 Local time: 四 ...
- codeforces#518 Div2 ABCDE
A---Birthday http://codeforces.com/contest/1068/problem/A 题意: 有n种硬币,m个人.m个人要给Ivan送硬币,每个人送的硬币都要互不相同但数 ...
- java基础解析系列(六)---注解原理及使用
java基础解析系列(六)---注解原理及使用 java基础解析系列(一)---String.StringBuffer.StringBuilder java基础解析系列(二)---Integer缓存及 ...
- matplotlib的下载和安装方法
官网:http://matplotlib.org/ Installation节 Visit the Matplotlib installation instructions. Installing节 ...