【关键路径】HDU4109 Instrction Arrangement

本博客探讨了在计算机组织与架构课程中遇到的指令依赖问题,如WAR、WAW和RAW依赖。当两指令间的距离小于安全距离时,可能会导致冲突,即危害。为解决此问题,可以通过添加无用操作(气泡)来调整指令顺序,确保它们之间的距离大于安全距离。题目要求对多条指令进行重新排列,以最小化完成所有指令所需的时间。输入包含指令数、依赖关系及安全距离,输出是最小执行时间。通过拓扑排序和最长路算法求解。
摘要由CSDN通过智能技术生成

Ali has taken the Computer Organization and Architecture course this term. He learned that there may be dependence between instructions, like WAR (write after read), WAW, RAW.
If the distance between two instructions is less than the Safe Distance, it will result in hazard, which may cause wrong result. So we need to design special circuit to eliminate hazard. However the most simple way to solve this problem is to add bubbles (useless operation), which means wasting time to ensure that the distance between two instructions is not smaller than the Safe Distance.
The definition of the distance between two instructions is the difference between their beginning times.
Now we have many instructions, and we know the dependent relations and Safe Distances between instructions. We also have a very strong CPU with infinite number of cores, so you can run as many instructions as you want simultaneity, and the CPU is so fast that it just cost 1ns to finish any instruction.
Your job is to rearrange the instructions so that the CPU can finish all the instructions using minimum time.

Input

The input consists several testcases.
The first line has two integers N, M (N <= 1000, M <= 10000), means that there are N instructions and M dependent relations.
The following M lines, each contains three integers X, Y , Z, means the Safe Distance between X and Y is Z, and Y should run after X. The instructions are numbered from 0 to N - 1.
Output
Print one integer, the minimum time the CPU needs to run.

Sample Input

5 2
1 2 1
3 4 1

Sample Output

2

题意

给出N道工序和M个关系,每个工序启动需要1秒,M行关系三个参数x y z分别代表x执行z秒后方能执行y。

思路

求拓扑排序的同时更新最长路,其实和最短路差不多,只不过是反过来。具体细节见代码。

代码

#include <cstdio>
#include <vector>
#include <cstring>
#include <queue>
#include <algorithm>
#define MAX_N 1000
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值