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

All Roads Lead to Rome (30)

1、题目:

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

2、思路

1、哈希映射,需要在加边的时候把每个英文节点映射成为整数:unordered_map可以做到,输出的时候要把整数映射回去

2、统计最短路的数量。这里是我做的时候遇到的最坑的,我一直以为只有到达了终点才需要判断最短路的条数,其实如果到达终点的某些中间节点也有很多条的话,那就会出错。对于最短路数量,我们可以开一个统计数组Cntdist[N],记录每个节点的最短路的数量。对于一个点,要么继承前一个点的最短路数量(第一次统计最短路),要么加上前一个点的数量(从另一个地方来的,也是最短路)

3、PAT最爱考输出细节,不能出错。

3、代码:

#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<unordered_map>
using namespace std;
typedef pair<int,int> PII;
const int N = 300,M = N*N;
unordered_map<string,int> S1;
unordered_map<int,string> S2;
int h[N],e[M],w[M],ne[M],idx;
int dist[N],pre[N],s[N],Cntdist[N];
bool st[N];
int a[N];
int z;
int n,m;
string start;
int get_s(string s)
{if(S1.count(s)==0) S1[s] = z++;return S1[s]; 
}
void add(int a,int b,int c)
{e[idx] = b,ne[idx] = h[a],w[idx] = c,h[a] = idx ++;
}
void Dijkstra(int x)
{int ed = S1["ROM"];memset(dist,0x3f,sizeof dist);memset(pre,-1,sizeof pre);Cntdist[x] = 1;priority_queue<PII,vector<PII>,greater<PII>> heap;dist[x] = 0;heap.push({0,x});while(heap.size()){auto t = heap.top();heap.pop();int dis = t.first,ver = t.second;if(st[ver]) continue;st[ver] = true;for(int i = h[ver];~i;i= ne[i]){int j = e[i];if(dist[j] > dist[ver] + w[i]){dist[j] = dist[ver] + w[i];pre[j] = ver;heap.push({dist[j],j});Cntdist[j] = Cntdist[ver];}else if(dist[j] == dist[ver] + w[i]){Cntdist[j] += Cntdist[ver];int res1 = 1,res2 = 1,ans1 = a[j],ans2 = a[ver];for(int k = pre[j];k!=x;k = pre[k]){res1 ++;ans1 += a[k];}for(int k = pre[ver];k!=x;k = pre[k]){res2 ++;ans2 += a[k];}// 如果加上最后一个节点,那么最后的值会是多少res2++,ans2 += a[j];if(ans2 > ans1) pre[j] = ver;else if(ans2==ans1&&ans2/res2 > ans1/res1) pre[j] = ver; heap.push({dist[j],j});}}}
}
int main()
{std::ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);memset(h,-1,sizeof h);cin >> n >> m >> start;int sta = get_s(start);S2[sta] = start;for(int i = 1;i<=n-1;i++){string name;int cos;cin>>name>>cos;int t1 = get_s(name);a[t1] = cos;}for(int i = 1;i<=m;i++){int cost;string name1,name2;cin>> name1 >> name2 >> cost;int na1 = get_s(name1),na2 = get_s(name2);S2[na1] = name1,S2[na2] = name2;add(na1,na2,cost),add(na2,na1,cost);}Dijkstra(sta);int ed = S1["ROM"];int cost = 0;int cnt = 1;s[0] = ed;for(int i = pre[ed];i!=sta;i = pre[i])s[cnt++] = i;s[cnt++] = sta;reverse(s,s+cnt);cout<< Cntdist[ed]  << " "<<dist[ed]<<" ";for(int i = 1;i<cnt;i++)cost += a[s[i]];cout<< cost<<" "<< cost/(cnt-1)<<endl;cout<<S2[s[0]];for(int i = 1;i<cnt;i++)cout<<"->"<<S2[s[i]];return 0;}

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

相关文章:

  • GO语言学习笔记(与Java的比较学习)(四)
  • 在实训云平台上配置云主机
  • 什么是隔离式栅极驱动器?
  • 蓝桥杯算法赛 第 6 场 小白入门赛 解题报告 | 珂学家 | 简单场 + 元宵节日快乐
  • 附加Numpy数组
  • 收银系统源码-智慧新零售,ERP进销存功能详解
  • STM32使用PB3, PB4引脚的注意事项
  • OSCP靶场--DVR4
  • 【嵌入式——QT】日期与定时器
  • 如何决定使用HashMap还是TreeMap?
  • 平台工程与安全
  • 智能咖啡厅助手:人形机器人 +融合大模型,行为驱动的智能咖啡厅机器人(机器人大模型与具身智能挑战赛)
  • js处理IOS虚拟键盘弹出后输入框被遮住
  • 脚手架工程使用ElementUI
  • 163邮箱SMTP端口号及服务器地址详细设置?
  • 【STM32】STM32学习笔记-独立看门狗和窗口看门狗(47)
  • 计算机网络——IPV4数字报
  • java抽象方法和抽象类
  • echarts鼠标向右/向左绘制实现放大/还原
  • Go编译DLL与SO
  • css浮动
  • 小程序怎么开发?怎么开发自己的小程序
  • Unity(第十八部)物理力学,碰撞,触发、关节和材质
  • 内网搭建mysql8.0并搭建主从复制详细教程!!!
  • MYSQL 解释器小记
  • 具身智能计算系统,机器人时代的 Android | 新程序员
  • win11开启IPV6并手动设置地址
  • WPF中如何设置自定义控件
  • 【Leetcode每日一题】二分查找 - 寻找旋转排序数组中的最小值(难度⭐⭐)(22)
  • QT C++实战:实现用户登录页面及多个界面跳转