首页 > 试题广场 >

Gas Station (30)

[编程题]Gas Station (30)
  • 热度指数:5331 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
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”.
示例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

输出

G1
2.0 3.3
提供一个刚写好的Python版本,牛客Accepted,PAT除一个点超时外也可全部通过

from math import inf
from sys import stdin
from statistics import mean


resident, gas, road, range_max = map(int, input().split())
spot = resident + gas
graph = [ [inf] * (spot+1) for _ in range(spot+1) ]
result, far_max, avg_min = 0, -inf, 0

for line in stdin.readlines():
    v1, v2, length = line.split()
    length = int(length)
    v1 = int(v1[1:]) + resident if v1[0] == 'G' else int(v1)
    v2 = int(v2[1:]) + resident if v2[0] == 'G' else int(v2)
    if v1 == v2: length = 0
    if graph[v1][v2] != inf:
        length = min(length, graph[v1][v2])
    graph[v1][v2] = graph[v2][v1] = int(length)

def Dijkstra(source):
    dist, collected = [inf] * (spot+1), [False] * (spot+1)
    dist[source] = 0
    while True:
        v, dist_min = -1, inf
        for i in range(1, spot+1):
            if not collected[i] and dist[i] < dist_min:
                v = i
                dist_min = dist[i]
        if v == -1: break
        collected[v] = True

        for w in range(1, spot+1):
            if graph[v][w] != inf and not collected[w]:
                if dist[v] + graph[v][w] < dist[w]:
                    dist[w] = dist[v] + graph[v][w]
    return dist

for i in range(resident+1, spot+1):
    dist = Dijkstra(i)
    dist_min = inf
    for r in range(1, resident+1):
        if dist[r] > range_max:
            dist_min = -1
            break
        if dist[r] < dist_min:
            dist_min = dist[r]
    if dist_min == -1: continue
    dist_avg = mean( dist[1:resident+1] )
    if dist_min > far_max or (dist_min == far_max and dist_avg < avg_min):
        result = i - resident
        far_max = dist_min
        avg_min = dist_avg

if result:
    print('G{}\n{:.1f} {:.1f}'.format( result, far_max, avg_min ))
else:
    print('No Solution')

注:牛客一个测试点有误,输入225条边的数据,而原数据为255,故我直接用sys.stdin.readlines()读取数据行,规避该问题。


编辑于 2021-03-04 22:27:00 回复(0)

问题信息

难度:
1条回答 9220浏览

热门推荐

通过挑战的用户

Gas Station (30)