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 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
一,注意点:
1,像这种有字母也有数字的字符串,一定不要忘了不要只取一位数字,也可能有两位数字!!!本题最后一个点就是多位数字。
2,最后想把所有备用点弄成一个结构体,存储最短距离,平均距离,编号,然后sort排序,取第一个。其实不必这样,因为只需要取最值,设三个变量一个个比较存储最值即可。
二,我的代码
#include<cstdio>
#include<algorithm>
#include<string.h>
#include<vector>
using namespace std;
const int max_n = 1100;
const int INF = 1000000000;
int N = 0;
int M = 0;
int D = 0;
double dis[max_n] = { 0 };
bool vis[max_n] = { false };
struct Node {
int v;
int dis;
Node(int _v, int _dis) :v(_v), dis(_dis) {};
};
vector<Node> map[max_n];
int judge(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 id + N;
else return id;
}
void Dijsktra(int s) {
fill(dis, dis + max_n, INF);
fill(vis, vis + max_n, false);
dis[s] = 0;
for (int i = 1; i <= N+M; i++) {
double MIN = INF;
int u = -1;
for (int j = 1; j <= N+M; j++) {
if (vis[j] == false && dis[j] < MIN) {
u = j;
MIN = dis[j];
}
}
if (u == -1)return;
vis[u] = true;
for (int j = 0; j < map[u].size(); j++) {
int v = map[u][j].v;
int d = map[u][j].dis;
if (vis[v] == false && dis[u] + d < dis[v]) {
dis[v] = dis[u] + d;
}
}
}
}
double find_min(double arr[]) {
double min_1 = INF;
for (int i = 1; i <= N; i++) {
if (min_1 > arr[i])
min_1 = arr[i];
}
return min_1;
}
double find_ave(double arr[]) {
double sum = 0;
for (int i = 1; i<=N; i++) {
sum += arr[i];
}
return sum / N;
}
int main() {
int K = 0;
char str1[4], str2[4];
int x = 0, y = 0, z = 0;
scanf("%d %d %d %d", &N, &M, &K, &D);
for (int i = 0; i < K; i++) {
scanf("%s", str1);
scanf("%s", str2);
scanf("%d", &z);
x = judge(str1);
y = judge(str2);
map[x].push_back(Node(y, z));
map[y].push_back(Node(x, z));
}
double real_min = -1, real_ave = INF;
int id = 0;
for (int i = N+1; i <= M+N; i++) {
int ds = 0;
Dijsktra(i);
for (int j = 1; j <= N; j++) {
if (dis[j]>D) {
ds = -1;
break;
}
}
if (ds == -1)continue;
double min_1 = find_min(dis);
double ave_1 = find_ave(dis);
if (min_1 > real_min || min_1 == real_min&&ave_1 < real_ave) {
real_min = min_1;
real_ave = ave_1;
id = i - N;
}
}
if (id == 0) {
printf("No Solution\n");
}
else {
printf("G%d\n", id);
printf("%.1f %.1f", real_min, real_ave);
}
return 0;
}