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

地址:http://pat.zju.edu.cn/contests/pat-a-practise/1087

用dijkstra求最短路径,比较麻烦的时在找节点时的判断标准。通常,我写dijkstra的模板是这样的:

  1. 根据已知信息初始化最短cost数组,比如这里的lcost lhapp ahapp等(42行~54行)
  2. 当未遍历到终点时一直进入循环(55行)
  3. 循环体里面先根据最短的cost数组找出当前的最短路径节点(56行~73行)
  4. 遍历该节点(74行)
  5. 通过该节点更新所有于该节点相连的邻接节点(75行~100行)
  6. 返回第二步继续循环

按照题目中判断最短路径的标准是,先比较cost,cost较小为最短路径,如果cost一样比较happiness,happiness越大路径越好,若happiness也一样,则比较平均happiness。另外,题目还要求计算出所有cost一样的路径个数,这样在第5步时,如果找到cost一样的路径时,要把路径个数叠加上去(体现在第86行代码)。代码:

 #include <map>
#include <vector>
#include <stdio.h>
#include <string.h>
#include <string>
using namespace std; const int N = ;
int cost[N][N];
int happ[N];
bool istraved[N];
int lcost[N];
int lhapp[N];
int pathNum[N];
double ahapp[N];
int main()
{
int n, k;
char str_in1[];
char str_in2[];
while(scanf("%d%d%s",&n,&k,str_in1) != EOF){
map<string,int> nodeNum;
vector<vector<int> >path(n+,vector<int>());
memset(cost,,sizeof(cost));
memset(happ,,sizeof(happ));
memset(istraved,,sizeof(istraved));
memset(lcost,,sizeof(lcost));
memset(lhapp,,sizeof(lhapp));
memset(pathNum,,sizeof(pathNum));
memset(ahapp,,sizeof(ahapp));
nodeNum[str_in1] = ;
for(int i = ; i < n+; ++i){
scanf("%s%d",str_in1,&happ[i]);
nodeNum[str_in1] = i;
}
for(int i = ; i < k; ++i){
scanf("%s%s",str_in1,str_in2);
scanf("%d",&cost[nodeNum[str_in1]][nodeNum[str_in2]]);
cost[nodeNum[str_in2]][nodeNum[str_in1]] = cost[nodeNum[str_in1]][nodeNum[str_in2]]; }
int s = ;
int e = nodeNum["ROM"];
istraved[s] = true;
for(int i = ; i <= n; ++i){
lcost[i] = cost[s][i];
if(cost[s][i]){
lhapp[i] = happ[i];
ahapp[i] = happ[i];
pathNum[i] = ;
path[i].push_back(s);
}
}
pathNum[s] = ;
while(!istraved[e]){
int mincost = 0x7fffffff;
int maxhapp = -;
double maxahapp = -;
int pos = ;
for(int i = ; i <= n; ++i){
if(!istraved[i] && lcost[i]){
if(
mincost > lcost[i] ||
(mincost == lcost[i] && maxhapp < lhapp[i]) ||
(mincost == lcost[i] && maxhapp == lhapp[i] && maxahapp < ahapp[i])
){
mincost = lcost[i];
maxhapp = lhapp[i];
maxahapp = ahapp[i];
pos = i;
}
}
}
istraved[pos] = true;
for(int i = ; i <= n; ++i){
if(!istraved[i] && cost[pos][i]){
if(lcost[i] == || lcost[i] > lcost[pos] + cost[pos][i]){
lcost[i] = lcost[pos] + cost[pos][i];
lhapp[i] = lhapp[pos] + happ[i];
pathNum[i] = pathNum[pos];
path[i].clear();
path[i].insert(path[i].end(),path[pos].begin(),path[pos].end());
path[i].push_back(pos);
ahapp[i] = (lhapp[i] * 1.0) / path[i].size();
}else if(lcost[i] == lcost[pos] + cost[pos][i]){
pathNum[i] += pathNum[pos];
if(
lhapp[i] < lhapp[pos] + happ[i] ||
lhapp[i] == lhapp[pos] + happ[i] && ahapp[i] < (lhapp[i]*1.0)/(path[pos].size() + )
){ lhapp[i] = lhapp[pos] + happ[i];
path[i].clear();
path[i].insert(path[i].end(),path[pos].begin(),path[pos].end());
path[i].push_back(pos);
ahapp[i] = (lhapp[i] * 1.0) / path[i].size();
}
}
}
} }
printf("%d %d %d ",pathNum[e],lcost[e],lhapp[e]);
if(path[e].empty())
printf("0\n");
else
printf("%d\n",lhapp[e]/path[e].size());
for(int i = ; i < path[e].size(); ++i){
map<string,int>::iterator it = nodeNum.begin();
for(it; it != nodeNum.end(); ++it){
if(it->second == path[e][i]){
printf("%s->",it->first.c_str());
}
}
}
printf("ROM\n");
}
return ;
}

