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”.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
#define MaxData 2002
typedef struct
{
int Ne, Nv;
int length[MaxData][MaxData];
}GNode;
typedef GNode* graph;
typedef struct {
int v1, v2;
int len;
}ENode;
typedef ENode * edge;
graph createGraph(int size);
void insertEdge(graph map, edge e);
graph buildGraph(int N, int M, int routes);
int Toint(string v, int n);
void dijkstra(int v,int N,graph map, int ** dist, bool * visited);
int findMin(int v,int N,int ** dist, bool * visited);
int main()
{
int N, M, K, D;
cin >> N >> M >> K >> D;
graph map = new GNode;
int ** dist = new int*[N+M];
bool * visited = new bool[N+M];
for (int i = 0; i < N+M; i++)dist[i] = new int[N + M];
for (int i = 0; i < N+M; i++)visited[i] = false;
map = buildGraph(N, M, K);
int size = N + M;
float * res = new float[N];
float * avg = new float[N];
for (int i = 0; i < M; i++) {
int minimum = 66535;
float sum = 0; bool jud = true;
dijkstra(N + i, size, map, dist, visited);
for (int j = 0; j < N; j++) {
if (dist[N + i][j] > D) {
jud = false;
break;
}
else {
sum += dist[N + i][j];
if (dist[N + i][j] < minimum) minimum = dist[N + i][j];
}
}
if (jud) {
avg[i] = sum / N;
res[i] = minimum;
}
else { res[i] = -2; avg[i] = 65535; }
for (int i = 0; i < N + M; i++)visited[i] = false;
}
int max = -1; int temp=-1;
for (int i = 0; i < M; i++) {
if(res[i]==-2)continue;
if (res[i] >= max) {
if (res[i] == max) {
if (avg[i] < avg[temp])
temp = i;
}
else {
max = res[i];
temp = i;
}
}
}
if (max == -1) cout << "No Solution";
else {
cout << 'G' << temp+1 << endl << fixed << setprecision(1) << res[temp] << ' ' << fixed << setprecision(1) << avg[temp];
}
return 0;
}
graph createGraph(int size) {
graph map = new GNode;
map->Nv = size;
map->Ne = 0;
for (int i = 0; i < size; i++)
for (int j = 0; j < size; j++) {
if (i == j) map->length[i][j] = 0;
else map->length[i][j] = 65535;
}
return map;
}
void insertEdge(graph map, edge e) {
map->length[e->v1][e->v2] = e->len;
map->length[e->v2][e->v1] = e->len;
}
graph buildGraph(int N,int M,int routes) {
graph map = new GNode;
int size = N + M;
map = createGraph(size);
for(int i = 0; i < routes; i++) {
string v1, v2;
int n1, n2,l;
cin >> v1 >> v2>>l;
n1 = Toint(v1, N) - 1;
n2 = Toint(v2, N) - 1;
edge e = new ENode;
e->v1 = n1; e->v2 = n2; e->len = l;
insertEdge(map, e);
}
return map;
}
int Toint(string v,int n) {
int len = v.length();
int num = 0;
if (v[0] == 'G') {
for (int i = 1; i < len; i++) {
num = num * 10 + v[i] - '0';
}
num += n ;
}
else {
for (int i = 0; i < len; i++) {
num = num * 10 + v[i] - '0';
}
}
return num;
}
void dijkstra(int v, int N, graph map, int ** dist, bool * visited) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
dist[i][j] = map->length[i][j];
}
}
visited[v] = true;
int min = findMin(v,N, dist,visited);
while (min != -1) {
for (int i = 0; i < N; i++) {
if (!visited[i]&&dist[v][min] + dist[min][i] < dist[v][i]) {
dist[v][i] = dist[v][min] + dist[min][i];
}
}
visited[min] = true;
min = findMin(v,N, dist, visited);
}
}
int findMin(int v,int N, int ** dist,bool * visited) {
int minimum = 65535; int tmp = -1;
for (int i = 0; i < N; i++) {
if (!visited[i]&&dist[v][i] < minimum) {
minimum = dist[v][i];
tmp = i;
}
}
return tmp;
}