ITKeyword,专注技术干货聚合推荐

注册 | 登录

hdu 4109 Instrction Arrangement(关键路径)

分享于 2015-08-18

推荐:HDU4109 Instrction Arrangement 拓扑排序 关键路径

Problem Description Ali has taken the Computer Organization and Architecture course this term. He learned that there may be dependence between instruc

2018阿里云全部产品优惠券(新购或升级都可以使用,强烈推荐)
领取地址https://promotion.aliyun.com/ntms/yunparter/invite.html

题目:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=25063

Instrction Arrangement Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u

Submit Status

Description

推荐:【关键路径】hdu 4109

#include <map>#include <set>#include <list>#include <queue>#include <deque>#include <stack>#include <string>#include <time.h>#include <cstdio>

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. 
         
关键路径:关键路径通常(但并非总是)是决定项目工期的进度活动序列。它是项目中最长的路径,即使很小浮动也可能直接影响整个项目的最早完成时间。本题就是应用关键路径的算法求出最短时间(最长的一条子路径)。有关关键路径的推荐博客: http://blog.csdn.net/pigli/article/details/5777048
百度上的例子:
这时从开始顶点到达完成顶点的所有路径都是关键路径。一个AOE网的关键路径可以不止一条,如图AOE网中有二条关键路径,(v1,v2,v5,v7,v9 ) 和 (v1,v2,v5,v8,v9 )它们的路径长度都是24。如图所示:

本题代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N=1005;
int indeg[N],finish[N],n,m;
struct node{
    int v,cost;
    struct node *next;
    node(){
        v=cost=0;
        next=NULL;
    }
}vnode[N];
void init(){
    memset(indeg,0,sizeof(indeg));
    memset(vnode,0,sizeof(vnode));
    for(int i=0;i<n;i++) finish[i]=1;
}
void create(){
    node *s;
    int a,b,c;
    for(int i=0;i<m;i++){
        scanf("%d %d %d",&a,&b,&c);
        indeg[b]++;
        s=new node();
        s->v=b;
        s->cost=c;
        s->next=vnode[a].next;
        vnode[a].next=s;
    }
}
void topo(){
    node *p;
    int v;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(indeg[j]==0){
                v=j;
                break;
            }
        }
        indeg[v]=-1;
        p=vnode[v].next;
        while(p){
            if(finish[p->v]<finish[v]+p->cost)
                finish[p->v]=finish[v]+p->cost;
            indeg[p->v]--;
            p=p->next;
        }
    }
}
int main()
{
    //freopen("cin.txt","r",stdin);
    int ans;
    while(cin>>n>>m){
        init();
        ans=0;
        create();
        topo();
        for(int i=0;i<n;i++)if(finish[i]>ans)ans=finish[i];
        printf("%d\n",ans);
    }
    return 0;
}


推荐:hdu 4109 Instrction Arrangement (topo+关键路径) (附简单的测试数据)

链接:http://acm.hdu.edu.cn/showproblem.phppid=4109 题解:cpu能够并行处理一些指令,且每次处理的时间为1s,但是给出限制条件a,b,c 就是指令a和指令b之间必

题目:http://acm.hust.edu.cn/vjudge/problem/viewProblem.actionid=25063 Instrction Arrangement Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit St

相关阅读排行


用户评论

游客

相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。