码迷,mamicode.com
首页 > 其他好文 > 详细

图论基础

时间:2018-08-03 21:20:23      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:should   nis   也有   ensure   nat   char   sizeof   关键路径   content   

1 关键路径

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.

InputThe 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. 
OutputPrint 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.
题意:有许多个任务,需要完成它们,有先后顺序,并且也有间隔时间,比如AB间隔时间为t,则t之后才能做B,对于做每个没有关联的任务,你可以同时完成它们,需1ns(时间),
题解:很容易就看出来这是个拓扑排序了,利用bfs拓扑排序,只有入度为0才进队,并且在入队时更新最大距离(关键路径就是求图上最长路),但有一个注意点,就是刚开始找入度为0的点,
你需要先给它们dis值置为1,因为完成它们需1ns,关键路径也可用spfa求最长路,权值取反,求最短路
 1 var
 2 head,next,a,rd,q,dis,w:array[0..50000]of longint;
 3 e,ans,i,j,x,y,z,n,m:longint;
 4 function max(x,y:longint):longint;
 5  begin
 6    if x>y then exit(x) else exit(y);
 7  end;
 8 procedure add(x,y,z:longint);
 9 begin
10  inc(e);a[e]:=y;next[e]:=head[x];head[x]:=e; w[i]:=z;
11 end;
12 procedure bfs;
13  var h,t,i,v:longint;
14  begin
15   h:=0;t:=0;
16   for i:=0 to n-1 do
17    begin
18     if rd[i]=0 then begin inc(t); q[t]:=i; rd[i]:=-1;dis[i]:=1; end;
19    end;
20    while h<t do
21     begin
22       inc(h);
23      i:=head[q[h]];
24      while i>0 do
25       begin
26         v:=a[i];
27         dis[v]:=max(dis[v],dis[q[h]]+w[i]);
28         dec(rd[v]);
29         if rd[v]=0 then begin inc(t); q[t]:=v; rd[v]:=-1;end;
30         i:=next[i];
31       end;
32     end;
33   //  for i:=0 to n-1 do write(dis[i], );
34  end;
35 begin
36 while not eof do
37 begin
38 fillchar(a,sizeof(a),0);
39 fillchar(next,sizeof(next),0);
40 fillchar(head,sizeof(head),0);
41 fillchar(rd,sizeof(rd),0);
42 fillchar(w,sizeof(w),0);
43 fillchar(dis,sizeof(dis),0);
44 fillchar(q,sizeof(q),0);
45 ans:=0; e:=0;
46  readln(n,m);
47  for i:=1 to m do
48   begin
49     readln(x,y,z);
50     add(x,y,z);
51     inc(rd[y]);
52   end;
53   bfs;
54   for i:=0 to n-1 do ans:=max(ans,dis[i]);
55   writeln(ans);
56 end;
57 end.

 

 

图论基础

标签:should   nis   也有   ensure   nat   char   sizeof   关键路径   content   

原文地址:https://www.cnblogs.com/brilliant107/p/9416249.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!