[Poj 2387][Usaco2004 Nov]Til the Cows Come Home 带奶牛回家

[Usaco2004 Nov]Til the Cows Come Home 带奶牛回家

时间限制: 1 Sec 内存限制: 128 MB

题目描述

贝茜在谷仓外的农场上,她想回到谷仓,在第二天早晨农夫约翰叫她起来挤奶之前尽可能多地睡上一觉.由于需要睡个好觉,贝茜必须尽快回到谷仓.农夫约翰的农场上有N(2≤N≤1000)个路标,每一个路标都有唯一的编号(1到N).路标1是谷仓,路标N是贝茜一整天呆在那里的果树园.农场的所有路标之间共有T(1≤T≤2000)条不同长度的供奶牛走的有向小路.贝茜对她识别方向的能力不是很自信,所以她每次总是从一条小路的头走到尾,再以这条路的尾作为下一条路的头开始走. 现给出所有路标之间的小路,要求输出贝茜回到谷仓的最短路程(每组输入数据都保证有解).

输入

第1行:2个整数T和N.
第2到T+1行:每行用空格隔开的三个整数描述一条小路.前两个整数是这条小路的尾和头,
第三个整数是这条小路的长度(不大于100).

输出

一个整数,表示贝茜从路标N到路标1所经过的最短路程

样例输入

5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100

样例输出

90

提示

共有5个路标,贝茜可以依次经过路标4,3,2,1到家

要判重!
要判重!
要判重!
重要的事情说三遍!
dijkstra需要判重,SPFA不用

邻接矩阵的Dijkstra

var
 n,m,p:longint;
 i,j:longint;
 min,t:longint;
 a,b,c:longint;
 w:array[0..1000,0..1000]of longint;
 dist,s:array[0..1000]of longint;
begin
 readln(m,n); p:=1;
 for i:=1 to m do
  begin
   readln(a,b,c);
   if ((w[a,b]<>0)and(c<w[a,b]))or(w[a,b]=0) //**判重!!!**
   then
    begin
     w[a,b]:=c;
     w[b,a]:=c;
    end;
  end;
 for i:=1 to n do
  dist[i]:=maxlongint;
 dist[n]:=0;
 while s[0]<>n do
  begin
   min:=maxlongint;
   for i:=1 to n do
    if (dist[i]<min)and(s[i]=0)
    then begin min:=dist[i]; t:=i; end;
   for i:=1 to n do
    if (s[i]=0)and(w[t,i]<>0)and(dist[t]+w[t,i]<dist[i])
    then dist[i]:=dist[t]+w[t,i];
   s[t]:=1; inc(s[0]);
   if s[p]=1 then break;
  end;
 writeln(dist[p]);
end.

邻接矩阵的SPFA

var
 w:array[0..1000,1..1000]of longint;
 p,dist:array[0..1000]of longint;
 t:array[0..10000]of longint;
 i,j,k:longint;
 a,b,c,x,y,tt:longint;
 n,m,start,finish:longint;
 head,tail:longint;
begin
 readln(m,n);
 for i:=1 to m do
  begin
   readln(a,b,c);
   if (w[a,b]=0)or((w[a,b]<>0)and(c<w[a,b]))
   then
    begin
     w[a,b]:=c;
     w[b,a]:=c;
    end;
  end;
 start:=n; finish:=1;
 for i:=1 to n do
  dist[i]:=100000;
 dist[start]:=0;
 head:=1; tail:=2; t[1]:=start; p[start]:=1;
 while head<tail do
  begin
   x:=head; y:=tail;
   for i:=head to tail-1 do
    begin
     for j:=1 to n do
      if (w[t[i],j]<>0)and(dist[t[i]]+w[t[i],j]<dist[j])
      then
       begin
        dist[j]:=dist[t[i]]+w[t[i],j];
        if p[j]=0
        then begin t[y]:=j; inc(y); end;
       end;
     inc(x);
     p[t[i]]:=0;
    end;
   head:=x; tail:=y;
  end;
 writeln(dist[finish]);
end.

邻接链表的SPFA

var
 w:array[0..10000,1..3]of longint;
 p,dist:array[0..1000]of longint;
 t:array[0..10000]of longint;
 i,j,k:longint;
 a,b,c,x,y,tt:longint;
 n,m,start,finish:longint;
 head,tail:longint;
begin
 readln(m,n); k:=n+1;
 for i:=1 to m do
  begin
   readln(a,b,c);
   w[k,1]:=b;
   w[k,2]:=c;
   if w[a,3]=0
   then w[a,3]:=k
   else w[w[a,1],3]:=k;
   w[a,1]:=k;
   inc(k);
   w[k,1]:=a;
   w[k,2]:=c;
   if w[b,3]=0
   then w[b,3]:=k
   else w[w[b,1],3]:=k;
   w[b,1]:=k;
   inc(k);
  end;
 start:=n; finish:=1;
 for i:=1 to n do
  dist[i]:=100000;
 dist[start]:=0;
 head:=1; tail:=2; t[1]:=start; p[start]:=1;
 while head<tail do
  begin
   x:=head; y:=tail;
   for i:=head to tail-1 do
    begin
     tt:=w[t[i],3];
     while tt<>0 do
      begin
       if dist[t[i]]+w[tt,2]<dist[w[tt,1]]
       then
        begin
         dist[w[tt,1]]:=dist[t[i]]+w[tt,2];
         if p[w[tt,1]]=0
         then begin t[y]:=w[tt,1]; inc(y); end;
        end;
       tt:=w[tt,3];
      end;
     inc(x);
     p[t[i]]:=0;
    end;
   head:=x; tail:=y;
  end;
 writeln(dist[finish]);
end.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值