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
Hint
In the 1st ns, instruction 0, 1 and 3 are executed; In the 2nd ns, instruction 2 and 4 are executed. So the answer should be 2.
 

题意:
可以理解为有m个工程和N条生产线,每个生产线完成都需要一定的时间,每个工程开始的前提是连接它的生产线全部完成。可以多条生产线同时运作,求所有工程完成的最早时间(工程开始立即完成)。

思路:
通过拓扑排序每次找入度为0的点,依次计算所有工程的结束时间,得出的最大时间 即为最早时间
这里用邻接表存每个点联系的所有的边。
此题也可以用传统拓扑排序解法  但时间和空间(- -)要得多些


#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
struct node
{
    int to,value;
};
vector<node>next[1005];      //向量+结构体 邻接表
int in_degree[1005];          //节点的入度
int finish[1005];            //节点的开始时间
int m,n;
void topsort()
{
    queue<int>q;
    int i,j;
    int now;
    for(i=0;i<m;i++)
        if(in_degree[i]==0)
            {
                finish[i]=1;       //题意
                q.push(i);
            }
    while(!q.empty())
    {
        now=q.front();
      //  cout<<now<<endl;
        q.pop();
        for(i=0;i<next[now].size();i++) 
        {
            int b=next[now][i].to;
            int v=next[now][i].value;
            finish[b]=max(finish[b],finish[now]+v);     //某个工程的开始时间
            in_degree[b]--;
            if(in_degree[b]==0)
                q.push(b);
        }
    }
    return;
}
int main()
{
    int i,j;
    while(~scanf("%d%d",&m,&n))
    {
        memset(finish,0,sizeof(finish));
        memset(in_degree,0,sizeof(in_degree));
        for(i=0; i<=m; i++)
            next[i].clear();
        int a,b,c;
        node d;
        while(n--)
        {
            scanf("%d%d%d",&a,&b,&c);
            d.to=b;
            d.value=c;
            next[a].push_back(d);
            in_degree[b]++;
        }
        topsort();
        int ans=0;
        for(i=0;i<=m;i++)
            if(finish[i]>ans)
                ans=finish[i];
            cout<<ans<<endl;
    }
    return 0;
}


拓扑排序解法:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int map[1005][1005];
int finish[1005];
int indegree[1005];
int m;
void topsort()
{
    int i,j,loc;
    for(i=0; i<m; i++)
    {
        for(j=0; j<m; j++)
            if(!indegree[j])
                loc=j;
        indegree[loc]=-1;
        for(j=0; j<m; j++)
        {
            if(map[loc][j])
            {
                finish[j]=max(finish[j],finish[loc]+map[loc][j]);
                indegree[j]--;
            }
        }
    }
    return;
}
int main()
{
    int a,b,c,n,i,j,ans;
    while(~scanf("%d%d",&m,&n))
    {
        ans=0;
        memset(map,0,sizeof(map));
        memset(indegree,0,sizeof(indegree));
        for(i=0; i<m; i++)
            finish[i]=1;
        while(n--)
        {
            scanf("%d%d%d",&a,&b,&c);
            if(map[a][b]==0)
                map[a][b]=c;
            else if(c>map[a][b])
                map[a][b]=c;
        }
        for(i=0; i<m; i++)
            for(j=0; j<m; j++)
                if(map[i][j]!=0)
                    indegree[j]++;
        topsort();
        for(i=0; i<m; i++)
            if(finish[i]>ans)
                ans=finish[i];
        printf("%d\n",ans);
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值