[图的遍历&多标准] 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 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
分析:这是一道图的遍历题目,但是涉及到多个判断标准。首先,路径长度(花费)最小。其次,获得的happy值最大。再次,happy的平均值最大(不包括开始的城市).虽然增加了判断标准,但是做法还是一样的。该题基于图的深度遍历DFS来做,每次遍历到目的节点时更新判断标准。另外,图的DFS与数的DFS不同的地方在于,图的DFS需要增加一个vis数组用于表示某个节点是否访问过,而树不需要,因为树是不含环的。另外,该题给的是字符串表示的节点,我们可以用map来实现字符串和int型之间的映射,方便编写代码。
- #include <iostream>
- #include <cstdio>
- #include <cmath>
- #include <cstring>
- #include <algorithm>
- #include <vector>
- #include <map>
- using namespace std;
- const int maxn=;
- const int INF=1e9;
- int g[maxn][maxn];
- int happy[maxn];
- int vis[maxn]={false};
- map<string ,int> str2int;
- map<int,string> int2str;
- int st,ed;
- vector<int> tmppath,path;
- int min_cost=INF;
- int max_happy=;
- int avg_happy=;
- int lest_num=;
- int n;
- void dfs(int s,int cost,int hy)
- {
- vis[s]=true;
- if(s==ed)
- {
- if(cost<min_cost)
- {
- min_cost=cost;
- path=tmppath;
- lest_num=;
- max_happy=hy;
- avg_happy=hy/(path.size()-);
- }
- else if(cost==min_cost)
- {
- lest_num+=;
- if(hy>max_happy)
- {
- max_happy=hy;
- path=tmppath;
- avg_happy=hy/(path.size()-);
- }
- else if(hy==max_happy)
- {
- if(tmppath.size()<path.size()&&tmppath.size()>)
- {
- path=tmppath;
- int down=path.size()-;
- avg_happy=hy/down;
- }
- }
- }
- return ;
- }
- for(int v=;v<n;v++)
- {
- if(vis[v]==false&&g[s][v]!=INF)
- {
- tmppath.push_back(v);
- dfs(v,cost+g[s][v],hy+happy[v]);
- vis[v]=false;
- tmppath.pop_back();
- }
- }
- }
- int main()
- {
- fill(g[],g[]+maxn*maxn,INF);
- int k;
- string begin;
- cin>>n>>k>>begin;
- str2int.insert(make_pair(begin,));
- int2str.insert(make_pair(,begin));
- for(int i=;i<n;i++)
- {
- string str;
- int h;
- cin>>str>>h;
- if(str=="ROM") ed=i;
- str2int.insert(make_pair(str,i));
- int2str.insert(make_pair(i,str));
- happy[i]=h;
- }
- for(int i=;i<k;i++)
- {
- string u,v;
- int cost;
- cin>>u>>v>>cost;
- int uu,vv;
- uu=str2int[u];
- vv=str2int[v];
- g[uu][vv]=g[vv][uu]=cost;
- }
- tmppath.push_back();
- dfs(,,);
- cout<<lest_num<<" "<<min_cost<<" "<<max_happy<<" "<<avg_happy<<endl;
- for(int i=;i<path.size();i++)
- {
- if(i>) cout<<"->";
- cout<<int2str[path[i]];
- }
- }
[图的遍历&多标准] 1087. All Roads Lead to Rome (30)的更多相关文章
- 1087. All Roads Lead to Rome (30)
时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Indeed there are many different ...
- 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 ...
- 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 分)(dijkstra+dfs或dijkstra+记录路径)
题意: 输入两个正整数N和K(2<=N<=200),代表城市的数量和道路的数量.接着输入起点城市的名称(所有城市的名字均用三个大写字母表示),接着输入N-1行每行包括一个城市的名字和到达该 ...
- PAT甲级练习 1087 All Roads Lead to Rome (30分) 字符串hash + dijkstra
题目分析: 这题我在写的时候在PTA提交能过但是在牛客网就WA了一个点,先写一下思路留个坑 这题的简单来说就是需要找一条最短路->最开心->点最少(平均幸福指数自然就高了),由于本题给出的 ...
- 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 ...
- pat1087. All Roads Lead to Rome (30)
1087. All Roads Lead to Rome (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- 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
PAT甲级1087. All Roads Lead to Rome 题意: 确实有从我们这个城市到罗马的不同的旅游线路.您应该以最低的成本找到您的客户的路线,同时获得最大的幸福. 输入规格: 每个输入 ...
随机推荐
- spark练习--由IP得到所在地
今天我们就来介绍,如何根据一个IP来求出这个IP所在的地址是什么,首先我们如果要做这个内容,那么我们要有一个IP地址的所在地字典,这个我们可以在网上购买,形如: 1.0.1.0|1.0.3.255|1 ...
- 英语linux+英语firefox+英语Oracle OEM如何设置成显示日语
1 linux安装盘挂载,安装日语语言包 2 linux的系统语言设置为日语 3 firefox的 edit-> setting -> contents -> language se ...
- loj#121.「离线可过」动态图连通性
题面 话说#122怎么做啊 题解 我的\(\mathrm{LCT}\)水平极差,连最小生成树都快忘了,赶紧复习一下 做法和这篇是一样的 这道题还可以练习线段树分治 还可以练习ETT 果然是道吼题 代码 ...
- GPUImage每个类的作用
28 #import "GPUImageBrightnessFilter.h" //亮度 29 #import "GPUImageExpos ...
- Android Studio 导入别人项目时候遇见的问题“Gradle DSL method not found: 'compile()'”
Gradle DSL method not found: 'compile() 遇见这个问题截图: 解决: 在项目的根目录的build.gradle文件中是不是用了compile方法 如果有的话,剪切 ...
- Python 字符串 整数 浮点数
• 几个函数: str() : 将一个整数或者浮点数变成字符串 int() : 将一个浮点数或一个字符串变成整数 float : 将一个整数或者字符串变成一个浮点型数据 • 整数的运算永远是精确的,而 ...
- python代码异常范围检查方法(非常实用)
对于python编程的代码,如果需要进行相应的检查其中的错误或者异常,并且确定出现异常语句的大致范围,主要有以下四种方法: 1.第一种方法:遇错即止(告知原因) try ......(所需检查语句) ...
- 执行sh脚本报“/usr/bin/env: "sh\r": 没有那个文件或目录”错误
出现这个错误的原因是出错的语句后面多了“\r”这个字符,换言之,脚本文件格式的问题,我们只需要把格式改成unix即可: vi xx.sh :set ff :set ff=unix :wq!
- csb反编译为csd,并自动进行资源的删除
好多人都想将csb进行反编译为csd,然后进行资源的清理 目前自己的项目也遇到了类似的问题,所以进行了整理 还有很多不完善的地方,后续会一步步加深 请大家多多指教 下载链接:https://pan.b ...
- jmeter线程组介绍
Jmeter中的测试计划是一直有的,但可以在右侧修改名字,要开始做具体测试设计前,都需要在测试计划下边添加一个线程组,添加路径为鼠标捕获测试计划后,点击鼠标右键->添加->Threads( ...