You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
2.5 KiB
69 lines
2.5 KiB
#
|
|
# wukun 华为云笔试
|
|
|
|
# 软件安装工具
|
|
# 有一个比较复杂的软件系统需要部署到客户提供的服务器上。该软件系统的安装过程非常繁琐,为了降低操作成本,需要开发一个工具实现自动化部署。软件的安装过程可以分成若干个小步骤,某些步骤间存在依赖关系,被依赖的步骤必须先执行完,才能执行后续的安装步骤。满足依赖条件的多个步骤可以并行执行。请你开发一个调度程序,以最短的时间完成软件的部署。
|
|
# 输入:
|
|
# 第一行:总步骤数 N(0<N<=10000)
|
|
# 第二行:N个以空格分隔的整数,代表每个步骤所需的时间。该行所有整数之和不大于int32
|
|
# 第三行开始的N行:表示每个步骤所依赖的其它步骤的编号(编号从1开始,行号减2表示步骤的编号),如果依赖多个步骤,用空格分隔。-1表示无依赖测试用例确保各个安装步骤不会出现循环依赖。
|
|
# 输出:
|
|
# 1个数字,代表最短执行时间。
|
|
|
|
# 样例1
|
|
# 输入:4
|
|
# 6 2 1 2
|
|
# -1
|
|
# 1
|
|
# 3
|
|
# 输出:9
|
|
|
|
# 样例2:
|
|
# 输入:4
|
|
# 1 2 3 4
|
|
# 2 3
|
|
# 3
|
|
# -1
|
|
# 1
|
|
# 输出:10
|
|
|
|
from collections import defaultdict, deque
|
|
def func(N, step_times, dependencies):
|
|
# 构建图和入度计数
|
|
graph = defaultdict(list)
|
|
in_degree = [0] * N
|
|
time_to_complete = [0] * N
|
|
# 填充依赖关系
|
|
for i in range(N):
|
|
for dep in dependencies[i]:
|
|
if dep != -1:
|
|
graph[dep - 1].append(i)
|
|
in_degree[i] += 1
|
|
# 使用队列处理入度为0的步骤
|
|
queue = deque()
|
|
for i in range(N):
|
|
if in_degree[i] == 0:
|
|
queue.append(i)
|
|
time_to_complete[i] = step_times[i]
|
|
# 进行拓扑排序
|
|
while queue:
|
|
current = queue.popleft()
|
|
current_completion_time = time_to_complete[current]
|
|
for neighbor in graph[current]:
|
|
in_degree[neighbor] -= 1
|
|
time_to_complete[neighbor] = max(time_to_complete[neighbor], current_completion_time + step_times[neighbor])
|
|
if in_degree[neighbor] == 0:
|
|
queue.append(neighbor)
|
|
return max(time_to_complete)
|
|
|
|
if __name__ == "__main__":
|
|
# 输入示例
|
|
N = int(input())
|
|
step_times = list(map(int, input().split()))
|
|
print(step_times)
|
|
dependencies = []
|
|
for _ in range(N):
|
|
deps = list(map(int, input().split()))
|
|
dependencies.append(deps)
|
|
# 输出最短执行时间
|
|
print(func(N, step_times, dependencies))
|