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. 《Linux多线程服务端编程》笔记——线程同步精要

    并发编程基本模型 message passing和shared memory. 线程同步的四项原则 尽量最低限度地共享对象,减少需要同步的场合.如果确实需要,优先考虑共享 immutable 对象. ...

  2. 简单加解密算法 - vb.net

    Public Class Form1    Dim charAarray() As Char    '加密    Private Sub Btn_En_Click(sender As System.O ...

  3. 使用AOP的方式监测方法执行耗时

    在一些对系统中,往往可能需要对一些核心业务做相应的监测.如:记录调用参数,返回值,方法执行耗时等等.如果直接在方法的前后加入代码,如下: public int F(int a, string s) { ...

  4. Javascript/js的相等和不等运算符(= 、== 、===)

    "=="和"==="运算符用于比较两个值是否相等,当然它们对相等的定义不尽相同.两个运算符允许任意类型的操作数,如果操作数相等则返回tru,否则返回false. ...

  5. 基於tiny4412的Linux內核移植 --- 实例学习中断背后的知识(1)

    作者:彭东林 邮箱:pengdonglin137@163.com QQ:405728433 平台 tiny4412 ADK Linux-4.9 概述 前面几篇博文列举了在有设备树的时候,gpio中断的 ...

  6. 不要怂,就是GAN (生成式对抗网络) (四):训练和测试 GAN

    在 /home/your_name/TensorFlow/DCGAN/ 下新建文件 train.py,同时新建文件夹 logs 和文件夹 samples,前者用来保存训练过程中的日志和模型,后者用来保 ...

  7. [JQuery]JQuery选择器引擎Sizzle

    写代码过程中,发现使用JQuery选择器时,$('div.tooltip')和$('.tooltip')的结果不一样,怀疑和选择器的代码逻辑有关(事后证明是代码的低级错误,但是从查找原因的过程中,学到 ...

  8. ArcGIS制图技巧系列(1)还原真实的植被

    ArcGIS制图技巧系列(1)还原真实的植被 by 李远祥 在GIS数据中,植被一般都是面装要素的形式存在.很多人在使用植被渲染的时候,一般会采用填充符号去渲染.而在ArcGIS中,填充符号要么就是纯 ...

  9. Swift2.2 看完这篇博客 你不想懂也会懂得----二叉树

    一:初衷 我自己也好奇,为什么莫名其妙的想起写这个,其实数据里面包含的结构和逻辑我自己觉得才是最原始经典的,最近也在学swift,就向着利用swift整理一些二叉树.自己刚开始的时候也是用OC看着别的 ...

  10. Apache常见功能实战详解

    Apache 是一款使用量排名第一的 web 服务器,LAMP 中的 A 指的就是它.由于其开源.稳定.安全等特性而被广泛使用.前边的一篇文章中已经记录过如何搭建 LAMP 架构,搭建仅是第一步,其中 ...