Author: CHEN, Yue
Organization: 浙江大学
Time Limit: 200 ms
Memory Limit: 64 MB
Code Size Limit: 16 KB
A1072. 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
Code
//NKW 甲级真题1028
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#define INF 1000000000
using namespace std;
const int maxn = 1100;
int n, m, k, ds, g[1100][1100], dist, db, de, ansid = -1;
char b[5], e[5];
double mindist, ave, ansdist = -1, ansave = INF;
int d[1020];
bool vis[maxn];
int getid(char s[]){
int len = strlen(s), id = 0;
for (int i = 0; i < len; i++)
if (s[i] != 'G')
id = id * 10 + (s[i] - '0');
if (s[0] == 'G') return n + id;
else return id;
}
void dijkstra(int s){
fill(d, d + 1020, INF);
memset(vis, 0, sizeof(vis));
d[s] = 0;
for (int i = 0; i < m + n; i++){
int u = 0, min = INF;
for (int v = 1; v <= m + n; v++){
if (vis[v] == 0 && d[v] < min){
u = v;
min = d[v];
//printf("d[%d][%d]=%d\n", s,v,d[v]);
}
}
if (u == 0) return;
vis[u] = 1;
for (int v = 1; v <= m + n; v++)
if (vis[v] == 0 && g[u][v] != INF && d[u] + g[u][v] < d[v])
d[v] = d[u] + g[u][v];
}
}
int main(){
scanf("%d %d %d %d", &n, &m, &k, &ds);
fill(g[0], g[0] + maxn*maxn, INF);
for (int i = 0; i < k; i++){
scanf("%s %s %d", b, e, &dist);
db = getid(b);
de = getid(e);
g[db][de] = dist;
g[de][db] = dist;
}
for (int i = n + 1; i <= m + n; i++){
dijkstra(i);
mindist = INF;
ave = 0;
for (int j = 1; j <= n; j++){
if (d[j] < mindist)
mindist = d[j];
if (d[j] > ds){
mindist = -1;
break;
}
ave += 1.0 * d[j] / n;
}
if (mindist == -1) continue;
if (mindist > ansdist){
ansid = i;
ansdist = mindist;
ansave = ave;
}
else if (mindist == ansdist&&ave < ansave){
ansid = i;
ansave = ave;
}
}
if (ansid != -1){
printf("G%d\n", ansid - n);
printf("%.1f %.1f\n", ansdist, ansave);
}
else printf("No Solution\n");
return 0;
}
Analysis
-已知有m个加油站,从中选取1个站点,要求没有超出服务范围ds,且离居民区的最近的人最远。
-如果有很多个最远的加油站,输出距离所有居民区距离平均值最小的那个。如果平均值还是一样,就输出按照顺序排列加油站编号最小的那个。