PAT 1087 All Roads Lead to Rome的更多相关文章

  1. PAT 1087 All Roads Lead to Rome[图论][迪杰斯特拉+dfs]

    1087 All Roads Lead to Rome (30)(30 分) Indeed there are many different tourist routes from our city ...

  2. PAT甲级1087. All Roads Lead to Rome

    PAT甲级1087. All Roads Lead to Rome 题意: 确实有从我们这个城市到罗马的不同的旅游线路.您应该以最低的成本找到您的客户的路线,同时获得最大的幸福. 输入规格: 每个输入 ...

  3. [图的遍历&多标准] 1087. All Roads Lead to Rome (30)

    1087. All Roads Lead to Rome (30) Indeed there are many different tourist routes from our city to Ro ...

  4. PAT 甲级 1087 All Roads Lead to Rome(SPFA+DP)

    题目链接 All Roads Lead to Rome 题目大意:求符合题意(三关键字)的最短路.并且算出路程最短的路径有几条. 思路:求最短路并不难,SPFA即可,关键是求总路程最短的路径条数. 我 ...

  5. PAT 甲级 1087 All Roads Lead to Rome

    https://pintia.cn/problem-sets/994805342720868352/problems/994805379664297984 Indeed there are many ...

  6. PAT (Advanced Level) 1087. All Roads Lead to Rome (30)

    暴力DFS. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...

  7. 【PAT甲级】1087 All Roads Lead to Rome (30 分)(dijkstra+dfs或dijkstra+记录路径)

    题意: 输入两个正整数N和K(2<=N<=200),代表城市的数量和道路的数量.接着输入起点城市的名称(所有城市的名字均用三个大写字母表示),接着输入N-1行每行包括一个城市的名字和到达该 ...

  8. PAT甲级练习 1087 All Roads Lead to Rome (30分) 字符串hash + dijkstra

    题目分析: 这题我在写的时候在PTA提交能过但是在牛客网就WA了一个点,先写一下思路留个坑 这题的简单来说就是需要找一条最短路->最开心->点最少(平均幸福指数自然就高了),由于本题给出的 ...

  9. 1087. All Roads Lead to Rome (30)

    时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Indeed there are many different ...

随机推荐

  1. DAO,Service接口与实现类设计

    DAO接口 为每个DAO声明接口的好处在于 1. 可以在尚未实现具体DAO的时候编写上层代码,如Service里对DAO的调用 2. 可以为DAO进行多实现,例如有JDBCDAO实现,MyBatisD ...

  2. Octave下操作CH341

    #include <octave/oct.h> #include <windows.h> #include <cstdint> #include <fstre ...

  3. 未知高度的图片在div设置垂直居中

    方法一: 该方法是将外部容器的显示模式设置成display:table,img标签外部再嵌套一个span标签,并设置span的显示模式为display:table-cell,这样就可以很方便的使用ve ...

  4. eclipse 使用Maven deploy命令部署构建到Nexus上 【二】

    http://blog.csdn.net/jun55xiu/article/details/43051627

  5. NTP Server

    Network Time Protocol互联网时间协议 NTP is intended to synchronize all participating computers to within a ...

  6. MySQL的IFNULL函数

    MySQL函数里有一个很有用的函数IFNULL,它的形式是IFNULL(fieldA,fieldB),意义是当字段fieldA是NULL时取fieldB,不是NULL时取fieldA的值. 这个函数与 ...

  7. 免费的HTML模板引导 - lonely

    ​在线演示 本地下载 今天和大家分享另一款模板-Lonely.它可以被用在一些个人或者类似简单一些的网站上,动画效果的滚动非常特别!

  8. Android开发之对话框高级应用

    Android开发之对话框高级应用 创建并显示一个对话框非常easy.可是假设想进行一些更高级点的操作,就须要一些技巧了.以下将和大家分享一下对话框使用的一些高级技巧. 1.改变对话框的显示位置: 大 ...

  9. 解决百度编辑器在编辑视频时src丢失的问题

    问题描述:使用的是最新的UEditor 1.4.3.3版本,在上传完视频后,编辑的时候出现视频的src丢失的问题 解决方式:修改ueditor.config.js文件,将 img: ['src', ' ...

  10. activemq无法启动且后台管理界面进不去的解决办法

    从官网下载了一个最新的activemq,目前最新版本是5.14.5 我下载的是windows版本,通过执行%activemq home%/bin/win64/InstallService.bat,可以 ...