# hdu 4109 Instrction Arrangement(关键路径)

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

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

Description

#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.
```

```#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;
}```

×
• 登录
• 注册

×

### 请激活账号

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

您的注册邮箱： 修改

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