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 (<= 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.
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”.
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
G1
2.0 3.3
2 1 2 10
1 G1 9
2 G1 20
No Solution
规划加油站的建设点!有N的居民房,M个加油站待建点,K条边,要求所有居民房距离加油站不得超过DS
要求加油站在不超过DS的情况下距离居民房越远越好。让你找出距离最远的加油站
如果这样最远的加油站存在多个,找出距离居民房平均距离最近的一个
如果仍然存在多个,找出编号最小的一个
注意,加油站点的编号由G1, G2, G3…表示,居民房编号由1,2,3…表示
输入第一行给出N M K DS
接着给出K行,分别表示 顶点1 顶点2 距离(边权)
输出要求输出加油站点编号,另起一行输出加油站距居民房最短距离 和 加油站距离居民房的平均距离,以空格隔开。
这道题可以使用Dijkstra来做,枚举加油站,对于每个加油站为起点使用Dijkstra算法计算居民房与之最短距离,再使用一个函数算出这个距离即可
因为要使用多次Dijkstra算法,所以要特别注意初始化操作。
代码如下:
//1072 Gas Station (30分)
#include
#include
using namespace std;
int const MAX = 1020;
int const INF = INT_MAX;
int G[MAX][MAX];
int dis[MAX];
bool vis[MAX];
int N, M, K, DS;
void Dijkstra(int s)
{
fill(dis, dis + MAX, INF);
dis[s] = 0;
for (int i = 1; i <= N + M; i++)
{
int u = -1, min = INF;
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 v = 1; v <= N + M; v++)
{
if (vis[v] == false && G[u][v] != INF && dis[u] + G[u][v] < dis[v])
{
dis[v] = dis[u] + G[u][v];
}
}
}
}
int main()
{
fill(G[0], G[0] + MAX * MAX, INF);
fill(dis, dis + MAX, INF);
cin >> N >> M >> K >> DS;
for (int i = 0; i < K; i++)
{
string cs1, cs2;
int c1, c2, w;
cin >> cs1 >> cs2 >> w;
if (cs1[0] == 'G')
{
c1 = stoi(cs1.substr(1))+N;
}
else
{
c1 = stoi(cs1);
}
if (cs2[0] == 'G')
{
c2 = stoi(cs2.substr(1))+N;
}
else
{
c2 = stoi(cs2);
}
G[c1][c2] = G[c2][c1] = w;
}
int ansid = -1;
double ansdis = -1, ansaver = INF;
for (int i = N + 1; i <= N + M; i++)
{
double mindis = INF, aver = 0;
fill(vis, vis + MAX, false);
Dijkstra(i);
for (int j = 1; j <= N; j++)
{
if (dis[j] > DS)
{
mindis = -1;
break;
}
if (dis[j] < mindis)
{
mindis = dis[j];
}
aver += 1.0 * dis[j];
}
if (mindis == -1) //beyond the range
continue;
aver = aver / N;
if (mindis > ansdis)//加油站在不超过DS的情况下距离居民房越远越好
{
ansid = i;
ansdis = mindis;
ansaver = aver;
}
else if (mindis == ansdis && aver < ansaver)
{
ansid = i;
ansaver = aver;
}
}
if (ansid == -1)
printf("No Solution");
else
printf("G%d\n%.1f %.1f", ansid - N, ansdis, ansaver);
return 0;
}