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
题意:
N所居民房M个加油站待建点和K条无向边,使所有居民房都在服务范围DS内,同时使距离最近的居民房距离加油站的距离最远,若有多个解则选平均距离最小的,平均距离相同选编号最小的
分析:
居民房编号即为本身,加油站编号为G后数字加N;对每个待建点使用Dijkstra得到所有居民房距离该点的最短距离;最后检查是否有距离超过DS,并检查是否需更新最短距离
代码:
#include
#include
#include
#include
using namespace std;
#define INF 1e9
#define MAXV 1020
int N, M, K, DS, G[MAXV][MAXV], d[MAXV];
bool vis[MAXV] = {false};
//Dijkstra算法求所有顶点到起点s的最短距离
void Dijkstra(int s){
memset(vis, false, sizeof(vis));
fill(d, d+MAXV, INF);
d[s] = 0;
for(int i=0;i<N+M;i++){
int u=-1, MIN=INF;//u为使d[u]最小的顶点编号,MIN存放最小d[u]
for(int j=1;j<=N+M;j++)//找到未访问的顶点中d[]最小的
if(vis[j]==false && d[j]<MIN){
u = j;
MIN = d[j];
}
if(u==-1) return;//找不到说明不连通
vis[u] = true;
for(int v=1;v<=N+M;v++)
if(vis[v]==false && G[u][v]!=INF)
if(d[u]+G[u][v]<d[v]) d[v] = d[u] + G[u][v];
}
}
//将str[]转换为数字,若str是数字则返回本身,否则返回去掉G之后的数加N
int getID(char str[]){
int i=0, len=strlen(str), ID=0;
while(i<len){
if(str[i]!='G') ID = ID*10 + (str[i]-'0');
i++;
}
if(str[0]=='G') return N+ID;
else return ID;
}
int main()
{
cin>>N>>M>>K>>DS;
int u, v, w;
char city1[5], city2[5];
fill(G[0], G[0]+MAXV*MAXV, INF);
for(int i=0;i<K;i++){
cin>>city1>>city2>>w;//以字符串读入城市编号
u = getID(city1);
v = getID(city2);
G[v][u] = G[u][v] = w;
}
double ansDis=-1, ansAvg=INF;//最大最短距离,最小平均距离
int ansID = -1;//最终加油站ID
for(int i=N+1;i<=N+M;i++){//枚举所有加油站
double minDis=INF, avg=0;//minDis为最大的最近距离,avg为平均距离
Dijkstra(i);//进行Dijkstra算法,求出d数组
for(int j=1;j<=N;j++){
if(d[j]>DS){//存在距离大于DS的居民房则直接跳出
minDis = -1;
break;
}
if(d[j]<minDis) minDis = d[j];//更新最大的最近距离
avg += 1.0*d[j]/N;
}
if(minDis==-1) continue;//存在距离大于DS的居民房,跳过该加油站
if(minDis>ansDis){//更新最大的最近距离
ansID = i;
ansDis = minDis;
ansAvg = avg;
}else if(minDis==ansDis && avg<ansAvg){
ansID = i;
ansAvg = avg;
}
}
if(ansID==-1) cout<<"No Solution"<<endl;
else{
cout<<"G"<<ansID-N<<endl;
printf("%.1f %.1f\n",ansDis,ansAvg);
}
return 0;
}