3891: [Usaco2014 Dec]Piggy Back

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 116  Solved: 92
[Submit][Status][Discuss]

Description

Bessie and her sister Elsie graze in different fields during the day, and in the evening they both want to walk back to the barn to rest. Being clever bovines, they come up with a plan to minimize the total amount of energy they both spend while walking. Bessie spends B units of energy when walking from a field to an adjacent field, and Elsie spends E units of energy when she walks to an adjacent field. However, if Bessie and Elsie are together in the same field, Bessie can carry Elsie on her shoulders and both can move to an adjacent field while spending only P units of energy (where P might be considerably less than B+E, the amount Bessie and Elsie would have spent individually walking to the adjacent field). If P is very small, the most energy-efficient solution may involve Bessie and Elsie traveling to a common meeting field, then traveling together piggyback for the rest of the journey to the barn. Of course, if P is large, it may still make the most sense for Bessie and Elsie to travel separately. On a side note, Bessie and Elsie are both unhappy with the term "piggyback", as they don't see why the pigs on the farm should deserve all the credit for this remarkable form of transportation. Given B, E, and P, as well as the layout of the farm, please compute the minimum amount of energy required for Bessie and Elsie to reach the barn.

给定一个N个点M条边的无向图,其中Bessie在1号点,Elsie在2号点,它们的目的地为N号点。Bessie每经过一条边需要消耗B点能量,Elsie每经过一条边需要消耗E点能量。当它们相遇时,它们可以一起行走,此时它们每经过一条边需要消耗P点能量。求它们两个到达N号点时最少消耗多少能量?

Input

The first line of input contains the positive integers B, E, P, N, and M. All of these are at most 40,000. B, E, and P are described above. N is the number of fields in the farm (numbered 1..N, where N >= 3), and M is the number of connections between fields. Bessie and Elsie start in fields 1 and 2, respectively. The barn resides in field N. The next M lines in the input each describe a connection between a pair of different fields, specified by the integer indices of the two fields. Connections are bi-directional. It is always possible to travel from field 1 to field N, and field 2 to field N, along a series of such connections.

Output

A single integer specifying the minimum amount of energy Bessie and
Elsie collectively need to spend to reach the barn.  In the example
shown here, Bessie travels from 1 to 4 and Elsie travels from 2 to 3
to 4.  Then, they travel together from 4 to 7 to 8.
 

Sample Input

4 4 5 8 8
1 4
2 3
3 4
4 7
2 5
5 6
6 8
7 8

Sample Output

22

HINT

 

Source

题解:直接求出1、2、n点到各点的距离(由于是无向图所以方向神马的直接无视之),然后枚举各个汇合点,然后计算各个点的代价,然后输出,然后AC
一开始在怀疑这样子是否一定可行,是否会存在两者会合后再次分开的可能,但实际上,如果两个人一起行动更合适的话,那么相遇就不需要再分离;如果两个人不适合一起行动的话,那么干脆就不需要相遇。所以不要相遇了再分离,综上。(HansBug:嗨。。这话写的。。。为啥感觉越读越戳泪点TT)
(PS:程序里面我很逗比的还弄了个b作为反向map,实际上完全不必,一开始我没发现这个是无向图,所以b用来存储反向图,后来才发现我想多了TT)
 /**************************************************************
Problem:
User: HansBug
Language: Pascal
Result: Accepted
Time: ms
Memory: kb
****************************************************************/ type
point=^node;
node=record
g,w:longint;
next:point;
end;
map=array[..] of point;
arr=array[..] of longint;
var
i,j,k,l,m,n,a1,a2,a3:longint;
a,b:map;
c,e,f,g:arr;
d:array[..] of longint;
function min(x,y:longint):longint;inline;
begin
if x<y then min:=x else min:=y;
end;
function max(x,y:longint):longint;inline;
begin
if x>y then max:=x else max:=y;
end;
procedure add(x,y,z:longint;var a:map);inline;
var p:point;
begin
new(p);p^.g:=y;p^.w:=z;
p^.next:=a[x];a[x]:=p;
end;
procedure spfa(x:longint;a:map;var c:arr);inline;
var f,r:longint;p:point;
begin
fillchar(g,sizeof(g),);
fillchar(c,sizeof(c),);
f:=;r:=;d[]:=x;g[x]:=;c[x]:=;
while f<r do
begin
p:=a[d[f]];
while p<>nil do
begin
if (c[p^.g]=) or((c[p^.g]>) and (c[p^.g]>(c[d[f]]+p^.w))) then
begin
c[p^.g]:=c[d[f]]+p^.w;
if g[p^.g]= then
begin
g[p^.g]:=;
d[r]:=p^.g;
inc(r);
end;
end;
p:=p^.next;
end;
g[d[f]]:=;
inc(f);
end;
for i:= to n do dec(c[i]);
end; begin
readln(a1,a2,a3,n,m);
for i:= to n do a[i]:=nil;
for i:= to n do b[i]:=nil;
for i:= to m do
begin
readln(j,k);
add(j,k,,a);
add(k,j,,a);
end;
spfa(,a,c);
spfa(,a,e);
spfa(n,a,f);
l:=maxlongint;
for i:= to n do
if (c[i]<>-) and (e[i]<>-) and (f[i]<>-) then
l:=min(l,a1*c[i]+a2*e[i]+a3*f[i]);
writeln(l);
end.

