hdu 4109

来源:互联网 发布:出租房软件 编辑:程序博客网 时间:2024/06/08 20:17
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 21 2 13 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.
#include <iostream>#include <stdlib.h>#include <string.h>using namespace std;int into[1010],ee[1010],n,m;//into表示入度,ee表示从最早完成该任务的时间,ee初始化为1struct link{ int v; int cost; struct link *next;};struct node{ struct link *next;}vode[1010];void creat_link(){   int a,b,c;  struct link *s;  for(int i=0;i<=n-1;i++)  {   vode[i].next=NULL;  }  for(int i=0;i<=m-1;i++)  {   cin>>a>>b>>c;   into[b]++;   s=(struct link*)malloc(sizeof(struct link));   s->v=b;   s->cost=c;   s->next=vode[a].next;   vode[a].next=s;  }}void topsort()//边拓扑,边求最长路径{ int v; struct link *p; for(int i=0;i<=n-1;i++) {  for(int j=0;j<=n-1;j++)  {   if(into[j]==0)   {    v=j;    break;   }  }  into[v]=-1;  p=vode[v].next;  while(p!=NULL)  {   if(ee[p->v]<ee[v]+p->cost)//如果权值和变大则替换    ee[p->v]=ee[v]+p->cost;   into[p->v]--;   p=p->next;  } }}int main(){    int ans;    while(cin>>n>>m)    {     memset(into,0,sizeof(into));     for(int i=0;i<=n-1;i++)     {      ee[i]=1;     }     creat_link();     ans=0;     topsort();     for(int i=0;i<=n-1;i++)     {       if(ee[i]>ans)        ans=ee[i];     }    cout << ans << endl;    }    return 0;}

0 0
原创粉丝点击