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. [Git] git merge之squash

    reference : https://www.cnblogs.com/ungshow/p/3515161.html 看CM源码时,发现历史记录里有很多squash,于是google了解了一下. Gi ...

  2. 文件权限控制篇access alphasort chdir chmod chown chroot closedir fchdir fchmod fchown fstat ftruncate getcwd

    access(判断是否具有存取文件的权限) 相关函数 stat,open,chmod,chown,setuid,setgid 表头文件 #include<unistd.h> 定义函数 in ...

  3. 【BZOJ】【2200】【USACO 2011 Jan】道路和航线

    做了一天…… TLE:数组开小了-_-#道路是有50000的,双向要乘二.(我特么怎么想的就以为是树了……) WA:一些大点都WA了,小点都过了.好纠结…… AC了QAQ,不知道为什么,在并查集合并的 ...

  4. 数据库实例: STOREBOOK > 表空间 > 编辑 表空间: USERS

    ylbtech-Oracle:数据库实例: STOREBOOK  >  表空间  >  编辑 表空间: USERS 表空间  >  编辑 表空间: USERS 1. 一般信息返回顶部 ...

  5. OpenCV 脸部跟踪(2)

          前面一篇文章中提到,我们在一副脸部图像上选取76个特征点,以及这些特征点的连通性信息来描述脸部形状特征,本文中我们会把这些特征点映射到一个标准形状模型.       通常,脸部形状特征点能 ...

  6. design-twitter

    https://leetcode.com/problems/design-twitter/ class Twitter { unordered_map<int, set<int> & ...

  7. 第二章 企业项目开发--maven父子模块

    2.1.maven父子模块 在实际开发中,我们基本都会用maven父子分模块的方式进行项目的开发. 2.2.实际操作 2.2.1.手工建立一个ssmm0的文件夹,并在该文件夹中加入一个pom.xml文 ...

  8. JAVASCRIPT校验大全[转]

    var IsFireFox = document.getElementById &&! document.all;//判断是否为FireFox //页面里回车到下一控件的焦点 func ...

  9. 巧妙使用div+css模拟表格对角线

    首先声明: 这只是探讨一种CSS模拟表格对角线的用法,实际在工作中可能觉得这样做有点小题大作,这不是本主题讨论的重点.如果对此深以为然的朋友,请一笑过之... 有时在插入文档时,要用到表格对角线,常见 ...

  10. Inner Classes with TypeScript

    原文:https://blog.oio.de/2014/03/21/inner-classes-typescript/ b.ts class Foo { sex:string; say(){ new ...