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> # ...
随机推荐
- Spring boot注解使用
1:@SpringBootApplication 注解 a:scanBasePackages 与scanBasePackageClasses配置Spring启动时扫描的包路径或者扫描的字节码文件 b: ...
- java.util.zip.ZipException: duplicate entry(重复依赖多版本的类库)
同步SVN仓库中的代码,更新后,运行项目,出现如下错误: com.android.build.api.transform.TransformException: java.util.zip.ZipEx ...
- C#控件中的KeyDown、KeyPress 与 KeyUp事件浅谈
研究了一下KeyDown,KeyPress 和 KeyUp 的学问.让我们带着如下问题来说明: 1.这三个事件的顺序是怎么样的? 2.KeyDown 触发后,KeyUp是不是一定触发? 3.三个事件的 ...
- calico 网络流量 过程 分析 apt-get install telnet
1.calico node 容器在kubernetes中以DaemonSet 的方式运行,容器的网络模式为hostNetwor,与host共享网络栈,拥有相同的Ip和hostname 2.查看某个po ...
- (转)公有云vr客户端tcp连接数太多造成 系统卡顿问题 [bittorrent tracker优化] -公有云常见网络问题及思路
在公有云服务器 发现使用tcp(http)的tracker连接数太多 用户太多会造成windows系统卡顿 特此发表一下修改配置和路由器的方法 解决卡顿问题 解决方法1(参考内容): 修改 /etc/ ...
- day76
昨日回顾: 1 ajax 什么是ajax:异步的JavaScript 和xml 2 特点:异步,局部刷新 3 简单的与后台交互:(携带数据:可以拼到url上---->从GET中取,) ...
- odoo 打印格式中 打印第一个数据默认
<table style="width:100%;"> <tr> <td style="word-wrap:break-word;width ...
- Ubuntu学习总结-01 安装Ubuntu
Ubuntu(友帮拓.优般图.乌班图)是一个以桌面应用为主的开源GNU/Linux操作系统,Ubuntu 是基于Debian GNU/Linux,支持x86.amd64(即x64)和ppc架构,由全球 ...
- scala-数组操作
package com.bigdata import scala.collection.mutable.ArrayBuffer object ArrayO { def main(args: Array ...
- kettle学习笔记(一)——入门与安装
一.概述 1.kettle是什么 Kettle是一款国外开源的ETL工具,纯java编写,可以在Window.Linux.Unix上运行,绿色无需安装,数据抽取高效稳定.中文名称叫水壶,该项目的主程序 ...