经典指数          
原因
1139
浏览数
0
收藏数
 

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. 输入描述: Each input file contains one test case. For each case, the first line contains 4 positive integers: N (3), the total number of houses; M (4), 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 formatP1 P2 Distwhere 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. 输出描述: 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”. 输入例子: 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 输出例子: G1 2.0 3.3

     举报   纠错  
 
切换
1 个答案
几个需要注意的地方:         1.输入判断。  可能会有“G5 G5 10”这样的输入,注意筛选,选取最小值         2.判断是住宅还是加油站。         3.判断是否超出加油站覆盖范围         4.多解时最优解的筛选 总体思路:         1.输入数据         2.用Floyd算法求出两点之间的最短距离         3.逐个判断哪个加油站是有效最优解         4.按要求输出结果 代码如下: #include #include #include #include #include using namespace std; int main() { // 读入数据 int N, M, K, ds; scanf("%d %d %d %d", &N, &M, &K, &ds); vector > dist(N+M+1, vector(N+M+1, INT_MAX/2)); // 给对角线填上值 for(int i=1; i<=N+M; i++) { dist[i][i] = 0; } int src, des, tempDist; char str[5]; for(int i=0; i (dist[i][k]+dist[k][j])) { dist[i][j] = dist[i][k] + dist[k][j]; } } } } // 选出最优解 bool isFind = false, isOk; int minDist=0, tempMinDist, index; int sumDist=INT_MAX/2, tempSumDist; for(i=N+1; i<=N+M; i++) { tempSumDist = 0; isOk = true; tempMinDist = INT_MAX; for(j=1; j<=N; j++) { if(dist[i][j] > ds) { isOk = false; break; } else { tempSumDist += dist[i][j]; if(dist[i][j] < tempMinDist) { tempMinDist = dist[i][j]; } } } if(isOk) { if(tempMinDist > minDist) { isFind = true; index = i; minDist = tempMinDist; sumDist = tempSumDist; } else if (tempMinDist == minDist && tempSumDist < sumDist) { isFind = true; index = i; sumDist = tempSumDist; } } } // 输出结果 if(isFind) { double avg = sumDist*1.0/N; printf("G%d\n%d.0 %.1f\n", index-N, minDist, avg); } else { printf("No Solution\n"); } return 0; }
 
切换
撰写答案
扫描后移动端查看本题