There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and return it to any other stations in the city.

The Public Bike Management Center (PBMC) keeps monitoring the real-time capacity of all the stations. A station is said to be in perfect condition if it is exactly half-full. If a station is full or empty, PBMC will collect or send bikes to adjust the condition of that station to perfect. And more, all the stations on the way will be adjusted as well.

When a problem station is reported, PBMC will always choose the shortest path to reach that station. If there are more than one shortest path, the one that requires the least number of bikes sent from PBMC will be chosen.


Figure 1

Figure 1 illustrates an example. The stations are represented by vertices and the roads correspond to the edges. The number on an edge is the time taken to reach one end station from another. The number written inside a vertex S is the current number of bikes stored at S. Given that the maximum capacity of each station is 10. To solve the problem at S3, we have 2 different shortest paths:

1. PBMC -> S1 -> S3. In this case, 4 bikes must be sent from PBMC, because we can collect 1 bike from S1 and then take 5 bikes to S3, so that both stations will be in perfect conditions.

2. PBMC -> S2 -> S3. This path requires the same time as path 1, but only 3 bikes sent from PBMC and hence is the one that will be chosen.

Input Specification:

Each input file contains one test case. For each case, the first line contains 4 numbers: Cmax (<= 100), always an even number, is the maximum capacity of each station; N (<= 500), the total number of stations; Sp, the index of the problem station (the stations are numbered from 1 to N, and PBMC is represented by the vertex 0); and M, the number of roads. The second line contains N non-negative numbers Ci(i=1,...N) where each Ci is the current number of bikes at Si respectively. Then M lines follow, each contains 3 numbers: Si, Sj, and Tij which describe the time Tij taken to move betwen stations Si and Sj. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print your results in one line. First output the number of bikes that PBMC must send. Then after one space, output the path in the format: 0->S1->...->Sp. Finally after another space, output the number of bikes that we must take back to PBMC after the condition of Sp is adjusted to perfect.

Note that if such a path is not unique, output the one that requires minimum number of bikes that we must take back to PBMC. The judge's data guarantee that such a path is unique.

Sample Input:

  1. 10 3 3 5
  2. 6 7 0
  3. 0 1 1
  4. 0 2 1
  5. 0 3 3
  6. 1 3 1
  7. 2 3 1

Sample Output:

  1. 3 0->2->3 0
    题目大意是找一条最短路径,(走每条路径,都要把节点调整为最佳状态,带走或增加一些车子)有多条最短路径时,输出从管理中心最少带出的车子。带出的车子相同时,输出带回最少的车子。
    dijkstra求最短路径有一些拓展问题:如何求出有多少条最短路径;如何输出路径;包含一些附加信息是我的处理
    有多少条最短路径和输出最短路径可以归为一种解法。我们定义vector<int>q[n],q[i].size()表示最短路径的个数。通过dfs可以输出最短路径。
    对于这个问题,我们还有限制条件。所以对所有的最短路径做一遍dfs,保存答案即可。
    下面是代码:
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<vector>
  5. using namespace std;
  6. int e[][];
  7. int dis[];
  8. int c[];
  9. bool vis[];
  10. const int inf=0x3f3f3f3f;
  11. int cmax,n,s,m;
  12. vector<int>q[];
  13. vector<int>path,temppath;
  14. int mintake,minback;
  15. void dijkstra(int d)
  16. {
  17. memset(vis,,sizeof(vis));
  18. dis[]=;
  19. for(int i=;i<=n;i++)
  20. dis[i]=e[][i];
  21. while()
  22. {
  23. int maxx=inf,tmp=-;
  24. for(int i=;i<=n;i++)
  25. {
  26. if(vis[i]==&&dis[i]<maxx)
  27. {
  28. maxx=dis[i];
  29. tmp=i;
  30. }
  31. }
  32. vis[tmp]=;
  33. if(tmp==-)
  34. break;
  35. for(int i=;i<=n;i++)
  36. {
  37. if(vis[i]==&&e[tmp][i]!=inf)
  38. {
  39. if(dis[i]>dis[tmp]+e[tmp][i])
  40. {
  41. q[i].clear();
  42. q[i].push_back(tmp);
  43. dis[i]=dis[tmp]+e[tmp][i];
  44. }
  45. else if(dis[i]==dis[tmp]+e[tmp][i])
  46. {
  47. q[i].push_back(tmp);
  48. }
  49. }
  50. }
  51. }
  52. }
  53. void dfs(int v)
  54. {
  55. temppath.push_back(v);
  56. if(v==)
  57. {
  58. int back=,take=;
  59. for(int i=temppath.size()-;i>=;i--)
  60. {
  61. int id=temppath[i];
  62. if(c[id]>)
  63. back+=c[id]; //需要带走的
  64. else
  65. {
  66. if(back>(-c[id]))//如果之前带走的大于当前需要的
  67. {
  68. back+=c[id];
  69. }
  70. else
  71. {
  72. take+=-(back+c[id]);
  73. back=;
  74. }
  75. }
  76. }
  77. if(take<mintake)
  78. {
  79. mintake=take;
  80. minback=back;
  81. path=temppath;
  82. }
  83. else if(take==mintake&&back<minback)
  84. {
  85. mintake=take;
  86. minback=back;
  87. path=temppath;
  88. }
  89. temppath.pop_back();
  90. return;
  91. }
  92. for(int i=;i<q[v].size();i++)
  93. dfs(q[v][i]);
  94. temppath.pop_back();
  95. }
  96. int main()
  97. {
  98. mintake=inf;minback=inf;
  99. scanf("%d%d%d%d",&cmax,&n,&s,&m);
  100. int a,b,cost;
  101. memset(e,inf,sizeof(e));
  102. for(int i=;i<=n;i++)
  103. {
  104. scanf("%d",&c[i]);
  105. c[i]-=cmax/;
  106. }
  107.  
  108. for(int i=;i<=m;i++)
  109. {
  110. scanf("%d%d%d",&a,&b,&cost);
  111. e[a][b]=e[b][a]=cost;
  112. }
  113. dijkstra(s);
  114. dfs(s);
  115. printf("%d 0",mintake);
  116. for(int i=path.size()-;i>=;i--)
  117. cout<<"->"<<path[i];
  118. cout<<" "<<minback<<endl;
  119. }

