用Python做50道ACM之《Fast Food 》

6.Fast Food

http://acm.fzu.edu.cn/problem.php?pid=1005

重叠性未消除,本题需进一步完善

import sys

def sum_min_distance(restaurants_locations,depot_num,depots_group_min_distance):
    if len(restaurants_locations)==1 or len(restaurants_locations)==depot_num:
        depots_group_min_distance[str(restaurants_locations)]=0
        return depots_group_min_distance[str(restaurants_locations)]
    for mid in range(1,len(restaurants_locations)):
        k=depot_num
        if k==1:
            all_distance=[]
            for i in range(0,len(restaurants_locations)):
                depot=restaurants_locations[i]
                all_distance.append(sum([abs(depot-x) for x in restaurants_locations]))
            depots_group_min_distance[str(restaurants_locations)]=min(all_distance)
            return depots_group_min_distance[str(restaurants_locations)]
        else:
            left_group=restaurants_locations[:mid]
            right_group=restaurants_locations[mid:]
            for i in range(1,k):
                if i>len(left_group) or k-i>len(right_group):
                    continue
                left_min_distance=sum_min_distance(left_group,i-0,depots_group_min_distance)
                right_min_distance=sum_min_distance(right_group,k-i,depots_group_min_distance)
                min_distance=left_min_distance+right_min_distance
                if not str(restaurants_locations) in depots_group_min_distance.keys():
                    depots_group_min_distance[str(restaurants_locations)]=min_distance
                else:
                    if min_distance<depots_group_min_distance[str(restaurants_locations)]:
                        depots_group_min_distance[str(restaurants_locations)]=min_distance
    return depots_group_min_distance[str(restaurants_locations)]


restaurant_num=0 # 每个用例剩余行数
chain=0
while True:
    read_in=sys.stdin.readline()
    if not read_in:
        break
    if restaurant_num==0:
        chain+=1
        restaurant_num,depot_num=tuple([int(x) for x in read_in.rstrip().split(' ')])
        if restaurant_num==0 and depot_num==0: # 结束标志
            break
        restaurants_locations=[] # 餐馆位置表
        depots_group_min_distance={} # 一张存放各种仓库配送组的最小配送总距离的表
    else:
        restaurants_locations.append(int(read_in.rstrip()))
        restaurant_num-=1
        if restaurant_num==0: # 当用例下的所有行信息被读取完毕
            if restaurant_num==depot_num: # 平凡情况
                print('Chain '+int(chain))
                print('Total distance sum = 0')
                print()
            else: # 非平凡情况
            distance=sum_min_distance(restaurants_locations,depot_num,depots_group_min_distance)
                print('Chain '+int(chain))
                print('Total distance sum = %d'%distance)
                print()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值