当前位置: 首页 > news >正文

PAT A1087 All Roads Lead to Rome

1087 All Roads Lead to Rome

分数 30

作者 CHEN, Yue

单位 浙江大学

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 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

 

将字符串转化为数字下标进行存储,用map来实现int和string之间的映射关系;
 *
 * 当题目中要 求超过三个标尺时,先用Dijkstra算法求出每个顶点的前驱节点,
 * 即u是由哪个点哪个点(哪些点)转化过来能使d[u] 的值变小,使d[u]变小的
 * 点就是应该求的u的前驱节点;

/*** 将字符串转化为数字下标进行存储,用map来实现int和string之间的映射关系;* * 当题目中要 求超过三个标尺时,先用Dijkstra算法求出每个顶点的前驱节点,* 即u是由哪个点哪个点(哪些点)转化过来能使d[u] 的值变小,使d[u]变小的* 点就是应该求的u的前驱节点;
*/#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>using namespace std;const int N = 210, INF = 1e9;
int g[N][N], d[N], w[N]; //图,距离数组,幸福值数组
bool hs[N]; 
int Nv, Ne, st, ed; //顶点数目,边数目,起点,终点
int idx; //字符转化为数字的下标编号//最小花费,最大幸福值,最大平均幸福值,最短路径数目
int MINC = INF, MAXH, MAXA, num; 
vector<int> temp, path; //路径编号
vector<int> pre[N]; //前驱节点
map<int, string> idx_city; //字符串与数字编号的一一映射
map<string, int> city_idx;int add(string s)
{city_idx[s] = idx;idx_city[idx] = s;return idx++;
}void Read()
{fill(*g, *g+N*N, INF); //初始化string sta;cin >> Nv >> Ne >> sta;st = add(sta);for(int i=1; i<Nv; ++i){string s;int hap;cin >> s >> hap;int u = add(s);w[u] = hap;}ed = city_idx["ROM"];for(int i=0; i<Ne; ++i){string a, b;int u, v, cost;cin >> a >> b >> cost;u = city_idx[a], v = city_idx[b];g[u][v] = g[v][u] = min(g[u][v], cost);}
}void Dijkstra(int st)
{fill(d, d+N, INF);d[st] = 0;for(int i=0; i<Nv; ++i){int MIN = INF, u = -1;for(int j=0; j<Nv; ++j)if(hs[j] == 0 && d[j] < MIN){MIN = d[j];u = j;}if(u == -1) return;hs[u] = 1;for(int j=0; j<Nv; ++j)if(hs[j] == 0 && g[u][j] != INF){int cost = g[u][j];if(d[u] + cost < d[j]){d[j] = d[u] + cost;pre[j].clear();pre[j].push_back(u);}else if(d[u] + cost == d[j])pre[j].push_back(u);}}
}void dfs(int v)
{if(v == st){temp.push_back(v);++num;  int cost = 0, hap = 0;for(int j=temp.size()-1; j>0; --j){int fs = temp[j], sc = temp[j-1];cost += g[fs][sc];hap += w[sc];}int avg = hap / (temp.size() - 1);//求平均值没有算起点;if(cost < MINC) {MINC = cost;MAXH = hap;MAXA = avg;path = temp;}else if(cost == MINC && hap > MAXH){MAXH = hap;MAXA = avg;path = temp;}else if(cost == MINC && hap == MAXH && avg > MAXA){MAXA = avg;path = temp;}temp.pop_back();return;}temp.push_back(v);for(auto ele : pre[v])dfs(ele);temp.pop_back(); //回溯
}void Print()
{cout << num << ' ' << MINC << ' ' << MAXH << ' ' << MAXA << endl;for(int i=path.size()-1; i>=0; --i){if(i != path.size()-1)  cout << "->";cout << idx_city[path[i]];}
}int main()
{Read();Dijkstra(st);dfs(ed);Print();return 0;
}

http://www.lryc.cn/news/92258.html

相关文章:

  • 浅谈HttpURLConnection所有方法详解
  • 前端快速创建web3应用模版分享
  • 越权漏洞讲解
  • 短视频矩阵源码系统打包.源码
  • 云南LED、LCD显示屏系统建设,户外、室内广告大屏建设方案
  • Shell脚本:expect脚本免交互
  • 王道考研计算机网络第二章知识点汇总
  • 06.05
  • 【虹科案例】虹科数字化仪在激光雷达大气研究中的应用
  • Java抽象类介绍
  • 适配器模式的运用
  • 2023/6/8总结
  • AIGC大模型之——以文生图介绍
  • kali学习笔记(二)
  • avx指令集判断的坑
  • 求内推,求明主!
  • 第十三章:约束
  • M.2 SSD接口详解
  • 在本地Windows 11 系统的桌面版Docker上搭建PlantUML
  • mysql的sql_mode模式
  • chatgpt赋能python:Python编程必备之OpenCV库下载与安装
  • sparkSQL的使用
  • gitignore的语法
  • 长袋除尘器
  • HTML+CSS实训——Day02——仿一个网易云音乐的登陆界面
  • css蓝桥杯--电影院排座位
  • c++学习——多态
  • Java SPI机制及原理详解
  • 不压缩打包layui
  • 过去、现在及未来