Stop the Hollyweb! No DRM in HTML5.
随笔 - 77  文章 - 0  评论 - 7  阅读 - 66872

POJ 3255 dijkstra次短路

不说什么了,看代码吧。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
program p3255;
 
Type
 rec=record
   e,w,next:longint;
 end;
 
Var
 n,m,i,j,k,nowmin,minj,mink,u,top,st,ed,ww:longint;
 y:rec;
 a:array[0..200002] of rec;
 b:array[0..10002] of longint;
 v:array[1..2,0..10002] of boolean;
 d:array[1..2,0..10002] of longint;
 
Procedure add(st,ed,ww:longint);inline;
  begin
  inc(top);
  with a[top] do
    begin
    e:=ed;
    w:=ww;
    next:=b[st];
  end;
  b[st]:=top;
end;
 
  begin
  readln(n,m);
  top:=0;
  for i:=1 to m do
    begin
    readln(st,ed,ww);
    add(st,ed,ww);
    add(ed,st,ww);
  end;
  fillchar(v,sizeof(v),false);
  fillchar(d,sizeof(d),$7f);
  d[1,1]:=0;
  for i:=1 to 2*n do
    begin
    nowmin:=maxlongint;
    for j:=1 to n do
      for k:=1 to 2 do
        if not v[k,j] then
          if d[k,j]<nowmin then
            begin
            nowmin:=d[k,j];
            minj:=j;mink:=k;
          end;
 
    if nowmin=maxlongint then break;
    v[mink,minj]:=true;
    u:=b[minj];
    while u<>0 do
      begin
      y:=a[u];
      u:=y.next;
      if nowmin+y.w<d[1,y.e] then
        begin
        d[2,y.e]:=d[1,y.e];
        d[1,y.e]:=nowmin+y.w;
      end else
      if nowmin+y.w<d[2,y.e] then
        d[2,y.e]:=nowmin+y.w;
    end;
  end;
  writeln(d[2,n]);
  readln;
end.

 

posted on   灰天飞雁  阅读(210)  评论(0编辑  收藏  举报
< 2013年5月 >
28 29 30 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31 1
2 3 4 5 6 7 8

填写您的邮件地址,订阅我们的精彩内容:  点击这里给我发消息

添加到收藏夹

点击右上角即可分享
微信分享提示