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 (≤10
3
), the total number of houses; M (≤10), the total number of the candidate locations for the gas stations; K (≤10
4
), the number of roads connecting the houses and the gas stations; and D
S
, 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
题意分析,有n个住宅,m个加油站,找到符合以下条件的加油站的位置
1.距离所有住宅的最短距离最远
2.如果条件1重复,选择平均距离更小的
3.如果条件2重复,选择序号更小的
算法:对每一个加油站进行一次dijkstra算法,求到每一个住宅的最短距离。与三个条件进行比较,得到满足题意的加油站。
注意点:将加油站看做住宅,将他们的下标映射到住宅后面。
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n, m, k, d, tmpa, tmpb, dist;
cin >> n >> m >> k >> d;
vector<vector<pair<int, int>>>graph(n + m + 1);
string a, b,ret;
for (int i = 0; i < k; i++) {
cin >> a >> b >> dist;
tmpa = (a[0] == 'G' ? stoi(a.substr(1)) + n : stoi(a));
tmpb = (b[0] == 'G' ? stoi(b.substr(1)) + n : stoi(b));
graph[tmpa].push_back({ tmpb,dist });
graph[tmpb].push_back({ tmpa,dist });
}
double min_dist = INT_MIN, ave_dist = INT_MAX;
for (int i = n + 1; i <= n + m; i++) {
vector<double>distance(n + m + 1, INT_MAX);
vector<bool>visited(n + m + 1);
distance[i] = 0;
bool flag = true;
while (1) {
int min_ = INT_MAX, v = -1;
for (int j = 0; j < graph.size(); j++)
if (!visited[j] && distance[j] < min_) {
min_ = distance[j];
v = j;
}
if (v == -1)break;
visited[v] = true;
if (distance[v] > d && v <= n) {
flag = false;
break;
}
for (int j = 0; j < graph[v].size(); j++)
if (!visited[graph[v][j].first] && distance[v] + graph[v][j].second < distance[graph[v][j].first])
distance[graph[v][j].first] = distance[v] + graph[v][j].second;
}
if (!flag) continue;
double total = accumulate(distance.begin()+1,distance.begin()+n+1,0),
min_d = *min_element(distance.begin()+1,distance.begin()+n+1);
if (min_d > min_dist || (min_d == min_dist && (double)total / n < ave_dist)) {
ave_dist = (double)total / n;
ret = "G" + to_string(i - n);
min_dist = min_d;
}
}
if (ave_dist != INT_MAX)
printf("%s\n%.1f %.1f", ret.c_str(), min_dist, ave_dist);
else
printf("No Solution");
return 0;
}