# [HLOI 2015]Magic

392人阅读 评论(0)

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.

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：114961次
• 积分：4439
• 等级：
• 排名：第6719名
• 原创：336篇
• 转载：0篇
• 译文：0篇
• 评论：21条
友链～
文章分类
最新评论