Cyh和香穗子(NDK1352)

24 篇文章 0 订阅
Cyh和香穗子


Time Limit:10000MS  Memory Limit:65536K
Total Submit:14 Accepted:8 


Description 


话说,Cyh和香穗子是好朋友,一天他们在fzsz迷路了….Cyh在地点1,香穗子在地点n.由于Cyh是土生土长的fzsz人,所以Cyh准备去n地给香穗子带路. 
fzsz是个奇怪的地方,它由n地点组成,并且任意两个地点A,B满足要么A能到B,要么B能到A,要么都不能互相到达,一定不存在A和B都能互相到达. 
现在Cyh希望快点到达n地 




Input 


第一行两个数n,m 
接下来m行,每行两个数a,b,表示地点a能达到地点b 




Output 


Cyh最少经过的地点数


Sample Input 




4 5
1 2
2 3
2 4
1 3
3 4




Sample Output 




3


Hint 


数据范围: 
n <= 100000,m <= 500000,保正有解 




Source 


NOIdaokan


算法:图论


裸的最短路,我会告诉你说题目要求是有向图结果我开的无向图么……

program p3;

const
 maxn=100000;
 maxm=1000000;
 
type
 atp=record
  y,next,dis:longint;
 end;

var
 n,m,tot,head,tail:longint;
 first,v:array [0..maxn] of longint;
 b:array [0..maxn] of boolean;
 map:array [0..maxm] of atp;
 que:array [0..maxm] of longint;
 
procedure init;
var
 i,x,y:longint;
begin
 readln(n,m);
 for i:=1 to m do
  begin
   readln(x,y);
   inc(tot);
   map[tot].y:=y;
   map[tot].next:=first[x];
   map[tot].dis:=1;
   first[x]:=tot;
  end;
end;

procedure SPFA;
var
 t:longint;
begin
 fillchar(v,sizeof(v),100);
 head:=0;
 tail:=1;
 que[1]:=1;
 v[1]:=0;
 b[1]:=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;
 writeln(v[n]+1);
end;

begin
 assign(input,'3.in'); reset(input);
 assign(output,'3.out'); rewrite(output);
 
 init;
 SPFA;
 
 close(input); close(output);
end.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值