【hdoj 4109】Instrction Arrangement

1 篇文章 0 订阅
 Instrction Arrangement
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit Status

Description
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


题意理解;  cpu执行一个操作需要1ns的时间,有些操作必须在某个操作之后执行,且两个操作之间有安全距离(两个操作开始的时间差),求完成所有操作所需要的最短时间。

思路:topo排序,用动态数组存储每个操作完成的最短时间,最后输出,完成每个操作所需时间的最大值。

code:

#include <iostream>
#include <string.h>
#include <algorithm>
#include <cstdio>
#include <queue>
using namespace std;
int N,M;
int ind[1005],sta[1005];
long long pre[1005][1005],dp[1005];
int main()
{
    int x,y,z;
    long long  mx;
    while(scanf("%d%d",&N,&M)!=EOF)
    {
        int i;
        int top=0;
        memset(pre, 0, sizeof(pre));  
        memset(dp, 0, sizeof(dp));
        memset(ind, 0, sizeof(ind));
        memset(sta, 0, sizeof(sta));
        for(int i=0;i<M;i++)
        {
            scanf("%d%d%d",&x,&y,&z);
            pre[y][x]=z;     //记录y操作与X操作执行的间隔时间
            ind[y]++;    //入度++
        }
        for(int i=0;i<N;i++)
        {
            if(ind[i]==0)
            {
                dp[i] = 1;   //入度为0,dp[]数组记录时间为1;
                sta[top++] = i;   //将入度为0 的操作号存入sta数组。
            }
        }
        while(top != 0)
        {
            x = sta[--top];   //读取sta中的操作号
            for(int i = 0; i < N; i++)
            {
                if(pre[i][x] != 0)  
                {
                    dp[i] = dp[i] > pre[i][x] + dp[x] ? dp[i] : pre[i][x] + dp[x];<span style="font-family: Arial, Helvetica, sans-serif;">//更新x之后的每个操作的最短时间</span>
                    ind[i]--;   //更新完之后,将该操作的入度--
                    pre[i][x] = 0;  //i和X的关系取消
                    if(ind[i] == 0)   //如果此时操作i的入度为0,将i放入sta数组
                        sta[top++] = i;
                }
            }
        }
        for(mx = 0,  i = 0; i < N; i++)   
        {
            mx = mx > dp[i] ? mx : dp[i];
        }
        printf("%I64d\n", mx);
    }
    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值