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 (≤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 G
1 to G
M.
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.
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
2 1 2 10
1 G1 9
2 G1 20
No Solution
Dijkstra的应用。暴力求解,对每个G结点用Dijkstra()求解,每一次的结果按照题设要求判断。优先级如下:
1. 所有居民点在DS范围内;
2. 在满足1的条件下,优先选择最大的nearest(最大化各居民点到加油站的最小距离);
3. 若2不唯一,优先选择总距离(同理平均距离)最小的;
4. 若3不唯一,优先选择标号最小的(由于我们是按照顺序遍历的,若3不唯一不作任何更新即可)。
调试过程中一些几位无语的错误:
1. 寻找新的结点for循环中,把<=写成<;
2. 判断total和bestTotal的时候,符号写反!
这些错误虽然都不是思路上的,但是在考试中相当致命啊!本题编码时间20分钟,调试时间可能达到了2小时!
发现:题目保证了所有结点之间连通。
#include
#include
#include
#define INF 0x3f3f3f3f
using namespace std;
const int MAX = 1017;
int N,M,K,DS;//houses stations roads
int string2int(string s){
//1 to N G1 to GM
bool isStation =false;
if(s[0]=='G'){
s.erase(s.begin());
isStation=true;
}
int ans = 0;
for(int i=0;i graph[MAX];
bool visit[MAX];
int dis[MAX];
int main(void){
cin>>N>>M>>K>>DS;
string a,b;int dest;
while(K--){
cin>>a>>b>>dest;
int p1 = string2int(a);
int p2 = string2int(b);
graph[p1].push_back(Edge{p2,dest});
graph[p2].push_back(Edge{p1,dest});
}
int bestStation = -1;int bestBound = 0;int bestTotal = INF;
for(int k=N+1;k<=N+M;k++){//从k出发
int newP = k;
memset(dis,-1,sizeof(dis));
memset(visit,false,sizeof(visit));
dis[newP]=0;
visit[newP]=true;
int cnt=0;
while(cnt=1&&newP<=N) cnt++;
}
int nearest = INF;int total=0;
bool isOK = true;
for(int i=1;i<=N;i++){
if(dis[i]>DS){
isOK = false;
break;
}
nearest = min(nearest,dis[i]);
total+=dis[i];
}
if(isOK){
if(nearest>bestBound){
bestStation = k;
bestBound = nearest;
bestTotal = total;
}else if(nearest==bestBound&&total