哈利波特与魔法石[AHOI2002]

本文分析了一次在解决图论中最短路问题时遇到的错误,特别关注于如何正确处理有向图。通过案例研究,作者深入探讨了算法实现过程中的陷阱,并提供了解决方案。
算法:图论(最短路)

分析:比较裸的最短路了,竟然错在了有向图上,导致此题本应AC的结果只拿了20分……恨!


program hldtymfs;

const
 maxm=10000;
 maxn=100;
 diss:array [1..7] of longint=(2,6,4,8,6,10,14);
 
type
 atp=record
  y,dis,next:longint;
 end;

var
 tot,c,head,tail,start,finish:longint;
 map:array [0..maxm] of atp;
 que,first,v:array [0..maxn] of longint;
 b:array [0..maxn] of boolean;
 u:array [0..maxn] of 0..1;

procedure init;
var
 i,x,y,dis:longint;
begin
 tot:=0;
 fillchar(v,sizeof(v),100);
 for i:=1 to 7 do read(u[i]);
 readln(start,finish);
 readln(c);
 for i:=1 to c do
  begin
   readln(x,y,dis);
   inc(tot);
   map[tot].y:=y;
   if u[dis]=1 then map[tot].dis:=diss[dis] shr 1 else map[tot].dis:=diss[dis];
   map[tot].next:=first[x];
   first[x]:=tot;
   
   inc(tot);
   map[tot].y:=x;
   if u[dis]=1 then map[tot].dis:=diss[dis] shr 1 else map[tot].dis:=diss[dis];
   map[tot].next:=first[y];
   first[y]:=tot;   
  end;
end;

procedure SPFA;
var
 t:longint;
begin
 head:=0;
 tail:=1;
 v[start]:=0;
 que[1]:=start;
 b[start]:=true;
 while head<tail do
  begin
   inc(head);
   t:=first[que[head]];
   while t>0 do
    begin
     if v[que[head]]+map[t].dis<v[map[t].y] then
      begin
       v[map[t].y]:=v[que[head]]+map[t].dis;
       if not b[map[t].y] then
        begin
         b[map[t].y]:=true;
         inc(tail);
         que[tail]:=map[t].y;
        end;
      end;
     t:=map[t].next;
    end;
   b[que[head]]:=false;
  end;
end;

begin
 assign(input,'hldtymfs.in'); reset(input);
 assign(output,'hldtymfs.out'); rewrite(output);
 
 init;
 SPFA;
 writeln(v[finish]);
 
 close(input); close(output);
end.



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值