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 ...
随机推荐
- css笔记 - animation学习笔记(二)
animation动画 @keyframes规则 - 创建动画 from - to 等价于 0% - 100% 但是优先使用0% - 100%,因为浏览器兼容性还好点 animation 动画绑定 将 ...
- Python pyQt4/pyQt5 学习笔记2(状态栏、菜单栏和工具栏)
例子:状态栏.菜单栏和工具栏 import sys from PyQt4 import QtGui class Example(QtGui.QMainWindow): def __init__(sel ...
- LeetCode 10 Regular Expression Matching(字符串匹配)
题目链接 https://leetcode.com/problems/regular-expression-matching/?tab=Description '.' Matches any si ...
- sencha touch 可自动增长高度TextArea
js代码如下: /* *高度自动增长的文本框 */ Ext.define('ux.TextArea', { extend: 'Ext.field.TextArea', xtype: 'autoText ...
- VC++ 多线程编程,win32,MFC 例子(转)
一.问题的提出 编写一个耗时的单线程程序: 新建一个基于对话框的应用程序SingleThread,在主对话框IDD_SINGLETHREAD_DIALOG添加一个按钮,ID为IDC_SLEEP_SIX ...
- Centos 7.x临时的网络与路由配置
今天在虚拟机上安装了Centos 7.1操作系统,使用的最小化安装,安装完成后准备使用ifconfig命令时,发现命令不存在,如下: 心想肯定是新版的Centos 系统默认情况下没有使用ifconfi ...
- 【CF886E】Maximum Element DP
[CF886E]Maximum Element 题意:小P有一个1-n的序列,他想找到整个序列中最大值的出现位置,但是他觉得O(n)扫一遍太慢了,所以它采用了如下方法: 1.逐个遍历每个元素,如果这个 ...
- Unity3D笔记 英保通四 虚拟轴应用及键盘事件
Input: 1.使用这个类能够读取输入管理器设置的按键,以及访问移动设备的多点触控或加速感应数据.想要读取轴向使用Input.GetAxis方法获取下列默认轴: "Horizontal&q ...
- vue---结合elementui做异步数据分页
使用vue+elementui来请求数据做分页: <el-col :span="24" class="toolbar pageBar"> <e ...
- npm使用报错解决办法
在使用脚手架工具进行项目搭建的时候,很多时候会用到npm ,最近用npm的时候遇到一个错误: 'CALL "I:\Program Files\nodejs\\node.exe" & ...