码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 2923 floyd一次性求最小边

时间:2015-01-29 14:01:16      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:

  1. #include <stdio.h>
  2. #include <string>
  3. #include <iostream>
  4. #include <queue>
  5. #include <map>
  6. using namespace std;
  7. #define INF 0x7fffff
  8. #define MAX 200
  9. int path[MAX][MAX];
  10. int arry[10000];
  11. map<string, int>list;
  12. map<string, int>::iterator iter;
  13. void input(int m, int n)
  14. {
  15. list.clear();
  16. string tp;
  17. int flag = 0;
  18. int flag1 = 0;
  19. cin >> tp;
  20. list[tp] = ++flag;
  21. for(int i=1; i<=m; i++)
  22. {
  23. cin >> tp;
  24. iter = list.find(tp);
  25. if(iter == list.end() )
  26. {
  27. list[tp] = ++flag;
  28. arry[++flag1] = flag;
  29. }
  30. else
  31. arry[++flag1] = iter->second;
  32. }
  33. string tp1, tp3, tp2;
  34. int go, to;
  35. for(int i=1; i<=n; i++)
  36. {
  37. cin >> tp1 >> tp2 >> tp3;
  38. iter = list.find(tp1);
  39. if( iter != list.end() ) go = iter->second;
  40. else
  41. {
  42. list[tp1] = ++flag;
  43. go = flag;
  44. }
  45. iter = list.find(tp3);
  46. if( iter != list.end() ) to = iter->second;
  47. else
  48. {
  49. list[tp3] = ++flag;
  50. to = flag;
  51. }
  52. int num = 0;
  53. for (int j = 0; j < tp2.size(); j++)
  54. if (tp2[j] >= ‘0‘ && tp2[j] <= ‘9‘)
  55. {
  56. num *= 10;
  57. num += tp2[j] - ‘0‘;
  58. }
  59. if(tp2[0] == ‘<‘ )
  60. {
  61. if(path[to][go] > num)
  62. {
  63. path[to][go] = num;
  64. }
  65. }
  66. if(tp2[tp2.length()-1] == ‘>‘)
  67. {
  68. if(path[go][to] > num)
  69. path[go][to] = num;
  70. }
  71. }
  72. }
  73. void initiaze(int n)
  74. {
  75. for(int i=0; i<=n; i++)
  76. {
  77. for(int j=0; j<=n; j++)
  78. {
  79. path[i][j] = path[j][i] = INF;
  80. }
  81. }
  82. }
  83. void floyd (int n)
  84. {
  85. int i, j, k;
  86. for (k = 1; k <= n; k++)
  87. for (i = 1; i <= n; i++)
  88. for (j = 1; j <= n; j++)
  89. if (path[i][k] + path[k][j] < path[i][j])
  90. path[i][j] = path[i][k] + path[k][j];
  91. }
  92. int main()
  93. {
  94. // freopen("read.txt", "r", stdin);
  95. int co = 1;
  96. int m, n, k;
  97. while(~scanf("%d%d%d", &m, &n, &k) && (m+n+k) )
  98. {
  99. initiaze(m);
  100. input(n, k);
  101. floyd(m);
  102. int all = 0;
  103. for(int i=1; i<=n; i++)
  104. {
  105. if(arry[i] != 1)
  106. all += (path[1][arry[i]] + path[arry[i]][1]);
  107. }
  108. printf("%d. %d\n", co++, all);
  109. }
  110. return 0;
  111. }
  112. /*
  113. Problem Description
  114. Einbahnstra e (German for a one-way street) is a street on which vehicles should only move in one direction. One reason for having one-way streets is to facilitate a smoother flow of traffic through crowded areas. This is useful in city centers, especially old cities like Cairo and Damascus. Careful planning guarantees that you can get to any location starting from any point. Nevertheless, drivers must carefully plan their route in order to avoid prolonging their trip due to one-way streets. Experienced drivers know that there are multiple paths to travel between any two locations. Not only that, there might be multiple roads between the same two locations. Knowing the shortest way between any two locations is a must! This is even more important when driving vehicles that are hard to maneuver (garbage trucks, towing trucks, etc.)
  115. You just started a new job at a car-towing company. The company has a number of towing trucks parked at the company‘s garage. A tow-truck lifts the front or back wheels of a broken car in order to pull it straight back to the company‘s garage. You receive calls from various parts of the city about broken cars that need to be towed. The cars have to be towed in the same order as you receive the calls. Your job is to advise the tow-truck drivers regarding the shortest way in order to collect all broken cars back in to the company‘s garage. At the end of the day, you have to report to the management the total distance traveled by the trucks.
  116. Input
  117. Your program will be tested on one or more test cases. The first line of each test case specifies three numbers (N , C , and R ) separated by one or more spaces. The city has N locations with distinct names, including the company‘s garage. C is the number of broken cars. R is the number of roads in the city. Note that 0 < N < 100 , 0<=C < 1000 , and R < 10000 . The second line is made of C + 1 words, the first being the location of the company‘s garage, and the rest being the locations of the broken cars. A location is a word made of 10 letters or less. Letter case is significant. After the second line, there will be exactly R lines, each describing a road. A road is described using one of these three formats:
  118. A -v -> B
  119. A <-v - B
  120. A <-v -> B
  121. A and B are names of two different locations, while v is a positive integer (not exceeding 1000) denoting the length of the road. The first format specifies a one-way street from location A to B , the second specifies a one-way street from B to A , while the last specifies a two-way street between them. A , ``the arrow", and B are separated by one or more spaces. The end of the test cases is specified with a line having three zeros (for N , C , and R .)
  122. The test case in the example below is the same as the one in the figure.
  123. Output
  124. For each test case, print the total distance traveled using the following format:
  125. k . V
  126. Where k is test case number (starting at 1,) is a space, and V is the result.
  127. Sample Input
  128. 4 2 5
  129. NewTroy Midvale Metrodale
  130. NewTroy <-20-> Midvale
  131. Midvale --50-> Bakerline
  132. NewTroy <-5-- Bakerline
  133. Metrodale <-30-> NewTroy
  134. Metrodale --5-> Bakerline
  135. 0 0 0
  136. Sample Output
  137. 1. 80
  138. */





附件列表

     

    hdu 2923 floyd一次性求最小边

    标签:

    原文地址:http://www.cnblogs.com/sober-reflection/p/5316a9e1c20310c50dc27a28d54a51a4.html

    (0)
    (0)
       
    举报
    评论 一句话评论(0
    登录后才能评论!
    © 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
    迷上了代码!