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. 阿里云云盾抗下全球最大DDoS攻击(5亿次请求,95万QPS HTTPS CC攻击) ,阿里百万级QPS资源调度系统,一般的服务器qps多少? QPS/TPS/并发量/系统吞吐量

    阿里云云盾抗下全球最大DDoS攻击(5亿次请求,95万QPS HTTPS CC攻击) 作者:用户 来源:互联网 时间:2016-03-30 13:32:40 安全流量事件https互联网资源 摘要:  ...

  2. 【ContestHunter】【弱省胡策】【Round5】

    反演+FFT+构造+DP 写了这么多tag,其实我一个也不会 A 第一题是反演……数据范围10W,看着就有种要用FFT等神奇算法的感觉……然而蒟蒻并不会推公式,只好写了20+10分的暴力,然而特判30 ...

  3. @JVM中的几种垃圾收集算法

    标记-清除(Mark-Sweep) 算法分为"标记"和"清除"两个阶段:首先标记出所有需要回收的对象,在标记完成后统一回收掉所有被标记的对象(没有与GC Roo ...

  4. Android之ViewPager循环Demo

    ViewPager是谷歌官方提供的兼容低版本安卓设备的软件包,里面包含了只有在安卓3.0以上可以使用的api.Viewpager现在也算是标配了,如果一个App没有用到ViewPager感觉还是比较罕 ...

  5. Builder 建造者模式 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  6. Spring Security OAuth2 Demo

    Spring Security OAuth2 Demo 项目使用的是MySql存储, 需要先创建以下表结构: CREATE SCHEMA IF NOT EXISTS `alan-oauth` DEFA ...

  7. 一次真实的蓝屏分析 ntkrnlmp.exe

    故事背景: 话说我一直都是远程公司的电脑,在我晚上11点敲代码敲得正爽的时候,被远程的主机挂掉了,毫无征兆的挂掉了,我特么还好有闲着没事就ctrl + s保存代码的习惯,要不然白敲了那么久,我以为是公 ...

  8. UVA 10090 Marbles(扩展欧几里得)

    Marbles Input: standard input Output: standard output I have some (say, n) marbles (small glass ball ...

  9. 大型开放式网络课程MOOC的一点体会

            2012年,美国的顶尖大学陆续设立网络学习平台,在网上提供免费课程,Coursera.Udacity.edX三大课程提供商的兴起.给很多其它学生提供了系统学习的可能.这就是大型开放式网 ...

  10. 高德地图引入库错误std::string::find_first_of(char const*, unsigned long, unsigned long) const"

    一:std:编译器错误解决 二:错误提示 "std::string::find_first_of(char const*, unsigned long, unsigned long) con ...