PAT(图)——1072 Gas Station (30 分)
1072 Gas Station (30 分)
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个候选的加油站点,给出他们之间的路径,已知加油站能服务到的范围,求满足条件的加油站。
题目解析:
用dijkstra算法,注意加油站之间的路也可以通行。
具体代码:
#include<iostream>
#include<ctype.h>
#include<string>
#include<climits>
using namespace std;
#define MAXN 1020
#define DMAXN 999999999
int n,m;//n houses m stations
int k,d;//k roads d ranges of stations
double A[MAXN][MAXN];
int dis[MAXN];//minimum distances of src to i
bool vis[MAXN];
double mindist=0,alldist=0;
void dijkstra(int src) { //minimum distances of single src
fill(vis,vis+MAXN,false);
for(int i=1; i<=n+m; i++)
dis[i]=A[src][i];
dis[src]=0;
for(int i=1; i<=n+m; i++) {
int min=DMAXN,k=-1;
for(int j=1; j<=n+m; j++)
if(vis[j]==false&&dis[j]<min) {
min=dis[j];
k=j;
}
if(k==-1)
break;
vis[k]=true;
for(int j=1; j<=n+m; j++)
if(vis[j]==false&&dis[j]>dis[k]+A[k][j])
dis[j]=dis[k]+A[k][j];
}
}
int judge(int src) {
for(int i=1; i<=n; i++)
if(dis[i]>d) {
return 0;
}
return 1;
}
double getmin(int src) {
double min=DMAXN;
for(int i=1; i<=n; i++)
if(dis[i]<min)
min=dis[i];
return min;
}
double getall(int src) {
double alldist=0.0;
for(int i=1; i<=n; i++){
alldist+=dis[i];
}
return alldist;
}
int main() {
fill(A[0],A[0]+MAXN*MAXN,DMAXN);
cin>>n>>m>>k>>d;
for(int i=0; i<k; i++) {
string p1,p2;
double dist;
int a,b;
cin>>p1>>p2>>dist;
if(isdigit(p1[0]))
if(isdigit(p2[0])) {
a=stoi(p1);
b=stoi(p2);
} else {
a=stoi(p1);
b=stoi(p2.substr(1))+n;
}
else if(isdigit(p2[0])) {
a=stoi(p1.substr(1))+n;
b=stoi(p2);
} else {
a=stoi(p1.substr(1))+n;
b=stoi(p2.substr(1))+n;
}
if(a==b)
dist=0;
A[a][b]=A[b][a]=dist;
}
int flag=0,best;
for(int i=n+1; i<=n+m; i++) {
dijkstra(i);
if(judge(i))
flag=1;
else
continue;
int tmp_min=getmin(i);
int tmp_all=getall(i);
if(tmp_min>mindist||tmp_min==mindist&&tmp_all<alldist) {
best=i;
alldist=tmp_all;
mindist=tmp_min;
}
}
if(flag==0)
printf("No Solution\n");
else
printf("G%d\n%.1f %.1f\n",best-n,mindist,alldist/n);
return 0;
}