BZOJ 2100 [Usaco2010 Dec]Apple Delivery 最短路

Description

Bessie has two crisp red apples to deliver to two of her friends in the herd. Of course, she travels the C (1 <= C <= 200,000) cowpaths which are arranged as the usual graph which connects P (1 <= P <= 100,000) pastures conveniently numbered from 1..P: no cowpath leads from a pasture to itself, cowpaths are bidirectional, each cowpath has an associated distance, and, best of all, it is always possible to get from any pasture to any other pasture. Each cowpath connects two differing pastures P1_i (1 <= P1_i <= P) and P2_i (1 <= P2_i <= P) with a distance between them of D_i. The sum of all the distances D_i does not exceed 2,000,000,000. What is the minimum total distance Bessie must travel to deliver both apples by starting at pasture PB (1 <= PB <= P) and visiting pastures PA1 (1 <= PA1 <= P) and PA2 (1 <= PA2 <= P) in any order. All three of these pastures are distinct, of course. Consider this map of bracketed pasture numbers and cowpaths with distances: If Bessie starts at pasture [5] and delivers apples to pastures [1] and [4], her best path is: 5 -> 6-> 7 -> 4* -> 3 -> 2 -> 1* with a total distance of 12.

一张P个点的无向图,C条正权路。
CLJ要从Pb点(家)出发,既要去Pa1点NOI赛场拿金牌,也要去Pa2点CMO赛场拿金牌。(途中不必回家)
可以先去NOI,也可以先去CMO。
当然神犇CLJ肯定会使总路程最小,输出最小值。

Input

* Line 1: Line 1 contains five space-separated integers: C, P, PB, PA1, and PA2 * Lines 2..C+1: Line i+1 describes cowpath i by naming two pastures it connects and the distance between them: P1_i, P2_i, D_i

Output

* Line 1: The shortest distance Bessie must travel to deliver both apples

Sample Input

9 7 5 1 4
5 1 7
6 7 2
4 7 2
5 6 1
5 2 4
4 3 2
1 2 3
3 2 2
2 6 3



Sample Output

12

HINT

求翻译.........站内PM我吧.........




传送门

n年前……好吧其实是1年前,

普及组准备赛里面做到过一毛一样的题……

现在会了10^5级别效率也好的各种最短路当然轻松水过了……

当时还打的P,,学了个dijkstra+优先队列很得瑟,

pascal里面映射啥啥打了一大堆;

还写了一批注释(汗..)


其实C++STL直接乱搞解决啊……

可是懒了,就来了发P.



var u,v,w,TP,C,P,PA1,PA2,PB,i,Len1,Len2,Len3,ans:longint;
    next,head,son,value:array[0..400001] of longint;
    //下一条、头边、子点、值
    dis,heap,intoout,outtoin:array[0..100001] of longint;
   //distance、堆、内映射外、外映射内
    visit:array[0..100001] of boolean;
   //访问
procedure swap(var a,b:longint);
var t:longint;
begin
  t:=a; a:=b; b:=t;
end;
procedure add(u,v,w:longint);
begin
  inc(Len1);
  next[Len1]:=head[u];
  head[u]:=Len1;
  son[Len1]:=v;
  value[Len1]:=w;   //连边表
end;
procedure up(x:longint);
begin
  while (x>1)and(heap[x>>1]>heap[x]) do
  begin
    swap(heap[x>>1],heap[x]);
    swap(intoout[x>>1],intoout[x]);
    swap(outtoin[intoout[x>>1]],outtoin[intoout[x]]);
    x:=x>>1;
  end;
end;  //从下往上放元素
procedure down(x:longint);
var Tson:longint;
begin
  heap[1]:=heap[TP];
  intoout[1]:=intoout[TP];
  outtoin[intoout[1]]:=1;
  dec(TP);
  while (x<<1<=TP) do
  begin
    if (x<<1+1>TP)or
       (heap[x<<1]<heap[x<<1+1]) then
         Tson:=x<<1
       else Tson:=x<<1+1;
    if heap[Tson]<heap[x] then
    begin
      swap(heap[x],heap[Tson]);
      swap(intoout[x],intoout[Tson]);
      swap(outtoin[intoout[x]],outtoin[intoout[Tson]]);
      x:=Tson;
    end else
      break;
  end;
end;   //从上往下整堆
procedure Dijkstra(start,goal:longint;var Len:longint);
var i,minP,Near_S:longint;
begin
  for i:=1 to P do
  begin
    outtoin[i]:=i;
    intoout[i]:=i;   //堆内外映射
    heap[i]:=maxlongint div 3;
    dis[i]:=maxlongint div 3;
    visit[i]:=true;  //初始化
  end;
  heap[start]:=0;
  up(start); //初始建堆
  TP:=P;
  while (visit[goal])and(TP>0) do
  begin
    minP:=intoout[1];
    dis[minP]:=heap[1];
    visit[minP]:=false;
    Near_S:=head[minP];   //堆首元素
    down(1);  //整堆
    while Near_S<>0 do  //找相邻边Near Side
    begin
      if (visit[son[Near_S]])and
         (value[Near_S]+dis[minP]<heap[outtoin[son[Near_S]]]) then
         begin
           heap[outtoin[son[Near_S]]]:=value[Near_S]+dis[minP];
          //更新堆内元素
           up(outtoin[son[Near_S]]);
         end;
      Near_S:=next[Near_S];  //下一条
    end;
  end;
  Len:=dis[goal];  //取值
end;
begin
  readln(C,P,PB,PA1,PA2);
  Len1:=0;
  for i:=1 to C do
  begin
    readln(u,v,w);
    add(u,v,w);
    add(v,u,w);  //双向记边
  end;
  Dijkstra(PB,PA1,Len1);//PB->PA1
  Dijkstra(PB,PA2,Len2);    //PB->PA2
  Dijkstra(PA1,PA2,Len3);  //PA1->PA2
  if Len1>Len2 then ans:=Len2+Len3
               else ans:=Len1+Len3;  //比较
  writeln(ans);
end.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值