类似的问题还有PAT1003,也是多条最短路径的限制问题。

PAT1018 (dijkstra+dfs)的更多相关文章

  1. PAT-1018 Public Bike Management(dijkstra + dfs)

    1018. Public Bike Management There is a public bike service in Hangzhou City which provides great co ...

  2. 【bzoj4016】[FJOI2014]最短路径树问题 堆优化Dijkstra+DFS树+树的点分治

    题目描述 给一个包含n个点,m条边的无向连通图.从顶点1出发,往其余所有点分别走一次并返回. 往某一个点走时,选择总长度最短的路径走.若有多条长度最短的路径,则选择经过的顶点序列字典序最小的那条路径( ...

  3. PAT-1030 Travel Plan (30 分) 最短路最小边权 堆优化dijkstra+DFS

    PAT 1030 最短路最小边权 堆优化dijkstra+DFS 1030 Travel Plan (30 分) A traveler's map gives the distances betwee ...

  4. 天梯赛练习 L3-011 直捣黄龙 (30分) dijkstra + dfs

    题目分析: 本题我有两种思路,一种是只依靠dijkstra算法,在dijkstra部分直接判断所有的情况,以局部最优解得到全局最优解,另一种是dijkstra + dfs,先计算出最短距离以及每个点的 ...

  5. 1030 Travel Plan Dijkstra+dfs

    和1018思路如出一辙,先求最短路径,再dfs遍历 #include <iostream> #include <cstdio> #include <vector> ...

  6. patest_1003_Emergency (25)_(dijkstra+dfs)

    1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...

  7. 1018 Public Bike Management (30分) PAT甲级真题 dijkstra + dfs

    前言: 本题是我在浏览了柳神的代码后,记下的一次半转载式笔记,不经感叹柳神的强大orz,这里给出柳神的题解地址:https://blog.csdn.net/liuchuo/article/detail ...

  8. PAT 1111 Online Map[Dijkstra][dfs]

    1111 Online Map(30 分) Input our current position and a destination, an online map can recommend seve ...

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

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

随机推荐

  1. java接口的学习笔记

    1接口 接口是一个特殊的类,在JAVA中,接口是由抽象方法和全局敞亮组成. 在JAVA忠使用INTERFACE定义接口. public class InterfaceDemo { public sta ...

  2. hadoop的client搭建-即集群外主机访问hadoop

    1增加主机映射(与namenode的映射一样): 增加最后一行  [root@localhost ~]# su - root1 [root@localhost ~]# vi /etc/hosts 12 ...

  3. 排查bug的步骤

    原创文章,欢迎阅读,禁止转载. bug预防C/C++代码发布前的检查:检查有没有低级错误,可用cppcheck (bug预防是指在写程序的时候,bug没出现,积极的进行预防,减少.包括良好的编码风格. ...

  4. centos 主机名突然变成bogon的解决方法

    主机名突然变成bogon,访问网络可能会出现问题(也可能没问题,我的就没问题),可能用到主机名的服务(比如说:mysql)可能也会出现访问不了.   所以我们需要解决以下问题,本人的解决方法: 主机名 ...

  5. screen 命令安装使用

    初次接触Linux的朋友总会有个感觉:Windows平台想同时运行多个操作,执行多个程序或命令只需要打开程序即可:但在Linux中,命令行就一个,要想同时执行多个命令如何操作? 其实,只需要一个简简单 ...

  6. day06-三元表达式

    python中没有其他语言中的三元表达式,不过有类似的实现方法 其他语言中,例如java的三元表达式是这样int a = 1;String b = "";b = a > 1? ...

  7. mobile-net v2 学习记录。我是菜鸡!

    声明:只是自己写博客总结下,不保证正确性,我的理解很可能是错的.. 首先,mobile net V1的主要特点是: 1.深度可分离卷积.用depth-wise convolution来分层过滤特征,再 ...

  8. CSS VISUAL RULES

    CSS VISUAL RULES Review Visual Rules Incredible work! You used CSS to alter text and images througho ...

  9. img标签插入图片返回403,浏览器可以直接打开

    参考:https://segmentfault.com/q/1010000011752614/a-1020000011764026 博客园引入外部图片出现,出现403问题,应该是加了防盗链,会检测访问 ...

  10. Jetty-attack-test

    import httplib, urllib, ssl, string, sys, getopt from urlparse import urlparse ''' Author: Gotham Di ...