暴力DFS。

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<cmath>
  4. #include<vector>
  5. #include<map>
  6. #include<stack>
  7. #include<queue>
  8. #include<string>
  9. #include<iostream>
  10. #include<algorithm>
  11. using namespace std;
  12.  
  13. const int maxn=+;
  14. int n,m;
  15. string st;
  16. map<string ,int>m1;
  17. map<int ,string>m2;
  18. int sz;
  19. int h[maxn];
  20. struct Edge
  21. {
  22. int u,v,c;
  23. }e[maxn*maxn];
  24. int tot;
  25. vector<int>g[maxn];
  26.  
  27. int des;
  28. int ans_cost=0x7FFFFFFF;
  29. int ans_count=;
  30. int ans_happy=;
  31. int ans_point=;
  32. int path[maxn],ans_path[maxn];
  33. bool flag[maxn];
  34.  
  35. void dfs(int x,int cost,int happy,int point)
  36. {
  37. if(cost>ans_cost) return;
  38. if(x==des)
  39. {
  40. if(cost<ans_cost)
  41. {
  42. ans_cost=cost;
  43. ans_count=;
  44. ans_happy=happy;
  45. ans_point=point;
  46. for(int i=;i<point;i++)
  47. ans_path[i]=path[i];
  48. }
  49.  
  50. else if(cost==ans_cost)
  51. {
  52. ans_count++;
  53. if(happy>ans_happy)
  54. {
  55. ans_happy=happy;
  56. ans_point=point;
  57. for(int i=;i<point;i++)
  58. ans_path[i]=path[i];
  59. }
  60.  
  61. else if(happy==ans_happy)
  62. {
  63. if(point<ans_point)
  64. {
  65. ans_point=point;
  66. for(int i=;i<point;i++)
  67. ans_path[i]=path[i];
  68. }
  69. }
  70. }
  71. return;
  72. }
  73.  
  74. for(int i=;i<g[x].size();i++)
  75. {
  76. int id=g[x][i];
  77. path[point]=e[id].v;
  78. if(flag[e[id].v]==) continue;
  79. flag[e[id].v]=;
  80. dfs(e[id].v,cost+e[id].c,happy+h[e[id].v],point+);
  81. flag[e[id].v]=;
  82. }
  83. }
  84.  
  85. int main()
  86. {
  87. scanf("%d%d",&n,&m); cin>>st;
  88. m1[st]=++sz; m2[sz]=st;
  89.  
  90. for(int i=;i<=n-;i++)
  91. {
  92. string name; cin>>name;
  93. m1[name]=++sz; m2[sz]=name;
  94. int val; scanf("%d",&val);
  95. h[sz]=val;
  96. }
  97.  
  98. des=m1["ROM"];
  99.  
  100. tot=;
  101. for(int i=;i<=m;i++)
  102. {
  103. string U,V; int c; cin>>U>>V>>c;
  104. e[tot++].u=m1[U]; e[tot].v=m1[V]; e[tot].c=c;
  105. g[m1[U]].push_back(tot);
  106.  
  107. e[tot++].u=m1[V]; e[tot].v=m1[U]; e[tot].c=c;
  108. g[m1[V]].push_back(tot);
  109. }
  110.  
  111. memset(flag,,sizeof flag);
  112. flag[]=;
  113. dfs(,,,);
  114.  
  115. printf("%d %d %d %d\n",ans_count,ans_cost,ans_happy,ans_happy/ans_point);
  116.  
  117. cout<<st;
  118. for(int i=;i<ans_point;i++)
  119. cout<<"->"<<m2[ans_path[i]];
  120. printf("\n");
  121.  
  122. return ;
  123. }

PAT (Advanced Level) 1087. All Roads Lead to Rome (30)的更多相关文章

  1. 【PAT甲级】1087 All Roads Lead to Rome (30 分)(dijkstra+dfs或dijkstra+记录路径)

    题意: 输入两个正整数N和K(2<=N<=200),代表城市的数量和道路的数量.接着输入起点城市的名称(所有城市的名字均用三个大写字母表示),接着输入N-1行每行包括一个城市的名字和到达该 ...

  2. PAT甲级练习 1087 All Roads Lead to Rome (30分) 字符串hash + dijkstra

    题目分析: 这题我在写的时候在PTA提交能过但是在牛客网就WA了一个点,先写一下思路留个坑 这题的简单来说就是需要找一条最短路->最开心->点最少(平均幸福指数自然就高了),由于本题给出的 ...

  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. 1087. All Roads Lead to Rome (30)

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

  5. 1087 All Roads Lead to Rome (30)(30 分)

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

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

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

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

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

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

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

随机推荐

  1. Difference between x:Reference and x:Name

    {x:Reference ...} -> returns just a reference of an object it doesn't create that "bridge&qu ...

  2. python之道07

    2.用户输入一个数字,判断一个数是否是水仙花数. 水仙花数是一个三位数, 三位数的每一位的三次方的和还等于这个数. 那这个数就是一个水仙花数, 例如: 153 = 1******3 + 5****** ...

  3. HashMap允许将null用作键 也允许将null作为值

    HashMap不能保证元素的顺序,HashMap能够将键设为null,也可以将值设为null. 与之对应的是Hashtable,(注意大小写:不是HashTable),Hashtable不能将键和值设 ...

  4. 【dp】bzoj1613: [Usaco2008 Jan]Running贝茜的晨练计划

    还记得这是以前看上去的不可做题…… Description 奶牛们打算通过锻炼来培养自己的运动细胞,作为其中的一员,贝茜选择的运动方式是每天进行N(1<=N<=10,000)分钟的晨跑.在 ...

  5. FTP实验报告

    FTP实验报告 制作人:全心全意 准备工作: linux1:192.168.100.4 关闭防火墙.selinux机制 配置yum源 匿名访问 1.安装vsftpd服务和客户端 [root@local ...

  6. verilog behaviral modeling -- procedural timing contronls

    1.delay control : an expression specifies the time duration between initially encountering the state ...

  7. lombok安装、配置、使用

    eclipse安装Lombok 运行安装: java -jar ${path}\lombok.jar 选择IDE所在路径点击Install/Update即可使用. 手动安装1. 将lombok.jar ...

  8. ajax动态刷新下拉框

    动态post,避免直接给页面传输大量数据 /** * ajax通过商品刷新供应商 * by_kangyx * @throws IOException */ @RequestMapping(params ...

  9. Objective-c 实例变量的访问级别

    在C#和JAVA中无论是method还是variable都有严格的访问级别控制,那么在object-c中对访问级别的使用非常稀少,原因可能是因为在method上没有访问级别的语法,单单控制变量没有什么 ...

  10. IOS 自动布局-UIStackPanel和UIGridPanel(一)

    我以前是做windows phone开发的,后来转做IOS的开发,因此很多windows phone上面的开发经验也被我带到了IOS中.其实有些经验本身跟平台无关,跟平台有关的无非就是实现方法而已.好 ...