PAT 甲级 1087 All Roads Lead to Rome
https://pintia.cn/problem-sets/994805342720868352/problems/994805379664297984
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), 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 recommanded. 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 recommanded 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
代码:
#include <bits/stdc++.h>
using namespace std; #define inf 0x3f3f3f3f
int N, K;
string st;
map<string, int> mp, cost;
map<int, string> pos;
int maze[220][220], vis[220], dis[220], see[220];
int MinStep = 0, cnt = 0;
vector<vector<int> > ans;
vector<int> v; struct Node{
double ave;
int all = 0;
int num;
int n;
}node[100010]; bool cmp(const Node &a, const Node &b) {
if(a.all != b.all) return a.all > b.all;
return a.ave > b.ave;
} void dfs(int act, int step) {
if(step > MinStep) return; if(act == mp["ROM"]) {
cnt ++;
ans.push_back(v);
return;
} for(int i = 1; i <= N; i ++) {
if(maze[act][i] != inf && dis[act] + maze[act][i] == dis[i] && see[i] == 0) {
v.push_back(i);
see[i] = 1;
dfs(i, step + maze[act][i]);
v.pop_back();
see[i] = 0;
}
}
} void dijkstra(int act) {
dis[act] = 0;
int temp = act; for(int i = 1; i <= N; i ++) {
int minn = inf;
for(int j = 1; j <= N; j ++) {
if(dis[j] < minn && vis[j] == 0) {
minn = dis[j];
temp = j;
}
}
vis[temp] = 1;
for(int k = 1; k <= N; k ++) {
if(vis[k] == 0)
dis[k] = min(dis[k], maze[temp][k] + dis[temp]);
}
}
} int main() {
memset(dis, inf, sizeof(dis));
memset(vis, 0, sizeof(vis));
memset(maze, inf, sizeof(maze));
memset(see, 0, sizeof(see));
scanf("%d%d", &N, &K);
cin >> st;
mp[st] = 1;
pos[1] = st;
for(int i = 0; i < N - 1; i ++) {
string city; int val;
cin >> city >> val;
mp[city] = i + 2;
pos[i + 2] = city;
cost[city] = val;
}
for(int i = 0; i < K; i ++) {
string stt, en; int cos;
cin >> stt >> en >> cos;
maze[mp[stt]][mp[en]] = maze[mp[en]][mp[stt]] = cos;
}
dijkstra(1);
MinStep = dis[mp["ROM"]]; dfs(1, 0); for(int i = 0; i < ans.size(); i ++) {
node[i].num = i;
node[i].n = ans[i].size();
for(int j = 0; j < ans[i].size(); j ++) {
node[i].all += cost[pos[ans[i][j]]];
}
node[i].ave = 1.0 * node[i].all / node[i].n;
} sort(node, node + ans.size(), cmp);
printf("%d %d %d %d\n", cnt, MinStep, node[0].all, (int)node[0].ave);
cout << st;
for(int i = 0; i < ans[node[0].num].size(); i ++) {
cout << "->";
cout << pos[ans[node[0].num][i]];
}
return 0;
}
渐渐开始不喜欢最短路
PAT 甲级 1087 All Roads Lead to Rome的更多相关文章
- PAT甲级1087. All Roads Lead to Rome
PAT甲级1087. All Roads Lead to Rome 题意: 确实有从我们这个城市到罗马的不同的旅游线路.您应该以最低的成本找到您的客户的路线,同时获得最大的幸福. 输入规格: 每个输入 ...
- PAT 甲级 1087 All Roads Lead to Rome(SPFA+DP)
题目链接 All Roads Lead to Rome 题目大意:求符合题意(三关键字)的最短路.并且算出路程最短的路径有几条. 思路:求最短路并不难,SPFA即可,关键是求总路程最短的路径条数. 我 ...
- PAT甲级——A1087 All Roads Lead to Rome【30】
Indeed there are many different tourist routes from our city to Rome. You are supposed to find your ...
- 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 ...
- 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 ...
- [图的遍历&多标准] 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 (30 分)(dijkstra+dfs或dijkstra+记录路径)
题意: 输入两个正整数N和K(2<=N<=200),代表城市的数量和道路的数量.接着输入起点城市的名称(所有城市的名字均用三个大写字母表示),接着输入N-1行每行包括一个城市的名字和到达该 ...
- PAT甲级练习 1087 All Roads Lead to Rome (30分) 字符串hash + dijkstra
题目分析: 这题我在写的时候在PTA提交能过但是在牛客网就WA了一个点,先写一下思路留个坑 这题的简单来说就是需要找一条最短路->最开心->点最少(平均幸福指数自然就高了),由于本题给出的 ...
- PAT (Advanced Level) 1087. All Roads Lead to Rome (30)
暴力DFS. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...
随机推荐
- AxisWebservice 发送多参数配置
1.在web.xml中配置代码如下 <servlet> <servlet-name>AxisServlet</servlet-name> <display-n ...
- 向大家推荐一个在.Net下使用C#语言和Managed DirectX 9开发游戏的视频教程
视频教程:3D游戏开发步步高系列课程(微软课堂).美中不足的是视频的声音和画面不太对应.专心的听声音,听老师讲解吧. PPT和源码下载:3D游戏开发步步高系列课程-PPT和源码 网址链接:3D游戏开发 ...
- 使用iozone进行磁盘读写性能测试
对于很多GIS工程师,经常需要对系统的磁盘性能进行测试,为了排查问题或者帮助用户进行系统设计. IOZone这个磁盘性能测试工具就是一个很方便的辅助工具. 下面就以个测试共享目录的读写性能为例,说明其 ...
- PAT B1007 素数对猜想 (20 分)
让我们定义dn为:dn=pn+1−pn,其中pi是第i个素数.显然有d1=1,且对于n>1有dn是偶数.“素数对猜想”认为“存在无穷多对相邻且差为2的素 ...
- openJDK环境搭建编译(fedora)
1.安装VMware VMware-workstation-full-10.0.7-2844087.exe 破解码:HY06L-F334P-9Z6H9-6R2XM-23C6J 安装完成之后, ...
- oracle 将字符串转化为数值型to_number()
select to_number('22.222') from dual
- 基于Docker一键部署大规模Hadoop集群及设计思路
一.背景: 随着互联网的发展.互联网用户的增加,互联网中的数据也急剧膨胀.每天产生的数据量数以万计,本地文件系统和单机CPU已无法满足存储和计算要求.Hadoop分布式文件系统(HDFS)是海量数据存 ...
- VB CompactDatabase 压缩/修复数据库
Option Explicit Private Sub Command1_Click() On Error GoTo err Dim DbEngine, dbFile As String dbFile ...
- 【第十一课】Tomcat原理解析【转】
目录 一.Tomcat顶层架构 二.Tomcat顶层架构小结: 三.Connector和Container的微妙关系 四.Connector架构分析 五.Container架构分析 六.Contain ...
- vue中v-if 和 v-show的区别
简单来说,v-if 的初始化较快,但切换代价高:v-show 初始化慢,但切换成本低 1.共同点 v-if 和 v-show 都可以动态地显示DOM元素 2.区别 (1)手段: v-if 是动态的向D ...