hdu4109拓扑Instrction Arrangement

来源:互联网 发布:孙宏斌的钱哪来的知乎 编辑:程序博客网 时间:2024/06/05 07:55
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;
}

原创粉丝点击