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 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.
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
考点:Dijsktra+字符串的处理
1.substr的用法:
string sub1 = s.substr(5); //只有一个数字5表示从下标为5开始一直到结尾
string sub2 = s.substr(5, 3); //从下标为5开始截取长度为3位
2.stoi的用法
注意点:本题的要求是求一个加油站到所有的房子之间的距离的最短距离最大。然后求出该加油站到所有房子的平均距离。
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int inf = 999999999;
int e[1015][1015],dis[1015];
bool visit[1015];
int main(){
int n,m,k,d,sum=inf,cnt=0,minn=0,minindex;
cin>>n>>m>>k>>d;
fill(e[0],e[0]+1015*1015,inf);
for(int i = 0; i < k; i++){
string a,b;
int temp,tempa,tempb;
cin>>a>>b>>temp;
if(a[0] == 'G')
tempa = stoi(a.substr(1))+n;
else
tempa = stoi(a);
if(b[0] == 'G')
tempb = stoi(b.substr(1))+n;
else
tempb = stoi(b);
e[tempa][tempb] = e[tempb][tempa] = temp;
}
for(int i = 1; i <= m; i++){
fill(dis,dis+1015,inf);
fill(visit,visit+1015,false);
dis[n+i] = 0;
for(int i = 0; i < m+n; i++){
int u=-1,mindis=inf;
for(int j = 1; j <= m+n; j++)
if(visit[j]==false && dis[j]<mindis){
u = j;
mindis = dis[j];
}
if(u == -1)
break;
visit[u] = true;
for(int v = 1; v <= m+n; v++)
if(visit[v] == false && e[u][v]+dis[u] < dis[v])
dis[v] = e[u][v]+dis[u];
}
int tempsum = 0,tempminn = inf;
for(int j = 1; j <= n; j++){
if(dis[j]>d){
tempsum = -1;
break;
}
else
tempsum += dis[j];
tempminn = min(dis[j],tempminn);
}
if(tempsum != -1){
cnt++;
if(tempminn > minn){
minindex = i;
minn = tempminn;
sum = tempsum;
}
else if(tempminn == minn && tempsum < sum){
minindex = i;
sum = tempsum;
}
}
}
if(cnt == 0)
printf("No Solution");
else
printf("G%d\n%.1f %.1f",minindex, 1.0*minn,(1.0*sum)/n);
}