hdu4109拓扑Instrction Arrangement

Problem 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

如果要完成y事件,则要先完成x事件,多个事件可同时进行;
样例:
先完成 0 1 3再完成2 4,时间为2
拓扑排序,找关键路径;

代码:
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int a[1010][1010],dis[1010],topo[1010],in[1010];
int t;
int ma(int x,int y)
{
    return x>y?x:y;
}
void bfs(int n)
{
    int x=0;
    t=0;
    while(x<n)
    {
        for(int i=0;i<n;i++)
        {
            if(in[i]==0)
            {
                x++;
                in[i]--;
                int flag=0;//判断是否排序到最后
                for(int j=0;j<n;j++)
                {
                    if(a[i][j]!=0)
                    {
                        in[j]--;
                        flag=1;
                        dis[j]=ma(dis[j],dis[i]+a[i][j]);
                    }
                }
                if(flag==0)
                topo[t++]=dis[i]+1;//dis[i]为距离,而题目要求的是点的个数
                break;
            }
        }
    }
}
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        memset(dis,0,sizeof(dis));
        memset(topo,0,sizeof(topo));
        memset(a,0,sizeof(a));
        memset(in,0,sizeof(in));
        int x,y,z;
        while(m--)
        {
            scanf("%d%d%d",&x,&y,&z);
            if(a[x][y]==0)
            {
                a[x][y]=z;
                in[y]++;
            }
        }
        bfs(n);
        int s=0;
        for(int i=0;i<t;i++)
            s=ma(topo[i],s);
        printf("%d\n",s);
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值