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.
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.
5 2 1 2 1 3 4 1
2HintIn 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>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
struct Node
{
int x;
int t;
};
vector<Node> a[1100];
int n,m;
int in[1100],end[1100];
queue<int> q;
//找关键路径长度
int main()
{
while(scanf("%d%d",&n,&m)==2)
{
for(int i=1;i<=n;i++) a[i].clear(),in[i]=0;
while(m--)
{
int u,t,w;scanf("%d%d%d",&u,&t,&w);u++,t++;
Node cnt;cnt.x=t,cnt.t=w;
a[u].push_back(cnt);
in[t]++;
}
while(!q.empty()) q.pop();
memset(end,0,sizeof(end));
for(int i=1;i<=n;i++)
{
if(in[i]==0)
{
end[i]=1;
q.push(i);
}
}
int ans=1;
while(!q.empty())
{
int u=q.front();q.pop();
for(int i=0;i<a[u].size();i++)
{
int x=a[u][i].x,t=a[u][i].t;
end[x]=max(end[x],end[u]+t);
in[x]--;
if(in[x]==0)
{
q.push(x);
}
}
}
for(int i=1;i<=n;i++) ans=max(ans,end[i]);
printf("%d\n",ans);
}
return 0;
}

这是一个C++程序,用于求解HDU 4109问题的关键路径长度。程序通过读取输入的节点数和边的权重,使用队列进行广度优先搜索,更新每个节点的结束时间并找到关键路径的最大长度。
358

被折叠的 条评论
为什么被折叠?



