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
题意:
本题的题意开始没有理解,以为最优的第一条件就是平均值最小,但不是这样的。 第一条件:所有候选点中到house最小值最大的那个候选点,第一个测试用例中G1的最小值为2, G2的最小值为1,G3的最小值为2,所以选取候选点G1和G3继续比较; 4 2 4 3 3 1 3 4 5 3 2 4 G1 2.0 3.3 第二条件:平均值最小,第一个测试用例中,G1的平均值小于G3,所以最优解为G3; 第三条件:序号最小;思路:
其实就是求一下G开头的这m个点的单源最短路,然后找到最小距离最大的当中总和最小的当中标号最小的,然后输出。。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<string> using namespace std; const int inf = 0x3f3f3f3f; const int N = 1e3 + 100; int dp[N], vis[N], c[N][N]; char s1[N], s2[N]; struct node { double Min; double aver; int id; }ans[N]; bool cmp(node t1, node t2) { if (t1.Min != t2.Min) return t1.Min>t2.Min; else if (t1.aver != t2.aver) return t1.aver<t2.aver; else return t1.id<t2.id; } void dij(int s, int n) { int i, j, u; fill(dp, dp + 1 + n, inf); fill(vis, vis + 1 + n, 0); dp[s] = 0; while (1) { u = -1; for (i = 1; i <= n; i++) { if (!vis[i] && (u == -1 || dp[i]<dp[u])) u = i; } if (u == -1) break; vis[u] = 1; for (i = 1; i <= n; i++) { dp[i] = min(dp[i], dp[u] + c[u][i]); } } } int main() { int n, m, kk, d, num, t, s, k; double sum, Min, aver; node tmp; int u, v, w; int len1, len2, i, j; scanf("%d%d%d%d", &n, &m, &kk, &d); for (i = 1; i <= n + m; i++) { for (j = 1; j <= n + m; j++) c[i][j] = inf; } while (kk--) { u = v = 0; scanf("%s%s%d", s1, s2, &w); len1 = strlen(s1); len2 = strlen(s2); num = 1; if (s1[0] == 'G') { for (i = len1 - 1; i>0; i--) { u += num*(s1[i] - '0'); num *= 10; } u += n; } else { for (i = len1 - 1; i >= 0; i--) { u += num*(s1[i] - '0'); num *= 10; } } num = 1; if (s2[0] == 'G') { for (i = len2 - 1; i>0; i--) { v += num*(s2[i] - '0'); num *= 10; } v += n; } else { for (i = len2 - 1; i >= 0; i--) { v += num*(s2[i] - '0'); num *= 10; } } c[u][v] = c[v][u] = w; } k = 0; for (j = 1; j <= m; j++) { s = n + j; dij(s, n + m); sum = 0, Min = inf; for (i = 1; i <= n; i++) { if (dp[i]>d) break; if (i == s) continue; Min = min(Min, (double)dp[i]); sum += dp[i]; } aver = sum / n; tmp.aver = aver, tmp.id = s, tmp.Min = Min; if (i>n) ans[k++] = tmp; } sort(ans, ans + k, cmp); if (k == 0) { printf("No Solution\n"); return 0; } printf("G%d\n", ans[0].id - n); printf("%.1lf %.1lf\n", ans[0].Min, ans[0].aver); return 0; }
