PAT1087. All Roads Lead to Rome

题目大意

给定一个图的边权和点权, 求边权最小的路径; 若边权相同, 求点权最大; 若点权相同, 则求平均点权最大.

思路

先通过 Dijkstra 求得最短路径, 需要注意的是: 要保证每次松弛时 u 和 v 不相同, 否则会形成自环, 则从 ROM 开始 BFS 遍历每一条边权相同的路径.

代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <map>
  5. using namespace std;
  6. #define MAXN 300
  7. #define INF 0x7ffffff
  8. int nVertex, nEdge;
  9. map<string, int> s_i;
  10. map<int, string> i_s;
  11. vector<int> prepath[MAXN], temppath, anspath;
  12. int vw[MAXN];
  13. int ew[MAXN][MAXN];
  14. int dis[MAXN];
  15. int isVis[MAXN];
  16. int cntRo = 0;
  17. int ansHapy = 0;
  18. double ansAvg = 0;
  19. void dfs(int loc){
  20. temppath.push_back(loc);
  21. if(loc == 0){
  22. int hapy = 0;
  23. for(int i = 0; i < temppath.size(); i++)
  24. hapy += vw[temppath[i]];
  25. double avgHapy = hapy * 1.0 / (temppath.size() - 1);
  26. if(hapy > ansHapy){
  27. ansHapy = hapy;
  28. ansAvg = avgHapy;
  29. anspath = temppath;
  30. }
  31. else if(hapy == ansHapy && avgHapy > ansAvg){
  32. ansAvg = avgHapy;
  33. anspath = temppath;
  34. }
  35. cntRo++;
  36. temppath.pop_back();
  37. return;
  38. }
  39. for(int i = 0; i < prepath[loc].size(); i++){
  40. dfs(prepath[loc][i]);
  41. }
  42. temppath.pop_back();
  43. }
  44. int main(){
  45. scanf("%d%d", &nVertex, &nEdge);
  46. string tempStr; cin >> tempStr;
  47. s_i[tempStr] = 0; i_s[0] = tempStr;
  48. for(int i = 1; i < nVertex; i++){
  49. cin >> tempStr;
  50. s_i[tempStr] = i; i_s[i] = tempStr;
  51. scanf("%d", &vw[i]);
  52. }
  53. for(int i = 0; i < MAXN; i++){
  54. for(int j = 0; j < MAXN; j++){
  55. ew[i][j] = (i == j ? 0 : INF);
  56. }
  57. }
  58. for(int i = 0; i < nEdge; i++){
  59. string a, b; int c;
  60. cin >> a >> b >> c;
  61. ew[s_i[a]][s_i[b]] = ew[s_i[b]][s_i[a]] = c;
  62. }
  63. for(int i = 0; i < nVertex; i++)
  64. dis[i] = ew[0][i];
  65. dis[0] = 0;
  66. for(int i = 0; i < nVertex; i++){
  67. int u = -1, minn = INF;
  68. for(int j = 0; j < nVertex; j++){
  69. if(!isVis[j] && dis[j] < minn){
  70. minn = dis[j];
  71. u = j;
  72. }
  73. }
  74. isVis[u] = 1;
  75. for(int v = 0; v < nVertex; v++){
  76. if(u != v)
  77. {
  78. if(dis[v] > ew[u][v] + dis[u]){
  79. dis[v] = ew[u][v] + dis[u];
  80. prepath[v].clear();
  81. prepath[v].push_back(u);
  82. }
  83. else if(dis[v] == ew[u][v] + dis[u]){
  84. prepath[v].push_back(u);
  85. }
  86. }
  87. }
  88. }
  89. int rom = s_i["ROM"];
  90. dfs(rom);
  91. printf("%d %d %d %d\n", cntRo, dis[rom], ansHapy, (int)ansAvg);
  92. for(int i = anspath.size() - 1; i != 0; i--){
  93. cout << i_s[anspath[i]] << "->";
  94. }
  95. printf("ROM");
  96. return 0;
  97. }

PAT1087. All Roads Lead to Rome的更多相关文章

  1. pat1087. All Roads Lead to Rome (30)

    1087. All Roads Lead to Rome (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

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

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

  3. 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 ...

  4. [图的遍历&多标准] 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 ...

  5. PAT 1087 All Roads Lead to Rome

    PAT 1087 All Roads Lead to Rome 题目: Indeed there are many different tourist routes from our city to ...

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

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

  7. PAT_A1087#All Roads Lead to Rome

    Source: PAT A1087 All Roads Lead to Rome (30 分) Description: Indeed there are many different tourist ...

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

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

  9. A1087. All Roads Lead to Rome

    Indeed there are many different tourist routes from our city to Rome. You are supposed to find your ...

随机推荐

  1. Java性能调优-jstack-jstat-jmap

    0. 必须在java进程的用户下执行 a). 先排查自己业务代码,再第三方的开源代码 b). 工具类都在jdk/bin目录下, 实现代码在tools.jar中 1. jstack-线程快照-死锁/阻塞 ...

  2. nyoj 211——Cow Contest——————【floyd传递闭包】

    Cow Contest 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 N (1 ≤ N ≤ 100) cows, conveniently numbered 1.. ...

  3. ebiao 报表工具使用入门

    一.ebiao简价 e表是一个功能强大的Web报表工具,可使复杂报表的设计简单化,避免了大量的复杂SQL编写以及编程来准备数据,报表设计的效率大大提高.e表分为e表 for .NET和e表 for J ...

  4. 事件代理总结: 已经有一些使用主流类库的事件代理示例出现了,比如说jQuery、Prototype以及Yahoo! UI。你也可以找到那些不用任何类库的例子,比如说Usable Type blog上的这一个。一旦需要的话,事件代理将是你工具箱里的一件得心应手的工具,而且它很容易实现。

    如果你想给网页添加点JavaScript的交互性,也许你已经听过JavaScript的事件代理(event delegation),并且觉得这是那些发烧友级别的JavaScript程序员才会关心的什么 ...

  5. Android界面编程--使用活动条(ActionBar)--添加Action View

    ActionBar除了显示Action Item 外,还能显示普通的ui组件 2种方式添加Action View 1.指定ActionView的实现类 2.指定ActionView对应的视图资源 实现 ...

  6. 研究SSIS时候的一些常见错误

    1.[OLE DB 目标 [59]] 错误: SSIS 错误代码 DTS_E_OLEDBERROR.出现 OLE DB 错误.错误代码: 0x80004005.已获得 OLE DB 记录.源:“Mic ...

  7. MVC页面简单post提交

    页面代码 <script src="~/Scripts/jquery-1.10.2.js"></script> <script> $(funct ...

  8. linux防火墙与端口设置

    1.编辑iptables文件 # sudo vi /etc/sysconfig/iptables 添加如下一行 -A INPUT -p tcp -m state --state NEW -m tcp ...

  9. python学习(七)--豆瓣爬取电影名,评分以及演员

    import requestsimport re #爬取豆瓣电影排名pageNum = int(input("要查看第几页电影分数:"))#已知豆瓣默认每页展示20条#url= & ...

  10. 使用Spring的AOP实现切面日志

    AOP切面日志的使用方式 @Aspect @Component public class HttpAspect { private static final Logger logger = Logge ...