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 (≤10
3
), the total number of houses; M (≤10), the total number of the candidate locations for the gas stations; K (≤10
4
), the number of roads connecting the houses and the gas stations; and D
S
, 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
The main idea of the topic: there are gasStationNum stations and houseNum residential buildings. Choose a site to maximize the minimum distance from the residential building. If there are more than one, choose the one with the smallest average distance from all residential buildings. If there are still more than one, choose the one with the smallest serial number.
Analysis: Due to the distance between the site and the site, the site and the residential building, the information of the site should also be put into a two-dimensional array. Using a different station as the starting point each time, Dijkstra's algorithm is used to find the shortest path. Traverse the dis array, and if a value greater than maxScale is found, discard the site. Find the shortest path length, compare the shortest path lengths of each station, and choose the largest.
Input:
I use a two-dimensional array to store path information here, put the information of houseNum residential buildings in the front, and put the information of gasStationNum sites in the back. Use a string to receive, if the first character is G, it is judged to be a station and put in the back. Note that the sites and residential buildings here may have multiple digits. I chose to use the stoi function to convert digital strings into numbers (this part of the conversion refers to the code of God Liu, and the last point of this step is missing)
scanf("%d %d %d %d", &houseNum, &gasStationNum, &roadNum, &maxScale);
string a, b;
int a2,b2,c;
for (int i = 0; i < roadNum;i++){
cin >> a >> b >> c;
/*这里不能用scanf,scanf输入字符串是针对char*或char[]的,即scanf只能输入C语言中的内置数据
同理,printf不能直接输出字符串,要用输入输出流*/
if(a[0]=='G'){
a = a.substr(1);//从位置1开始,取到末尾
a2 = houseNum+stoi(a);//stoi用于将数字字符串转化成数字输出
}
else{
a2 = stoi(a);
}
if(b[0]=='G'){
b = b.substr(1);
b2 = houseNum+stoi(b);
}
else{
b2 = stoi(b);
}
road[a2][b2] = road[b2][a2] = c;
}//前面的代表居民楼,后面的代表站点
Dijkstra's algorithm to find the shortest path:
This is nothing to say, but multiple loops outside to find the shortest path of multiple nodes
for (int i = houseNum + 1; i <= houseNum + gasStationNum;i++){
//每次以不同的gasStation为起点寻找最短路径
double mindis = inf, aver = 0;
fill(minDis, minDis + 1011, inf);
fill(visit, visit + 1011, false);
minDis[i] = 0;
for (int j = 0; j < houseNum + gasStationNum;j++){
//gasStation也有路径长度,也要参与遍历
int u = -1, minn = inf;
for (int k = 1; k <= gasStationNum + houseNum;k++){
if(visit[k]==false&&minDis[k]<minn){
u = k;
minn = minDis[k];
}
}
if(u==-1)
break;
visit[u] = true;
for (int k = 1; k <= houseNum + gasStationNum;k++){
if(visit[k]==false&&minDis[u]+road[u][k]<minDis[k]){
minDis[k] = minDis[u] + road[u][k];
}
}
}
AC code
#include<bits/stdc++.h>
using namespace std;
const int inf = 9999999;
int road[1011][1011], minDis[1011];
bool visit[1011];
int houseNum, gasStationNum, roadNum, maxScale;
int main(){
fill(road[0], road[0] + 1011 * 1011, inf);//不存在的路径设置为inf,便于下面得到最短路径
scanf("%d %d %d %d", &houseNum, &gasStationNum, &roadNum, &maxScale);
string a, b;
int a2,b2,c;
for (int i = 0; i < roadNum;i++){
cin >> a >> b >> c;
/*这里不能用scanf,scanf输入字符串是针对char*或char[]的,即scanf只能输入C语言中的内置数据
同理,printf不能直接输出字符串,要用输入输出流*/
if(a[0]=='G'){
a = a.substr(1);//从位置1开始,取到末尾
a2 = houseNum+stoi(a);//stoi用于将数字字符串转化成数字输出
}
else{
a2 = stoi(a);
}
if(b[0]=='G'){
b = b.substr(1);
b2 = houseNum+stoi(b);
}
else{
b2 = stoi(b);
}
road[a2][b2] = road[b2][a2] = c;
}//前面的代表居民楼,后面的代表站点
int ansid=-1;
double ansdis = -1, ansaver = inf;//便于下面寻找最短路径中的最大值
for (int i = houseNum + 1; i <= houseNum + gasStationNum;i++){
//每次以不同的gasStation为起点寻找最短路径
double mindis = inf, aver = 0;
fill(minDis, minDis + 1011, inf);
fill(visit, visit + 1011, false);
minDis[i] = 0;
for (int j = 0; j < houseNum + gasStationNum;j++){
//gasStation也有路径长度,也要参与遍历
int u = -1, minn = inf;
for (int k = 1; k <= gasStationNum + houseNum;k++){
if(visit[k]==false&&minDis[k]<minn){
u = k;
minn = minDis[k];
}
}
if(u==-1)
break;
visit[u] = true;
for (int k = 1; k <= houseNum + gasStationNum;k++){
if(visit[k]==false&&minDis[u]+road[u][k]<minDis[k]){
minDis[k] = minDis[u] + road[u][k];
}
}
}
for (int k = 1; k <= houseNum;k++){
//
if(minDis[k]>maxScale){
mindis = -1;
break;
}
if(minDis[k]<mindis)
mindis = minDis[k];//找到居民楼到一个站点的最小值
aver += minDis[k] * 1.0;
}
if(mindis==-1)
continue;//此时代表有居民楼到站点的距离大于maxScale
aver = aver / houseNum;
if(mindis>ansdis){
//发现更大的最小值
ansdis = mindis;
ansid = i;
ansaver = aver;
}
else if(mindis==ansdis&&aver<ansaver){
//最小值相同,选择距离所有居⺠区距离平均值最⼩的
ansid = i;
ansaver = aver;
}
}
if(ansid==-1)
printf("No Solution");
else{
printf("G%d\n", ansid-houseNum);
printf("%.1lf %.1lf", ansdis, ansaver);
}
}