# [HLOI 2015]Magic

3 3
2 3 5
1 3
3 1
2 2

10

## 题解

• 二分+最短路，裸题
• 第一次看见HLJ的省选
• 这么写好像比标算还要优233
对比我的代码风格2333333
const
maxn=100000;
var
p,dist,x:array[0..maxn]of longint;
t:array[0..4*maxn]of longint;
w:array[0..4*maxn,1..3]of longint;
i,j,k:longint;
n,m,a,b,c,len,l,r,mid,ans:longint;
function gcd(a,b:longint):longint;
begin
if b=0
then gcd:=a
else gcd:=gcd(b,a mod b);
end;

function lcm(a,b:longint):longint;
begin
exit(a*(b div gcd(a,b)));
end;

procedure init(a,b,c:longint);
begin
w[len,1]:=b; w[len,2]:=c;
if w[a,3]=0
then w[a,3]:=len else w[w[a,1],3]:=len;
w[a,1]:=len; inc(len);
end;

function spfa(a:longint):longint;
begin
for i:=1 to n do
dist[i]:=1000000000;
begin
while tt<>0 do
begin
if w[tt,2]>a then dis:=1 else dis:=0;
if dist[v]+dis<dist[w[tt,1]]
then
begin
dist[w[tt,1]]:=dist[v]+dis;
if p[w[tt,1]]=0
then begin t[tail]:=w[tt,1]; inc(tail); p[w[tt,1]]:=1; end;
end;
tt:=w[tt,3];
end;
p[v]:=0;
end;
if dist[n]=0 then exit(1) else exit(0);
end;

begin
for i:=1 to n do
for i:=1 to m do
begin
readln(a,b); c:=lcm(x[a],x[b]); if c>r then r:=c;
init(a,b,c);
init(b,a,c);
end;
l:=0;
while l<r do
begin
mid:=(l+r)>>1;
if spfa(mid)=0
then l:=mid+1
else r:=mid;
end;
writeln(l);
end.
var
z,p,dist:array[0..100000]of longint;
t:array[0..500000]of longint;
w:array[0..300000,1..3]of longint;
i,j:longint;
n,m,k,s:longint;
function gcd(a,b:longint):longint;
begin
if b=0 then exit(a);
gcd:=gcd(b,a mod b);
end;

function lcm(a,b:longint):longint;
begin
exit(a*b div gcd(a,b));
end;

function spfa(a:longint):longint;
var
i,j:longint;
begin
for i:=2 to n do
dist[i]:=100000000;
begin
begin
tt:=w[t[i],3];
while tt<>0 do
begin
if w[tt,2]>a then pp:=1 else pp:=0;
if dist[t[i]]+pp<dist[w[tt,1]]
then
begin
dist[w[tt,1]]:=dist[t[i]]+pp;
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;
end;
if dist[n]=0 then exit(1) else exit(0);
end;

begin
//assign(input,'magic.in'); assign(output,'magic.out'); reset(input); rewrite(output);
for i:=1 to n do
for i:=1 to m do
begin
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);
if c>r then r:=c;
end;
l:=0;
while l<r do
begin
mid:=(l+r)div 2;
if spfa(mid)=1
then r:=mid
else l:=mid+1;
end;
if spfa(l)=1
then writeln(l)
else writeln(-1);
//close(input); close(output);
end.


#### 17.10.8B组总结

2017-10-16 14:54:18

#### 2016.07.17【初中部 NOIP提高组 】模拟赛C（附题目及代码打包）

2016-07-18 11:17:49

#### pascal 解题模板（NOIP）

2016-11-10 17:56:51

#### 新手村之BOSS战-入门综合练习1

2017-01-15 17:13:03

#### Magic Quadrant for Security Information and Event Management 2015

2015-10-07 19:29:19

#### springmagic_v1.1

2017年09月26日 12KB 下载

#### HLOI2016 滚粗记

2016-04-22 21:38:44

#### [省选] [线段树] [HLOI2016] 字符串问题

2017-02-18 15:55:39

#### flash magic manual

2010年12月23日 546KB 下载

#### Magic Number魔数是什么

2017-02-21 22:50:18