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. Android之使用XMLPull解析xml(二)

    转自:http://www.blogjava.net/sxyx2008/archive/2010/08/04/327885.html 介绍下在Android中极力推荐的xmlpull方式解析xml.x ...

  2. opencv cuda TK1 TX1 兼容设置

    cmake设置 CUDA_ARCH_BIN 3.2 5.2 CUDA_ARCH_PTX 3.2 5.2 否则报一下错误: OpenCV Error: Gpu API call (NCV Asserti ...

  3. Matlab注释多行和取消多行注释的快捷键

    matlab里注释符号是%,只是单行注释,可是没有多行注释符号,就像C/C++/Java中都有多行注释符号/*  */. 如果利用单行注释的方式手工注释一段程序会很麻烦,matlab软件自带快捷键支持 ...

  4. go语言之进阶篇非结构体匿名字段

    1.非结构体匿名字段 示例 : package main import "fmt" type mystr string //自定义类型,给一个类型改名 type Person st ...

  5. 比较全的OA系统功能模块列表

    如何判断一款协同OA软件,是否智能,是否注重细节,是否足够成熟呢?产品的设计优势.功能特性,需要我们总结,也需要让更多的用户了解.功能到底强在哪里?下文中将给出一个详尽的答案. 软件安装 傻瓜化向导式 ...

  6. Bootstrap全局CSS样式之表格

    .table--基础表格样式. .table-striped--给<tbody>之内的每一行添加斑马条纹样式: .table-bordered--为表格添加边框: .table-hover ...

  7. jquery解析XML及获取XML节点名称

    ).tagName $().tagName [].tagName[] $(].tagName context.nodeName $(this).context.nodeName function ge ...

  8. fcntl的区域锁定

    文件中的某个部分被锁定了,但其他的程序可以访问这个文件的其他部分,称为文件段锁定或文件区域锁定.经常使用文件区域锁定是fcntl函数. #include <sys/types.h> #in ...

  9. InitialContext和lookup(转)

    原文地址:http://wxg6203.iteye.com/blog/680830 最近因为工作需要开始学习Ejb3,遇到了一个让我很郁闷的事情,做一下小小的总结——小心new InitialCont ...

  10. The jQuery HTML5 Audio / Video Library (jQuery jPlayer插件给你的站点增加视频和音频功能)

    http://jplayer.org/ The jQuery HTML5 Audio / Video Library jPlayer is the completely free and open s ...