题目:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=25063
Time Limit: 1000MS | Memory Limit: 32768KB | 64bit IO Format: %I64d & %I64u |
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.
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.
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.
百度上的例子:
这时从开始顶点到达完成顶点的所有路径都是关键路径。一个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;
}