poj3159 Candies 2012-09-07

http://poj.org/problem?id=3159 

 

差分约束系统

(spfa+ 队列)会TLE,(spfa+stack)可以过,不过有点奇葩的是建边时 输入 (a,b,c) 如果连边 w(b,a)=c 然后 做 spfa(n),输出dis[1]会超时。反过来 连边 w(a,b)=c,然后做spfa(1),输出dis[n] 就484Ms过了。数据比较坑 爹吧。

 

  1 Program poj3159;
  2 
  3 type cord=record
  4 
  5              ne,da,po:longint;
  6 
  7           end;
  8 
  9 
 10 var n,m,le,tot:longint;
 11 
 12     head,dis:array[1..30000]of longint;
 13 
 14     h,v:array[1..200000]of longint;
 15 
 16     f:array[1..30000]of boolean;
 17 
 18     link:array[1..200000]of cord;
 19 
 20 
 21   Procedure add(x,y,z:longint);
 22 
 23    begin
 24 
 25        inc(le);
 26 
 27        with link[le] do
 28 
 29          begin
 30 
 31             po:=y;
 32 
 33             da:=z;
 34 
 35             ne:=head[x];
 36 
 37          end;
 38 
 39        head[x]:=le;
 40 
 41    end;
 42 
 43 
 44   Procedure init;
 45 
 46   var i,j,k,l:longint;
 47 
 48    begin
 49 
 50       readln(n,m);
 51 
 52       for i:=1 to m do
 53 
 54        begin
 55 
 56           readln(j,k,l);
 57 
 58           add(j,k,l);
 59 
 60        end;
 61 
 62    end;
 63 
 64 
 65   Procedure spfa(x:longint);
 66 
 67   var i,j,k,t:longint;
 68 
 69    begin
 70 
 71       for i:=1 to n do dis[i]:=maxlongint div 3;
 72 
 73       dis[x]:=0;
 74 
 75       h[1]:=x;
 76 
 77       f[x]:=true;
 78 
 79       t:=1;
 80 
 81       repeat
 82 
 83          k:=h[t];
 84 
 85          dec(t);
 86 
 87          f[k]:=false;
 88 
 89          i:=head[k];
 90 
 91          while i<>0 do
 92 
 93           begin
 94 
 95              if dis[k]+link[i].da<dis[link[i].po] then
 96 
 97                  begin
 98 
 99                      dis[link[i].po]:=dis[k]+link[i].da;
100 
101                      if f[link[i].po]=false then
102 
103                         begin
104 
105                             f[link[i].po]:=true;
106 
107                             inc(t);
108 
109                             h[t]:=link[i].po;
110 
111                         end;
112 
113                  end;
114 
115              i:=link[i].ne;
116 
117           end;
118 
119 
120       until  t=0;
121 
122    end;
123 
124 
125   Procedure main;
126 
127   var i,j:longint;
128 
129    begin
130 
131       spfa(1);
132 
133       writeln(dis[n]);
134 
135    end;
136 
137 
138 Begin
139 
140     
141 
142          init;
143 
144          main;
145 
146   
147 
148 end.

 

posted on 2016-03-02 20:59  Yesphet  阅读(139)  评论(0编辑  收藏  举报