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

1072. Gas Station (30)

时间:2015-12-06 12:51:33      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

dij适合计算单源最短路径,计算一个源到各个点的距离比较快

dfs不适合算每个点到每个点的距离,迭代次数太多、

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

A gas station has to be built at such a location that the minimum distance between the station and any of the residential housing is as far away as possible. However it must guarantee that all the houses are in its service range.

Now given the map of the city and several candidate locations for the gas station, you are supposed to give the best recommendation. If there are more than one solution, output the one with the smallest average distance to all the houses. If such a solution is still not unique, output the one with the smallest index number.

Input Specification:

Each input file contains one test case. For each case, the first line contains 4 positive integers: N (<= 103), the total number of houses; M (<= 10), the total number of the candidate locations for the gas stations; K (<= 104), the number of roads connecting the houses and the gas stations; and DS, the maximum service range of the gas station. It is hence assumed that all the houses are numbered from 1 to N, and all the candidate locations are numbered from G1 to GM.

Then K lines follow, each describes a road in the format
P1 P2 Dist
where P1 and P2 are the two ends of a road which can be either house numbers or gas station numbers, and Dist is the integer length of the road.

Output Specification:

For each test case, print in the first line the index number of the best location. In the next line, print the minimum and the average distances between the solution and all the houses. The numbers in a line must be separated by a space and be accurate up to 1 decimal place. If the solution does not exist, simply output “No Solution”.

Sample Input 1:
4 3 11 5
1 2 2
1 4 2
1 G1 4
1 G2 3
2 3 2
2 G2 1
3 4 2
3 G3 2
4 G1 3
G2 G1 1
G3 G2 2
Sample Output 1:
G1
2.0 3.3
Sample Input 2:
2 1 2 10
1 G1 9
2 G1 20
Sample Output 2:
No Solution

 
dij的通过算法

  1. #include<iostream>
  2. #include<map>
  3. #include<vector>
  4. #include<iostream>
  5. #include<stdio.h>
  6. #include<string>
  7. #include<string.h>
  8. #pragma warning(disable:4996)
  9. #define INF 1<<30
  10. using namespace std;
  11. int town[1021][1021] = { 0 };
  12. int mark[1021];
  13. map<string, int> loc;
  14. int n, m, k;
  15. double ds;
  16. int locCnt = 0;
  17. int minDis[12] = { 0 }, totalDis[12] = { 0 };
  18. int minDisTemp = INF;
  19. int outRangeMark[12] = { 0 };
  20. int dist[1021] = { 0 };
  21. int Str2Int(string s) {
  22. if (loc[s] == 0) {
  23. locCnt++;
  24. loc[s] = locCnt;
  25. return locCnt;
  26. }
  27. else
  28. return loc[s];
  29. }
  30. string Int2Str(int a) {
  31. for (auto &p : loc) {
  32. if (p.second == a)
  33. return p.first;
  34. }
  35. }
  36. void dij(int index) {
  37. int u,v,min=INF;
  38. for (int i = 1; i < n + m; i++) {
  39. min = INF;
  40. for (int j = 1; j <= n + m; j++) {
  41. if (dist[j] < min&&mark[j] == 0) {
  42. u = j;
  43. min = dist[j];
  44. }
  45. }
  46. mark[u] = 1;
  47. for (v = 1; v <= n + m; v++) {
  48. if (town[u][v]!=INF&&dist[v] > dist[u] + town[u][v]) {
  49. dist[v] = dist[u] + town[u][v];
  50. }
  51. }
  52. }
  53. }
  54. int main(void) {
  55. freopen("Text.txt", "r", stdin);
  56. cin >> n >> m >> k >> ds;
  57. for (int j = 0; j <= n + m; j++)
  58. for (int z = 0; z <= n + m; z++) {
  59. if (z == j)
  60. town[z][j] = 0;
  61. else
  62. town[j][z] = town[z][j] = INF;
  63. }
  64. for (int i = 0; i < k; i++) {
  65. string s1, s2;
  66. int dis;
  67. char a[6], b[6];
  68. scanf("%s %s %d", a, b, &dis);
  69. int q = 0;
  70. while (a[q]!=0)
  71. {
  72. s1 += a[q];
  73. q++;
  74. }
  75. q = 0;
  76. while (b[q] != 0)
  77. {
  78. s2 += b[q];
  79. q++;
  80. }
  81. //cin >> s1 >> s2 >> dis;
  82. town[Str2Int(s1)][Str2Int(s2)] = dis;
  83. town[Str2Int(s2)][Str2Int(s1)] = dis;
  84. }
  85. for (int i = 1; i <= m; i++) {
  86. memset(mark, 0, sizeof(mark));
  87. memset(dist, 0, sizeof(dist));
  88. minDisTemp = INF;
  89. string start = "G";
  90. start += i + ‘0‘;
  91. for (int j = 1; j <= n + m; j++)
  92. dist[j] = town[loc[start]][j];
  93. mark[loc[start]] = 1;
  94. dij(loc[start]);
  95. for (int j = 1; j <= n + m; j++) {
  96. if (Int2Str(j)[0] != ‘G‘) {
  97. if (1.0*dist[j] > ds)
  98. outRangeMark[i] = 1;
  99. if (dist[j] < minDisTemp)
  100. minDisTemp = dist[j];
  101. totalDis[i] += dist[j];
  102. minDis[i] = minDisTemp;
  103. }
  104. }
  105. }
  106. int maxIndex = -1, max = -1;
  107. for (int p = 1; p <= m; p++) {
  108. if (minDis[p] > max&&outRangeMark[p]==0) {
  109. max = minDis[p];
  110. maxIndex = p;
  111. }
  112. else if (minDis[p] == max&&totalDis[p] < totalDis[maxIndex] && outRangeMark[p] == 0) {
  113. maxIndex = p;
  114. }
  115. }
  116. if (maxIndex == -1)
  117. cout << "No Solution";
  118. else {
  119. string start = "G";
  120. start += maxIndex + ‘0‘;
  121. cout << start << endl;
  122. printf("%.1f %.1f", 1.0*minDis[maxIndex], 1.0*totalDis[maxIndex] / n);
  123. }
  124. return 0;
  125. }


