• PTA--1087 All Roads Lead to Rome(最短路计数+输出路径)


    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:

    1. 6 7 HZH
    2. ROM 100
    3. PKN 40
    4. GDN 55
    5. PRS 95
    6. BLN 80
    7. ROM GDN 1
    8. BLN ROM 1
    9. HZH PKN 1
    10. PRS ROM 2
    11. BLN HZH 2
    12. PKN GDN 1
    13. HZH PRS 1

    Sample Output:

    1. 3 3 195 97
    2. HZH->PRS->ROM

    题意:给定n个点,m条边,和起点sx,每个点有一个快乐值,每条边有一个花费值,要求你输出两行,第一行是到终点{最少花费路径条数,最少花费,快乐值,平均快乐值(不算起点)},第二行输出路径。
    优先选择花费少的路径,如果相同优先选择快乐值高的,否则选择平均快乐值高的。

    解析:因为输入是字符串,因此需要map来映射,然后最短路跑一边即可,记录一个路径path,跑最短路时记录下这个点的前置点是谁,最后从终点返回起点,最后反转一下就是路径,对于最短路更新详细可以参考代码。

    测试点2是针对路径条数

    1. #include
    2. using namespace std;
    3. const int N=205;
    4. typedef pair<int,int> PII;
    5. int n,m;
    6. int w[N],cost[N],hap[N],pre[N],point[N],road[N];
    7. //每个点的花费,到该点所需总花费,到该点的快乐值,该点的前置点,到该点经过了几个点,到该点有几条路径选择
    8. string sx;//
    9. mapint> id;//每个地点名对应的数字ID
    10. map<int,string> name;//每个ID对应的地点名
    11. vector v[N];
    12. bool vis[N];//标记该点是否访问
    13. void spfa()
    14. {
    15. queue<int> q;
    16. q.push(id[sx]);
    17. //初始化
    18. memset(cost,0x3f,sizeof cost);
    19. memset(point,0x3f,sizeof point);
    20. cost[id[sx]]=0;
    21. road[id[sx]]=1;
    22. point[id[sx]]=0;
    23. while(q.size())
    24. {
    25. int u=q.front();
    26. q.pop();
    27. vis[u]=false;
    28. for(int i=0;isize();i++)
    29. {
    30. int j=v[u][i].first;//邻点
    31. int k=v[u][i].second;//所需花费
    32. if(cost[j]>cost[u]+k)
    33. {
    34. point[j]=point[u]+1;//经过点数+1
    35. cost[j]=cost[u]+k;
    36. hap[j]=hap[u]+w[j];
    37. road[j]=road[u];//继承路径条数
    38. pre[j]=u;//记录前置点
    39. if(!vis[j])
    40. {
    41. vis[j]=true;
    42. q.push(j);
    43. }
    44. }else if(cost[j]==cost[u]+k)//相同花费
    45. {
    46. road[j]+=road[u];//注意是+road[u]而不是+1,这个是针对测试点2的
    47. if(hap[j]
    48. {
    49. hap[j]=hap[u]+w[j];
    50. point[j]=point[u]+1;
    51. pre[j]=u;
    52. if(!vis[j])
    53. {
    54. vis[j]=true;
    55. q.push(j);
    56. }
    57. }else//相同快乐值
    58. {
    59. if(point[j]>point[u]+1)//优先选择经过点数少的
    60. {
    61. point[j]=point[u]+1;
    62. pre[j]=u;
    63. if(!vis[j])
    64. {
    65. vis[j]=true;
    66. q.push(j);
    67. }
    68. }
    69. }
    70. }
    71. }
    72. }
    73. }
    74. void solve()
    75. {
    76. cin>>n>>m>>sx;
    77. int cnt=1;//记录映射数
    78. id[sx]=1;
    79. name[1]=sx;
    80. for(int i=1;i
    81. {
    82. string k;
    83. cin>>k;
    84. if(!id[k]) id[k]=++cnt,name[cnt]=k;
    85. cin>>w[id[k]];
    86. }
    87. for(int i=1;i<=m;i++)
    88. {
    89. string x,y;
    90. int c;
    91. cin>>x>>y>>c;
    92. int a=id[x],b=id[y];
    93. v[a].push_back({b,c});
    94. v[b].push_back({a,c});
    95. }
    96. spfa();
    97. int y=id["ROM"],pos=y;//终点
    98. vector<int> path;//保存路径方案
    99. while(pos!=id[sx])
    100. {
    101. path.push_back(pos);
    102. pos=pre[pos];
    103. }
    104. path.push_back(id[sx]);//起点
    105. printf("%d %d %d %d\n",road[y],cost[y],hap[y],hap[y]/((int)path.size()-1));
    106. reverse(path.begin(), path.end());//反转
    107. for(int i=0;isize();i++)
    108. {
    109. if(i!=0) printf("->");
    110. cout<
    111. }
    112. printf("\n");
    113. }
    114. int main()
    115. {
    116. int t=1;
    117. //scanf("%d",&t);
    118. while(t--) solve();
    119. return 0;
    120. }
  • 相关阅读:
    臻图信息赋能数字化变革,打造智慧商场新模式
    【C语言】还搞不明白结构体吗?不妨来看看这篇文章,带你初步了解结构体
    单目视频估计人体姿态+形状【注意力机制--连续性+相关性】含6数据集及补充材料
    047:mapboxGL本地上传shp文件,在map上解析显示图形
    2022-08-11 学习日记(31st day)网络通信(网络编程)
    关于useEvent的思考
    【SSO单点登录】ticket+token+redis 实现sso单点登录 && 防重放、防盗用、防篡改
    Nginx代理FastDFS刷新配置之后也不能访问FastDFS的文件
    CentOS下的miniconda3安装
    前端面试 算法与数据结构篇
  • 原文地址:https://blog.csdn.net/qq_63739337/article/details/134093554