[hdu 4109] (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): 1281 Accepted Submission(s): 534
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
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.
题目分析:
此题属于关键路径问题,个人理解即为短板问题,也可以这样理解:一群人都要从A地到达B地,但是有的人乘火车,有的人坐船,有的人乘飞机,对于这一群人,他们能完成这项任务的时间即为最晚到达的人所花费的时间;
此题是要求计算花费的最长时间,即在拓扑排序中求各个点到起点的最长路径即可,代码实现如下:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <queue>
using namespace std;
const int MAXN=1010;
const int MAXM=10010;
int n,m;
int ip;
int head[MAXM];
int indegree[MAXM],outdegree[MAXM];
int Ee[MAXM];
int max(int a,int b)
{
return a>b? a:b;
}
struct note
{
int to;
int w;
int next;
} edge[MAXM];
void add(int u,int v,int w)
{
edge[ip].to=v;
edge[ip].w=w;
edge[ip].next=head[u];
head[u]=ip++;
}
void toposort()
{
queue<int>q;
for(int i=0; i<n; i++)
{
if(indegree[i]==0)
{
q.push(i);
Ee[i]=1;
}
}
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u]; i!=-1; i=edge[i].next)
{
int v=edge[i].to;
int w=edge[i].w;
if(--indegree[v]==0) q.push(v);
if(Ee[v] < Ee[u]+w)//关键
{
Ee[v] = Ee[u] + w;
}
}
}
}
int main()
{
int u,v,w;
while(scanf("%d%d",&n,&m)!=EOF)
{
int ans=0;
ip=0;
memset(head,-1,sizeof(head));
memset(indegree,0,sizeof(indegree));
memset(outdegree,0,sizeof(outdegree));
memset(Ee,0,sizeof(Ee));
while(m--)
{
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
indegree[v]++;
}
toposort();
for(int i=0; i<n; i++)
{
ans=max(ans,Ee[i]);//寻找最长路径
}
printf("%d\n",ans);
}
return 0;
}