3891: [Usaco2014 Dec]Piggy Back的更多相关文章

  1. bzoj3891[Usaco2014 Dec]Piggy Back*

    bzoj3891[Usaco2014 Dec]Piggy Back 题意: 给定一个N个点M条边的无向图,其中Bessie在1号点,Elsie在2号点,它们的目的地为N号点.Bessie每经过一条边需 ...

  2. 3893: [Usaco2014 Dec]Cow Jog

    3893: [Usaco2014 Dec]Cow Jog Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 174  Solved: 87[Submit] ...

  3. 3892: [Usaco2014 Dec]Marathon

    3892: [Usaco2014 Dec]Marathon Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 169  Solved: 100[Submi ...

  4. [bzoj3893][Usaco2014 Dec]Cow Jog_暴力

    Cow Jog bzoj-3893 Usaco-2014 Dec 题目大意:题目链接. 注释:略. 想法: 先按照坐标排序. 我们发现每个牛只会被后面的牛影响. 所以我们考虑逆向枚举. 记录一下i+1 ...

  5. bzoj3892[Usaco2014 Dec]Marathon*

    bzoj3892[Usaco2014 Dec]Marathon 题意: 在二维平面上有N个点,从(x1,y1)到(x2,y2)的代价为|x1-x2|+|y1-y2|.求从1号点出发,按从1到N的顺序依 ...

  6. Bzoj3893 [Usaco2014 Dec]Cow Jog

    Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 302  Solved: 157 Description The cows are out exerci ...

  7. bzoj 3824: [Usaco2014 Dec]Guard Mark【状压dp】

    设f[s]为已经从上到下叠了状态为s的牛的最大稳定度,转移的话枚举没有在集合里并且强壮度>=当前集合牛重量和的用min(f[s],当前放进去的牛还能承受多种)来更新,高度的话直接看是否有合法集合 ...

  8. BZOJ-USACO被虐记

    bzoj上的usaco题目还是很好的(我被虐的很惨. 有必要总结整理一下. 1592: [Usaco2008 Feb]Making the Grade 路面修整 一开始没有想到离散化.然后离散化之后就 ...

  9. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

随机推荐

  1. C# winform初学者实例

    快递单打印通 下载地址: http://pan.baidu.com/s/1nue5ifn

  2. lufylegend库 鼠标事件 循环事件 键盘事件

    lufylegend库 鼠标事件 循环事件 键盘事件 <!DOCTYPE html> <html lang="en"> <head> <m ...

  3. JavaScript原生的节点操作

    前言:原生是Javascript的基础,还是需要多多重视,时间长都忘记了,现在整理一下. 获取子节点 children 不是标准的dom属性,但是几乎被所有浏览器支持.不包含文本节点. 注意:在IE中 ...

  4. Android开发系列之屏幕密度和单位转换

    由于Android的开源性,所以目前市面上面Android手机的分辨率特别多,这样的话就给我适配带来了一定的难度.要想做好适配,我们首先应该明白什么是分辨率.PPI.屏幕大小等概念,还有在不同的屏幕密 ...

  5. HDU5882

    Balanced Game Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  6. HDU5058

    So easy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  7. 【c基础】之 文件及其操作

    文件的打开与关闭 首先要定义一个文件指针类型,格式为 FILE *文件指针名; ; FILE *fp; //fp就是定义的文件指针 ●打开文件fopen()函数,格式: fp = fopen(&quo ...

  8. 从jvm的角度来看单例模式

    最近在看jvm,发现随着自己对jvm底层的了解,现在对java代码可以说是有了全新的认识.今天就从jvm的角度来看一看以前自以为很了解的单例模式. 了解单例模式的人都知道,单例模式有两种:" ...

  9. 基于Ceph快照的异地灾备设计

    作者:吴香伟 发表于 2017/02/06 版权声明:可以任意转载,转载时务必以超链接形式标明文章原始出处和作者信息以及版权声明 喜欢请点击右边打赏,谢谢支持! 引子 技术改变生活. 越来越方便的手机 ...

  10. 谈谈getElementsByClassName()

    HTML5中新增的一个方法getElementsByClassName(),但是并非所有浏览器有支持 因此我们构造一个方法兼容这个方法 <script type="text/javas ...