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
算法:图论
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.