题目链接:

pid=3631" style="font-size:18px">http://acm.hdu.edu.cn/showproblem.php?pid=3631

Shortest Path

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3962    Accepted Submission(s): 938

Problem Description
When YY was a boy and LMY was a girl, they trained for NOI (National Olympiad in Informatics) in GD team. One day, GD team’s coach, Prof. GUO asked them to solve the following shortest-path problem.

There is a weighted directed multigraph G. And there are following two operations for the weighted directed multigraph:

(1) Mark a vertex in the graph.

(2) Find the shortest-path between two vertices only through marked vertices.

For it was the first time that LMY faced such a problem, she was very nervous. At this moment, YY decided to help LMY to analyze the shortest-path problem. With the help of YY, LMY solved the problem at once, admiring YY very much. Since then, when LMY meets
problems, she always calls YY to analyze the problems for her. Of course, YY is very glad to help LMY. Finally, it is known to us all, YY and LMY become programming lovers.

Could you also solve the shortest-path problem?
 
Input
The input consists of multiple test cases. For each test case, the first line contains three integers N, M and Q, where N is the number of vertices in the given graph, N≤300; M is the number of arcs, M≤100000; and Q is the number
of operations, Q ≤100000. All vertices are number as 0, 1, 2, … , N - 1, respectively. Initially all vertices are unmarked. Each of the next M lines describes an arc by three integers (x, y, c): initial vertex (x), terminal vertex (y), and the weight of the
arc (c). (c > 0) Then each of the next Q lines describes an operation, where operation “0 x” represents that vertex x is marked, and operation “1 x y” finds the length of shortest-path between x and y only through marked vertices. There is a blank line between
two consecutive test cases.

End of input is indicated by a line containing N = M = Q = 0.
 
Output
Start each test case with "Case #:" on a single line, where # is the case number starting from 1.

For operation “0 x”, if vertex x has been marked, output “ERROR! At point x”.

For operation “1 x y”, if vertex x or vertex y isn’t marked, output “ERROR! At path x to y”; if y isn’t reachable from x through marked vertices, output “No such path”; otherwise output the length of the shortest-path. The format is showed as sample output.

There is a blank line between two consecutive test cases.
 
Sample Input
  1. 5 10 10
  2. 1 2 6335
  3. 0 4 5725
  4. 3 3 6963
  5. 4 0 8146
  6. 1 2 9962
  7. 1 0 1943
  8. 2 1 2392
  9. 4 2 154
  10. 2 2 7422
  11. 1 3 9896
  12. 0 1
  13. 0 3
  14. 0 2
  15. 0 4
  16. 0 4
  17. 0 1
  18. 1 3 3
  19. 1 1 1
  20. 0 3
  21. 0 4
  22. 0 0 0
 
Sample Output
  1. Case 1:
  2. ERROR! At point 4
  3. ERROR! At point 1
  4. 0
  5. 0
  6. ERROR! At point 3
  7. ERROR! At point 4
 
Source

代码例如以下:

  1. #include <cstdio>
  2. #include <cstring>
  3. #define INF 0x3fffffff
  4. #define MAXN 317
  5. int dis[MAXN][MAXN];
  6. int mark[MAXN];
  7. int n;
  8. int min(int a, int b)
  9. {
  10. return a < b ? a:b;
  11. }
  12. void init()
  13. {
  14. for(int i = 0; i < n; i++)
  15. {
  16. for(int j = 0; j < n; j++)
  17. {
  18. if(i == j)
  19. dis[i][j] = 0;
  20. else
  21. dis[i][j] = INF;
  22. }
  23. }
  24. }
  25.  
  26. void Floyd(int k)
  27. {
  28. for(int i = 0; i < n; i++)
  29. {
  30. for(int j = 0; j < n; j++)
  31. {
  32. dis[i][j] = min(dis[i][j],dis[i][k]+dis[k][j]);
  33. }
  34. }
  35. }
  36.  
  37. int main()
  38. {
  39. int i, j;
  40. int M, Q;
  41. int a, b, w;
  42. int cas = 0;
  43. while(~scanf("%d%d%d",&n,&M,&Q))
  44. {
  45. if(n == 0 && M == 0 && Q == 0)
  46. break;
  47. if(cas != 0)
  48. printf("\n");
  49. init();
  50. memset(mark,0,sizeof(mark));
  51. for(i = 0; i < M; i++)
  52. {
  53. scanf("%d%d%d",&a,&b,&w);
  54. if(w < dis[a][b])
  55. {
  56. dis[a][b] = w;
  57. }
  58. }
  59. int op, x, y;
  60. printf("Case %d:\n",++cas);
  61. for(i = 0; i < Q; i++)
  62. {
  63. scanf("%d",&op);
  64. if(op == 0)
  65. {
  66. scanf("%d",&x);
  67. if(mark[x])
  68. {
  69. printf("ERROR! At point %d\n",x);
  70. continue;
  71. }
  72. mark[x] = 1;
  73. Floyd(x);
  74. }
  75. else if(op == 1)
  76. {
  77. scanf("%d%d",&x,&y);
  78. if(!mark[x] || !mark[y])
  79. {
  80. printf("ERROR! At path %d to %d\n",x,y);
  81. continue;
  82. }
  83. if(dis[x][y] >= INF)
  84. printf("No such path\n");
  85. else
  86. printf("%d\n",dis[x][y]);
  87. }
  88. }
  89. }
  90. return 0;
  91. }

