2019 百度校招 QA测试编程题 - 公共交通

# Parse data
n,m,k = [int(i) for i in input().strip().split(' ')]
Road = []
bRoad = []
for _ in range(m):
    Road.append([int(i) for i in input().strip().split(' ')])

for _ in range(k):
    bRoad.append([int(i) for i in input().strip().split(' ')])

# check connectivity
def check(Road):
    state = [i+1 for i in range(n)]
    for i in range(len(Road)):
        a = min(Road[i])
        b = max(Road[i])
        for j in range(n):
            if state[j] == state[b-1]:
                state[j] = state[a-1]
    return set(state) == set([1])

def slove(Road,bRoad,i):
    if i == len(bRoad) and check(Road):
        return 0
    elif i==len(bRoad):
        return -1
    r = bRoad[i][:2]
    Road.append(r)
    branch_alpha = bRoad[i][2]+slove(Road,bRoad,i+1)
    Road.remove(r)
    branch_beta = slove(Road,bRoad,i+1)

    return min([i for i in [branch_alpha,branch_beta] if i !=-1])

print(slove(Road,bRoad,0))

简洁是挺简洁的,不过超时的比较严重,AC 9%,需要优化

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值