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 recommended. 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 recommended 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
dijkstra算法,需要记录路径,到某点最小cost,最大happiness,经过最小地点数。
代码:
#include <stdio.h> #include <string.h> #define inf 0x3f3f3f3f ///终点是ROM 城市名是三个大写字母 int n,k,d,num,destination; int pos[20000],happiness[205],sumhappiness[205],dis[205],mp[205][205],visited[205],placenum[205],path[205],pathnum[205]; char loc1[4],loc2[4],s[205][4]; int change(const char *t) { return (t[2] - 'A') + (t[1] - 'A') * 26 + (t[0] - 'A') * 26 * 26; } void getpath(int t) { if(t)getpath(path[t]); if(t)printf("->"); printf("%s",s[t]); } int main() { scanf("%d%d %s",&n,&k,loc1); int temp = change(loc1);///地点名对应int值 pos[temp] = num ++;///int值对应位置 每安排一个位置,num就+1 strcpy(s[num - 1],loc1);///同时记录对应位置 的地点名 for(int i = 1;i < n;i ++) { scanf("%s %d",loc1,&d); temp = change(loc1); pos[temp] = num ++; happiness[num - 1] = d; strcpy(s[num - 1],loc1); } destination = pos[change("ROM")]; for(int i = 0;i < num;i ++) {///初始化 for(int j = 0;j < num;j ++) { mp[i][j] = inf; } dis[i] = inf; path[i] = -1; } dis[0] = 0; pathnum[0] = 1;///路径数初始为1 for(int i = 0;i < k;i ++) { scanf("%s %s %d",loc1,loc2,&d); int a = pos[change(loc1)],b = pos[change(loc2)]; mp[a][b] = mp[b][a] = d; } while(1) {///dijkstra int t = -1,mi = inf; for(int i = 0;i < num;i ++) { if(visited[i])continue; if(dis[i] < mi)mi = dis[i],t = i; } if(t == -1)break; visited[t] = 1; for(int i = 0;i < num;i ++) { if(visited[i] || mp[t][i] == inf)continue; if(dis[i] > dis[t] + mp[t][i]) { dis[i] = dis[t] + mp[t][i]; sumhappiness[i] = sumhappiness[t] + happiness[i]; placenum[i] = placenum[t] + 1; path[i] = t; pathnum[i] = pathnum[t]; } else if(dis[i] == dis[t] + mp[t][i]) { pathnum[i] += pathnum[t]; if(sumhappiness[i] < sumhappiness[t] + happiness[i]) { sumhappiness[i] = sumhappiness[t] + happiness[i]; path[i] = t; placenum[i] = placenum[t] + 1; } else if(sumhappiness[i] == sumhappiness[t] + happiness[i] && placenum[i] > placenum[t] + 1) { path[i] = t; placenum[i] = placenum[t] + 1; } } } } printf("%d %d %d %d\n",pathnum[destination],dis[destination],sumhappiness[destination],sumhappiness[destination] / placenum[destination]); getpath(destination); }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 趁着过年的时候手搓了一个低代码框架
· 推荐一个DeepSeek 大模型的免费 API 项目!兼容OpenAI接口!