hdu 3631 Shortest Path(Floyd)的更多相关文章

  1. HDU 5636 Shortest Path(Floyd)

    题目链接  HDU5636 n个点,其中编号相邻的两个点之间都有一条长度为1的边,然后除此之外还有3条长度为1的边. m个询问,每次询问求两个点之前的最短路. 我们把这三条边的6个点两两算最短路, 然 ...

  2. HDU-3631 Shortest Path (floyd)

    Description When YY was a boy and LMY was a girl, they trained for NOI (National Olympiad in Informa ...

  3. HDU - 3631 Shortest Path(Floyd最短路)

    Shortest Path Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u SubmitStat ...

  4. Shortest Path(hdu5636)

    Shortest Path  Accepts: 40  Submissions: 610  Time Limit: 4000/2000 MS (Java/Others)  Memory Limit: ...

  5. HDU - 1973 - Prime Path (BFS)

    Prime Path Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  6. hdu 3631 Shortest Path

    floyd算法好像很奇妙的样子.可以做到每次加入一个点再以这个点为中间点去更新最短路,效率是n*n. #include<cstdio> #include<cstring> #i ...

  7. HDU ACM 1869 六度分离(Floyd)

    六度分离 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  8. Leetcode 943. Find the Shortest Superstring(DP)

    题目来源:https://leetcode.com/problems/find-the-shortest-superstring/description/ 标记难度:Hard 提交次数:3/4 代码效 ...

  9. HDU 5938 Four Operations(四则运算)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

随机推荐

  1. .NET Framework 中的类型系统的两个基本点

    它支持继承原则. 类型可从称为基类型的其他类型派生. 派生类型继承基类型的方法.属性和其他成员(存在一些限制). 之后,基类型可从某些其他类型派生,这种情况下,派生类型继承其层次结构中这两个基类型的成 ...

  2. Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a tableview cell's content view...

    Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a tabl ...

  3. Java多线程和线程池

    转自:http://blog.csdn.net/u013142781/article/details/51387749 1.为什么要使用线程池 在Java中,如果每个请求到达就创建一个新线程,开销是相 ...

  4. RSA加密算法正确性证明

    RSA加密算法是利用大整数分解耗时非常大来保证加密算法不被破译. 密钥的计算过程为:首先选择两个质数p和q,令n=p*q. 令k为n的欧拉函数,k=ϕ(n)=(p−1)(q−1) 选择任意整数a,保证 ...

  5. openssl 使用非阻塞 bio

    序 在项目中需要访问 https 加密的网页,为了保证并发性,需要用到非阻塞的 socket,搜索发现,这种使用场景的相关介绍不是很多,所以这里记录一下使用的过程. 在项目中,所使用的 ssl 库是老 ...

  6. 获取fragment中对应的控件的写法

    getActivity().findViewById(id);//用来关联activity中加载的控件id

  7. set_time_limit() 控制页面运行时间

    当你的页面有大量数据时,建议使用set_time_limit()来控制运行时间,默认是30s,所以需要你将执行时间加长点,如 set_time_limit(300)  ,其中将秒数设为0 ,表示持续运 ...

  8. CSS实现div居中

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  9. python+flask+mongodb+whoosh实现自己的搜索引擎(一):目录

    python+flask+jieba+mongodb+whoosh实现自己的搜索引擎 一.目录 二.基于python的爬虫 三.网页去燥,URL去重 四.基于mongodb的数据存储 五.基于whoo ...

  10. 未能写入输出文件 c:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/Temporary

    ERROR: 未能写入输出文件“c:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/Temporary ASP.NET Files/root/.... 一般遇到 ...