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 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
题意
给出N个居民点和M个加油站的候选位置以及加油站的服务距离Ds,求离最近居民点最远且平均距离最近的加油站(必须能服务到每一个居民点)。
思路
将题目中1~N映射到0~N-1,G1~GM映射到N~N+M-1,分别以G1~GM为源点做M次Dijkstra求最短路,依次判断并更新求得最终结果。
我使用的是改进版的Dijkstra算法,比《算法笔记》上略快一些。
《算法笔记》对应代码提交结果
改进版Dijkstra算法提交结果
代码
#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
#include <algorithm>
#include <limits.h>
#define MAX_N 1000
#define MAX_K 10000
using namespace std;
struct Edge{
int to;
int weight;
Edge(int to, int weight) : to(to), weight(weight){};
};
struct Node{
int to;
int weight;
Node(int to, int weight) : to(to), weight(weight){};
};
bool operator < (Node a,Node b){
return a.weight < b.weight;
}
int N, M, K, D;
int getIndex(char s[]){
int start = 0, index = 0;
if(s[0] == 'G') start = 1;
for(int i = start, l = (int)strlen(s); i < l; i++){
index = index * 10 + (s[i] - '0');
}
if(start == 0){
return index - 1;
}else{
return index + N - 1;
}
}
int bestId = -1;
float bestTotal = 0, bestMin = 0;
void update(int id, int distTo[]){
float min = -1, total = 0;
for(int i = 0; i < N; i++){
if(distTo[i] > D){// 遇到一个距离大于服务半径的居民点,退出
return;
}else if(min == -1 || distTo[i] < min){
min = distTo[i];
}
total += distTo[i];
}
// 根据题意更新答案
if(bestId == -1 || min > bestMin || (min == bestMin && total < bestTotal)){
bestTotal = total;
bestMin = min;
bestId = id;
}
}
int main() {
scanf("%d %d %d %d", &N, &M, &K, &D);
vector<vector<Edge>> G(N + M);
char s1[5], s2[5];
for(int i = 0, weight, u, v; i < K; i++){
scanf("%s %s %d", s1, s2, &weight);
u = getIndex(s1);
v = getIndex(s2);
G[u].push_back(Edge(v, weight));
G[v].push_back(Edge(u, weight));
}
priority_queue<Node> pq;
bool marked[MAX_N + MAX_K];
int distTo[MAX_N + MAX_K];
int l = N + M;
for(int i = N, u, v; i < l; i++){
// Dijkstra求最短路
fill(marked, marked + l, false);
fill(distTo, distTo + l, INT_MAX);
pq.push(Node(i, 0));
distTo[i] = 0;
while (!pq.empty()) {
u = pq.top().to;
pq.pop();
if(marked[u]) continue;
for(Edge& e : G[u]){
v = e.to;
if(marked[v]) continue;
if(distTo[u] + e.weight < distTo[v]){
distTo[v] = distTo[u] + e.weight;
pq.push(Node(v, distTo[v]));
}
}
}
// 更新答案
update(i - N, distTo);
}
if(bestId == -1){
printf("No Solution\n");
}else{
printf("G%d\n%.1f %.1f",bestId + 1, bestMin, bestTotal / N);
}
return 0;
}