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. 通过Web启动本地应用程序

    通过自定义协议在Web中启动本地应用程序 实例是打开本地安装的Word程序   注册自己的协议Windows Registry Editor Version 5.00 [HKEY_CLASSES_RO ...

  2. [PHP] ubuntu16.04下 Phpstorm发布项目到apache

    reference to : http://blog.csdn.net/qq_23937195/article/details/72953308 在网上找的不靠谱,倒腾了大半天的,终于找到正确姿势QA ...

  3. 信号处理篇alarm ferror kill mkfifo pause pclose perror pipe popen sigaction sigaddset sigdelset sigemptyset signal sleep strerror

    alarm(设置信号传送闹钟) 相关函数 signal,sleep 表头文件 #include<unistd.h> 定义函数 unsigned int alarm(unsigned int ...

  4. [转载] java的书

    1. Java 语言基础 谈到Java 语言基础学习的书籍,大家肯定会推荐Bruce Eckel 的<Thinking in Java >.它是一本写的相当深刻的技术书籍,Java 语言基 ...

  5. Servlet监听器统计在线人数

    监听器的作用是监听Web容器的有效事件,它由Servlet容器管理,利用Listener接口监听某个执行程序,并根据该程序的需求做出适应的响应. 例1 应用Servlet监听器统计在线人数. (1)创 ...

  6. Qt Quick 和qml介绍

    很多人不了解Qt Quick和Qml,还有很多人对其存在偏见.这篇文章就是来向这些有困惑的人介绍一下其是什么,有什么特点. 首先,这两个是一个东西吗? 答案:是的.但是,具体来说,Qt Quick是框 ...

  7. (转)Unity3D Android手机开发环境配置,可真机发布调试

    此方法配置好,在可以在unity直接发布到手机上,并可以实时调试. 1.配置eclipse环境:首先在官网下载安装包:http://developer.android.com/sdk/index.ht ...

  8. cocos2d-x -3.81+win7+vs2013开发环境创建新的项目

    cocos2d-x -3.81+win7+vs2013开发环境创建新的项目 1.准备阶段 (1) vs2013下载及安装 (2)cocos2d-x 3.8.1下载及解压 (3)python下载及安装( ...

  9. 【Javascript Demo】根据Email地址跳转到相应的邮箱登录页面

    我的初步想法是通过指定的邮箱地址自动查找到对应的邮箱登录页面,但是用数据库.js什么的都有局限性,因为各种各样的邮箱太多了,不能都包含的到,网上找了半天都没有找到满意的答案,自己又想不出方法,只能暂时 ...

  10. Cass环境下光标无显示

    先安装CAD2004,十字光标正常显示:再安装CASS7.0,光标就不显示了.现在不清楚是CAD的问题,还是CASS的问题,多半是后者.重新配置了CASS环境也不行. 于是,打开CAD选项,显示,窗口 ...