HDU 4109 Instrction Arrangement(拓扑排序+bfs)

55 篇文章 0 订阅
18 篇文章 0 订阅

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.

        
 

题解:

先说题意:

给出一堆任务,输入三个数字x,y,z,表示完成y任务之前要完成x任务,间隔为z,每次处理要时间1,问处理完全部最少要多少时间

然后一看就是一个拓扑排序。。。然后就用dfs。。爆炸了,然后就用bfs写,WA。。后来调了半天没发现错,加个while(scanf(。。。)!=EOF)就ac了。。原来题目说了是多组数据

拓扑排序就是每次找入度为0的点,然后遍历连通的数,遍历到通往的数那个数的入度就--,然后一轮过后再找度数为0的数。。一直到结束,以前写拓扑排序可以用dfs的,这题数据有点大,只好用bfs了,这题还有点dp的意味,后来写完又觉得像求最短路径里的spfa算法的翻版---最长路径,详解看代码

#include<algorithm>
#include<iostream>
#include<cstring>
#include<stdio.h>
#include<math.h>
#include<string>
#include<stdio.h>
#include<queue>
#include<stack>
#include<map>
#include<deque>
using namespace std;
int p[1005][1005];//存图,两点间距离
int a[1005];//存入度
int num[1005];//存点通向多少边
int to[1005][1005];//存i点通向的第j个点是哪个
int maxx;
int add[1005];//存最长路径
queue<int>q;
int main()
{
    int i,j,k,n,m,x,y,z,tot,time;
    int now;
    while(scanf("%d%d",&n,&m)!=EOF)//不加会WA
    {
        for(i=0;i<n;i++)
        {
            a[i]=0;
            num[i]=0;
            add[i]=0;//初始化
        }
        for(i=0;i<m;i++)
        {
            scanf("%d%d%d",&x,&y,&z);//存各种信息
            p[x][y]=z;
            a[y]++;
            to[x][num[x]]=y;
            num[x]++;
        }
        tot=0;
        time=0;
        for(i=0;i<n;i++)
        {
            if(a[i]==0)//找入度为0的点
            {
                add[i]=1;
                q.push(i);
            }
        }
        while(!q.empty())
        {
            now=q.front();
            for(i=0;i<num[now];i++)//遍历该点通往的点
            {
                if(add[now]+p[now][to[now][i]]>add[to[now][i]])//修改最存路径条件
                {
                    add[to[now][i]]=add[now]+p[now][to[now][i]];
                }
                a[to[now][i]]--;
                if(a[to[now][i]]==0)//入度为0了就加入
                {
                    q.push(to[now][i]);
                }
            }
            q.pop();
        }
        for(i=0;i<n;i++)//遍历一遍求最长
        {
            if(time<add[i])
                time=add[i];
        }
        printf("%d\n",time);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值