dfs 的超时算法
  1. #include<iostream>
  2. #include<map>
  3. #include<vector>
  4. #include<iostream>
  5. #include<stdio.h>
  6. #include<string>
  7. #pragma warning(disable:4996)
  8. #define INF 99999999
  9. using namespace std;
  10. int town[1011][1011] = { 0 };
  11. int mark[1011];
  12. map<string, int> loc;
  13. int n, m, k, ds;
  14. int locCnt = 0;
  15. int minDis[11] = { 0 }, totalDis[11] = { 0 };
  16. int minDisTemp = INF;
  17. int outRangeMark[11] = { 0 };
  18. int Str2Int(string s) {
  19. if (loc[s] == 0) {
  20. locCnt++;
  21. loc[s] = locCnt;
  22. return locCnt;
  23. }
  24. else
  25. return loc[s];
  26. }
  27. string Int2Str(int a) {
  28. for (auto &p : loc) {
  29. if (p.second == a)
  30. return p.first;
  31. }
  32. }
  33. void dfs(int startPomt, int endpoint,int length) {
  34. if (length >= minDisTemp)
  35. return;
  36. if (startPomt == endpoint) {
  37. if (length < minDisTemp) {
  38. minDisTemp = length;
  39. }
  40. }
  41. else {
  42. for (int i = n + m; i >=1; i--) {
  43. if (town[startPomt][i] != 0 && mark[i] == 0/*&&Int2Str(i).length()==1*/) {
  44. mark[i] = 1;
  45. dfs(i, endpoint, length + town[startPomt][i]);
  46. mark[i] = 0;
  47. }
  48. }
  49. }
  50. }
  51. int main(void) {
  52. //freopen("Text.txt", "r", stdin);
  53. cin >> n >> m >> k >> ds;
  54. for (int i = 0; i < k; i++) {
  55. string s1, s2;
  56. int dis;
  57. char a[3], b[3];
  58. scanf("%s %s %d", a, b, &dis);
  59. int q = 0;
  60. while (a[q]!=0)
  61. {
  62. s1 += a[q];
  63. q++;
  64. }
  65. q = 0;
  66. while (b[q] != 0)
  67. {
  68. s2 += b[q];
  69. q++;
  70. }
  71. //cin >> s1 >> s2 >> dis;
  72. town[Str2Int(s1)][Str2Int(s2)] = dis;
  73. town[Str2Int(s2)][Str2Int(s1)] = dis;
  74. }
  75. /*for (int i = 0; i < m; i++) {
  76. for (int j = 0; j < n; j++) {
  77. minDisTemp = INF;
  78. string start = "G";
  79. start += i + ‘0‘ + 1;
  80. string end;
  81. end += j + ‘0‘ + 1;
  82. mark[loc[start]] = 1;
  83. dfs(loc[start], loc[end], 0);
  84. mark[loc[start]] = 0;
  85. if (minDis[i] == 0)
  86. minDis[i] = minDisTemp;
  87. else if (minDis[i] != 0 && minDisTemp < minDis[i])
  88. minDis[i] = minDisTemp;
  89. if (minDisTemp > ds) {
  90. outRangeMark[i] = 1;
  91. break;
  92. }
  93. totalDis[i] += minDisTemp;
  94. }
  95. }
  96. int maxIndex = -1, max = 0;
  97. for (int p = 0; p < m; p++) {
  98. if (minDis[p] > max&&outRangeMark[p]==0) {
  99. max = minDis[p];
  100. maxIndex = p;
  101. }
  102. else if (minDis[p] == max&&totalDis[p] < totalDis[maxIndex] && outRangeMark[p] == 0) {
  103. maxIndex = p;
  104. }
  105. }
  106. if (maxIndex == -1)
  107. cout << "No Solution";
  108. else {
  109. string start = "G";
  110. start += maxIndex + ‘0‘ + 1;
  111. cout << start << endl;
  112. printf("%.1f %.1f", 1.0*minDis[maxIndex], 1.0*totalDis[maxIndex] / n);
  113. }
  114. */
  115. return 0;
  116. }





1072. Gas Station (30)

标签:

原文地址:http://www.cnblogs.com/zzandliz/p/5023230.html

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