http://acm.hdu.edu.cn/showproblem.php?pid=4109
Instrction Arrangement
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1320 Accepted Submission(s): 558
Problem 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.
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
Sample Output
#include <iostream>
#include <string.h>
#include <queue>
#include <stdio.h>
using namespace std;
const int M=1005;
struct Node
{
int v,w,next;
}edge[M*10];
int n,m,cnt;
int ind[M],ans[M*10],frist[M*10];
void inti()
{
memset(ans,0,sizeof(ans));
memset(ind,0,sizeof(ind));
memset(frist,-1,sizeof(frist));
}
int max(int a,int b)
{
if(a>b)
return a;
else
return b;
}
void uino(int u,int v,int w)///邻接表
{
edge[cnt].v=v;
edge[cnt].w=w;
edge[cnt].next=frist[u];
frist[u]=cnt++;
}
void read_case()
{
int a,b,w;
for(int i=0;i<m;i++ )
{
scanf("%d%d%d",&a,&b,&w);
uino(a,b,w);
ind[b]++;
}
}
void solve()
{
queue<int>q;
for(int i=0;i<n;i++)
{
if(!ind[i])
{
q.push(i);
ans[i]=1;
}
}
while(!q.empty())
{
int t=q.front();
q.pop();
for(int e=frist[t];e!=-1;e=edge[e].next)///!!!
{
int v=edge[e].v;
int ww=edge[e].w;
if(--ind[v]==0)
q.push(v);
if(ans[v]<ans[t]+ww)
ans[v]=ans[t]+ww;///!!!
}
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
inti();
cnt=0;
read_case();
solve();
int Max=0;
for(int i=0;i<n;i++)
Max=max(ans[i],Max);
printf("%d\n",Max);
}
return 0;
}