[HLOI 2015]Magic

Magic

时间限制 2S 内存限制 128M

题目描述

给定n个点m条边的图,每个点有一个点权ai,两点之间的边的边权为两点点权的最小公倍数。
从1号点走到n号点所需要的花费为路径上边权的最大值。
问从1号点走到n号点需要的最小花费为多少?

输入

第一行输入整数n和m
接下来一行n个数,第i个数ai代表第i个点的点权
接下来m行,每行2个数u,v,代表第u个点与第v个点之间有一条边
对于30%的数据,1<=n,m<=100,1<=u,v<=n,1<=ai<=1000
对于60%的数据,1<=n<=100,1<=m<=10000,1<=u,v<=n,1<=ai<=1000
对于100%的数据,1<=n,m<=100000,1<=u,v<=n,1<=ai<=1000

输出

输出一个整数代表最小花费

样例输入

3 3
2 3 5
1 3
3 1
2 2

样例输出

10

样例提示:

只有一条路径从1到3,边权为10,故最小花费为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;
var head,tail,i,tt,v,dis:longint;
begin
    for i:=1 to n do
        dist[i]:=1000000000;
    dist[1]:=0; t[1]:=1; head:=1; tail:=2; p[1]:=1;
    while head<tail do
        begin
            v:=t[head]; inc(head); tt:=w[v,3];
            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
    readln(n,m); len:=n+1; r:=0;
    for i:=1 to n do
        read(x[i]);
    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;
 a,b,c,l,r,mid,head,tail,x,y,tt,pp: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;
 head:=1; tail:=2; t[1]:=1; p[1]:=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 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;
   head:=x; tail:=y;
  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);
 readln(n,m);s:=0; k:=n+1;
 for i:=1 to n do
  read(z[i]);
 readln;
 for i:=1 to m do
  begin
   readln(a,b); c:=lcm(z[a],z[b]);
   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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值