显然是类似k短路,直接不停增广即可

好久没写A*了,裸的A*可能会TLE

加点剪枝就卡过去了………

  1 type node=record
  2        po,next:longint;
  3        cost:double;
  4      end;
  5      point=record
  6        loc:longint;
  7        num:double;
  8      end;
  9 
 10 var e,ee:array[0..200010] of node;
 11     d:array[0..5010] of double;
 12     q:array[0..2000010] of longint;
 13     h:array[0..2000010] of point;
 14     p,pp:array[0..5010] of longint;
 15     v:array[0..5010] of boolean;
 16     ans,t,x,y,i,len,n,m:longint;
 17     z,te:double;
 18 
 19 procedure swap(var a,b:point);
 20   var c:point;
 21   begin
 22     c:=a;
 23     a:=b;
 24     b:=c;
 25   end;
 26 
 27 procedure add(x,y:longint; z:double);
 28   begin
 29     e[i].po:=y;
 30     e[i].next:=p[x];
 31     e[i].cost:=z;
 32     p[x]:=i;
 33   end;
 34 
 35 procedure eadd(x,y:longint; z:double);
 36   begin
 37     ee[i].po:=y;
 38     ee[i].next:=pp[x];
 39     ee[i].cost:=z;
 40     pp[x]:=i;
 41   end;
 42 
 43 procedure spfa;
 44   var f,r,i,x,y:longint;
 45   begin
 46     for i:=1 to n-1 do
 47       d[i]:=1e40;
 48     d[n]:=0;
 49     f:=1;
 50     r:=1;
 51     q[1]:=n;
 52     while f<=r do
 53     begin
 54       x:=q[f];
 55       v[x]:=false;
 56       i:=pp[x];
 57       while i<>0 do
 58       begin
 59         y:=ee[i].po;
 60         if d[y]>d[x]+ee[i].cost then
 61         begin
 62           d[y]:=d[x]+ee[i].cost;
 63           if not v[y] then
 64           begin
 65             v[y]:=true;
 66             inc(r);
 67             q[r]:=y;
 68           end;
 69         end;
 70         i:=ee[i].next;
 71       end;
 72       inc(f);
 73     end;
 74   end;
 75 
 76 procedure sift(i:longint);
 77   var j:longint;
 78   begin
 79     j:=i shl 1;
 80     while j<=t do
 81     begin
 82       if (j<t) and (h[j].num>h[j+1].num) then inc(j);
 83       if h[i].num>h[j].num then
 84       begin
 85         swap(h[i],h[j]);
 86         i:=j;
 87         j:=j shl 1;
 88       end
 89       else break;
 90     end;
 91   end;
 92 
 93 procedure up(i:longint);
 94   var j:longint;
 95   begin
 96     j:=i shr 1;
 97     while j>0 do
 98     begin
 99       if h[i].num<h[j].num then
100       begin
101         swap(h[i],h[j]);
102         i:=j;
103         j:=j shr 1;
104       end
105       else break;
106     end;
107   end;
108 
109 procedure astar;
110   var i,x,y:longint;
111       dis:double;
112 
113   begin
114     h[1].loc:=1;
115     h[1].num:=d[1];
116     t:=1;
117     while t>0 do
118     begin
119       x:=h[1].loc;
120       dis:=h[1].num-d[x];
121       swap(h[1],h[t]);
122       dec(t);
123       sift(1);
124       if x=n then
125       begin
126         if te<dis then break
127         else begin
128           inc(ans);
129           te:=te-dis;
130         end;
131       //  writeln(te,' ',dis);
132       end;
133       i:=p[x];
134       while i<>0 do
135       begin
136         y:=e[i].po;
137         if dis+e[i].cost+d[y]<=te then
138         begin
139           inc(t);
140           h[t].loc:=y;
141           h[t].num:=dis+e[i].cost+d[y];
142           up(t);
143         end;
144         i:=e[i].next;
145       end;
146     end;
147   end;
148 
149 begin
150   readln(n,m,te);
151   for i:=1 to m do
152   begin
153     readln(x,y,z);
154     add(x,y,z);
155     eadd(y,x,z);
156   end;
157   spfa;
158   astar;
159   writeln(ans);
160 end.
View Code

 

posted on 2015-06-30 09:43  acphile  阅读(139)  评论(0编辑  收藏  举报