# 题目描述

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.

# 输入

Each input file contains one test case. For each case, the first line contains 4 positive integers: N ( ≤ 1 0 3 ≤10^3 ), the total number of houses; M (≤10), the total number of the candidate locations for the gas stations; K ( ≤ 1 0 4 ≤10^4 ), 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.

# 输出

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<cstdio>
#include<iostream>
#include<stdlib.h>
#include<queue>
#include<vector>
#include<algorithm>
#include<map>
#include<string>
using namespace std;
#define INF 1147483647
/*typedef struct node {
int w;
int v;
}Bian;
vector<Bian> mp[1010];*/
map<string, int> m;
int graph[1100][1110];
int dis[1100];
int vis[1100] = {

0 };
int N, M, num, Max;
void dj(int start)
{

for (int i = 1; i <= N+M; i++)
{

dis[i] = graph[start][i];
vis[i] = 0;
}
dis[start] = 0;
vis[start] = 1;
for (int i = 1; i <= N + M; i++)
{

int ans = INF, k;
for (int j = 1; j <= N + M; j++)
{

if (vis[j] == 0)
{

if (dis[j] < ans)
{

ans = dis[j];
k = j;
}
}
}
if (ans == INF)
{

break;
}
vis[k] = 1;
for (int j = 1; j <= N + M; j++)
{

if (!vis[j] && dis[j] > dis[k] + graph[k][j])
{

dis[j] = dis[k] + graph[k][j];
}
}
}
}
int main()
{

cin >> N >> M >> num >> Max;
for (int i = 1; i <= N; i++)
{

string str;
str += to_string(i);
m[str] = i;
}
for (int i = N + 1; i <= M + N; i++)
{

string str;
str = str + "G";
str = str + to_string(i-N);
m[str] = i;
}
for (int i = 1; i <= N + M; i++)
{

for (int j = 1; j <= N + M; j++)
{

graph[i][j] = INF;
}
}
for (int i = 0; i < num; i++)
{

string a, b;
int w;
cin >> a >> b >> w;
if (graph[m[a]][m[b]] > w)
{

graph[m[a]][m[b]] = w;
graph[m[b]][m[a]] = w;
}

}
int ans_g = 0;
int ans_m = 0;
int ans_a = INF;
for (int i = N + 1; i <= N + M; i++)
{

for (int i = 1; i <= N + M; i++)
{

dis[i] = INF;
}
dj(i);
int all = 0;
int mmin = INF;
for (int i = 1; i <= N; i++)
{

all += dis[i];
if (dis[i] > Max)
{

mmin = INF;
break;
}
if (mmin > dis[i])
{

mmin = dis[i];
}
}
if (mmin == INF)
{

continue;
}
else
{

if (mmin > ans_m)
{

ans_m = mmin;
ans_a = all;
ans_g = i;
}
else if (mmin == ans_m && all < ans_a)
{

ans_a = all;
ans_g = i;
}
}
}
if (ans_g == 0)
{

cout << "No Solution" << endl;
return 0;
}
else
{

cout << "G" << ans_g-N << endl;
double t1 = ans_m;
double t2 = ans_a * 1.0 / N;
printf("%.1lf %.1lf\n", t1, t2);
}
}


### Guess you like

Origin blog.csdn.net/qq_45478482/article/details/120071147
Recommended
Ranking
Daily