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.
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.
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.
- 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
- 3 3 195 97
- HZH->PRS->ROM
题意:给定n个点,m条边,和起点sx,每个点有一个快乐值,每条边有一个花费值,要求你输出两行,第一行是到终点{最少花费路径条数,最少花费,快乐值,平均快乐值(不算起点)},第二行输出路径。
优先选择花费少的路径,如果相同优先选择快乐值高的,否则选择平均快乐值高的。
解析:因为输入是字符串,因此需要map来映射,然后最短路跑一边即可,记录一个路径path,跑最短路时记录下这个点的前置点是谁,最后从终点返回起点,最后反转一下就是路径,对于最短路更新详细可以参考代码。
测试点2是针对路径条数
- #include
- using namespace std;
- const int N=205;
- typedef pair<int,int> PII;
- int n,m;
- int w[N],cost[N],hap[N],pre[N],point[N],road[N];
- //每个点的花费,到该点所需总花费,到该点的快乐值,该点的前置点,到该点经过了几个点,到该点有几条路径选择
- string sx;//
- map
int> id;//每个地点名对应的数字ID - map<int,string> name;//每个ID对应的地点名
- vector
v[N]; - bool vis[N];//标记该点是否访问
- void spfa()
- {
- queue<int> q;
- q.push(id[sx]);
- //初始化
- memset(cost,0x3f,sizeof cost);
- memset(point,0x3f,sizeof point);
- cost[id[sx]]=0;
- road[id[sx]]=1;
- point[id[sx]]=0;
- while(q.size())
- {
- int u=q.front();
- q.pop();
- vis[u]=false;
- for(int i=0;i
size();i++) - {
- int j=v[u][i].first;//邻点
- int k=v[u][i].second;//所需花费
- if(cost[j]>cost[u]+k)
- {
- point[j]=point[u]+1;//经过点数+1
- cost[j]=cost[u]+k;
- hap[j]=hap[u]+w[j];
- road[j]=road[u];//继承路径条数
- pre[j]=u;//记录前置点
- if(!vis[j])
- {
- vis[j]=true;
- q.push(j);
- }
- }else if(cost[j]==cost[u]+k)//相同花费
- {
- road[j]+=road[u];//注意是+road[u]而不是+1,这个是针对测试点2的
- if(hap[j]
- {
- hap[j]=hap[u]+w[j];
- point[j]=point[u]+1;
- pre[j]=u;
- if(!vis[j])
- {
- vis[j]=true;
- q.push(j);
- }
- }else//相同快乐值
- {
- if(point[j]>point[u]+1)//优先选择经过点数少的
- {
- point[j]=point[u]+1;
- pre[j]=u;
- if(!vis[j])
- {
- vis[j]=true;
- q.push(j);
- }
- }
- }
- }
- }
- }
- }
- void solve()
- {
- cin>>n>>m>>sx;
- int cnt=1;//记录映射数
- id[sx]=1;
- name[1]=sx;
- for(int i=1;i
- {
- string k;
- cin>>k;
- if(!id[k]) id[k]=++cnt,name[cnt]=k;
- cin>>w[id[k]];
- }
- for(int i=1;i<=m;i++)
- {
- string x,y;
- int c;
- cin>>x>>y>>c;
- int a=id[x],b=id[y];
- v[a].push_back({b,c});
- v[b].push_back({a,c});
- }
- spfa();
- int y=id["ROM"],pos=y;//终点
- vector<int> path;//保存路径方案
- while(pos!=id[sx])
- {
- path.push_back(pos);
- pos=pre[pos];
- }
- path.push_back(id[sx]);//起点
- printf("%d %d %d %d\n",road[y],cost[y],hap[y],hap[y]/((int)path.size()-1));
- reverse(path.begin(), path.end());//反转
- for(int i=0;i
size();i++) - {
- if(i!=0) printf("->");
- cout<
- }
- printf("\n");
- }
- int main()
- {
- int t=1;
- //scanf("%d",&t);
- while(t--) solve();
- return 0;
- }
-
相关阅读:
# Sql语句过长报错、查询慢优化方案探索
TOWE时序上电智能PDU产品在IDC数据中心机房的应用
利用Seagate service获得system shell
GLM-4本地部署的实战教程
手工测试如何转向自动化测试?字节5年自动化经验浅谈一下...
Java | Leetcode Java题解之第48题旋转图像
PHP视频网站用wamp、phpstudy运行定制开发mysql数据库BS模式
qt工程文件中根据编译环境进行不同操作
无TMP走Dev通道升级win11后续更新方法
eclipse配置tomcat详解(图文版)
-
原文地址:https://blog.csdn.net/qq_63739337/article/details/134093554
-
最新文章
-
沪漂五周年了:我越来越迷茫了
Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
MySQL-Seconds_behind_master的精度误差
[MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
Agent OS :五种驯服不确定性的范式
PortSwigger SQL注入LAB11
数据库即时编译JIT
[Begin]AI Learn Data Day 0
深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU