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
题目大意
加油站选址需要使加油站与任何住宅之间的最小距离尽可能远;但是同时必须保证所有房屋都在其服务范围内。题目给定城市地图和加油站的几个候选地点,题目要求给出地点推荐。如果有多个解,则输出到所有房子的平均距离最小的那个。如果这样的解决方案仍然不是惟一的,则输出索引号最小的解决方案。
解题思路
- 初始化数据并读入地图;
- 用Dijkstra法对每个可能的选址点进行处理,判断是否有不可到达的点,并计算每种方案中加油站到民宅的最短距离;
- 按照题目要求筛选数据;
- 输出结果并返回零值。
代码
#include<stdio.h>
#include<string.h>
#define maxn 1050
#define INF 100000000
struct Node{
int v;
int d;
}Adj[maxn][maxn];
int Num[maxn];
int vis[maxn];
int d[maxn];
double sum[maxn];
int dmin[maxn]={INF,INF,INF,INF,INF,INF,INF,INF,INF,INF,INF,INF};
int Impossible[maxn];//为1时不可达
int N,M,P,K,Dm;
void Init(){
char s[2][7];
int p[2],Dist;
int i,j;
scanf("%d%d%d%d",&N,&M,&K,&Dm);
P=N+M;//总结点数,前N为房
for(i=1;i<=P;i++){
Num[i]=0;
}
while(K--){
scanf("%s %s %d",&s[0],&s[1],&Dist);
for(i=0;i<2;i++){
p[i]=0;
for(j=0;j<7;j++){
if(s[i][j]=='G'){
continue;
}else if(s[i][j]==0){
break;
}else{
p[i]=p[i]*10+s[i][j]-'0';
}
}
if(s[i][0]=='G'){
p[i]+=N;//加油站从N+1开始编号
}
}
Adj[p[0]][Num[p[0]]].v=p[1];
Adj[p[0]][Num[p[0]]].d=Dist;
Num[p[0]]++;
Adj[p[1]][Num[p[1]]].v=p[0];
Adj[p[1]][Num[p[1]]].d=Dist;
Num[p[1]]++;
}
}
void Dijkstra(int S){
int i,j,u,v,min,k;
for(i=1;i<=P;i++){
vis[i]=0;
d[i]=INF;
}
d[S]=0;
k=S-N;
//初始化
for(i=1;i<=P;i++){
u=0,min=INF;
for(j=1;j<=P;j++){
if(vis[j]==0&&d[j]<min){
u=j;
min=d[u];
}
}
if(u==0){
Impossible[k]=1;
return;
}
vis[u]=1;
if(u>0&&u<=N){
if(d[u]>Dm){
Impossible[k]=1;
return;
}
if(d[u]<dmin[k]){
dmin[k]=d[u];
}
sum[k]+=d[u];
}
for(j=0;j<Num[u];j++){
v=Adj[u][j].v;
if(vis[v]==0&&d[v]>d[u]+Adj[u][j].d){
d[v]=d[u]+Adj[u][j].d;
}
}
}
return;
}
int main(){
int i;
int u,min;
Init();
for(i=1;i<=M;i++){
Dijkstra(i+N);
if(Impossible[i]){
sum[i]=INF;
continue;
}else{
sum[i]/=(N*1.0);
}
}
sum[0]=INF;
u=0;
min=-1;
for(i=1;i<=M;i++){
if(Impossible[i]==0&&dmin[i]>min){
u=i;
min=dmin[u];
}else if(Impossible[i]==0&&dmin[i]==min){
if(sum[i]<sum[u]){
u=i;
}
}
}
if(u==0){
printf("No Solution\n");
}else{
printf("G%d\n%.1f %.1f\n",u,1.0*dmin[u],sum[u]);
}
return 0;
}
运行结果