【题解】Luogu P3110 [USACO14DEC]驮运Piggy Back

题目描述

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.

Bessie 和 Elsie在不同的区域放牧,他们希望花费最小的能量返回谷仓。从一个区域走到一个相连区域,Bessie要花费B单位的能量,Elsie要花费E单位的能量。

如果某次他们两走到同一个区域,Bessie 可以背着 Elsie走路,花费P单位的能量走到另外一个相连的区域。当然,存在P>B+E的情况。

相遇后,他们可以一直背着走,也可以独立分开。

输入输出格式

输入格式:

INPUT: (file piggyback.in)

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: (file piggyback.out)

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.

输入输出样例

输入样例#1: 复制

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

输出样例#1: 复制

22

思路

  • 做三次最短路
  • 从农场1到所有点的最短路
  • 从农场2到所有点的最短路
  • 从终点到所有点的最短路
  • 一一枚举两头牛的相交点,更新最优答案

代码

#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<queue>
#include<cstring>
#include<iostream>
#include<algorithm>
#define re register int
using namespace std;
const int inf=2147483647;
int b,e,p,n,m;
#define MAXN 100000
inline int read(){
int x=0,w=1;
char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-') w=-1,ch=getchar();
while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+ch-48,ch=getchar();
return x*w;
}
void spfa(int,int *);
vector<int> edge[MAXN],e_w[MAXN];
int db[MAXN],de[MAXN],dp[MAXN],inque[MAXN];
int main() {
// freopen("p3110.in","r",stdin);
// freopen("p3110.out","w",stdout);
b=read(),e=read(),p=read(),n=read(),m=read();
for(int i=1;i<=m;++i) {
int u,v;
u=read(),v=read();
edge[u].push_back(v);
e_w[u].push_back(1);
edge[v].push_back(u);
e_w[v].push_back(1);
}
spfa(1,db);
spfa(2,de);
spfa(n,dp);
int ans=inf;
for(int i=1;i<=n;++i) ans=min(ans,b*db[i]+e*de[i]+p*dp[i]);
printf("%d\n",ans);
}
void spfa(int x,int *d) {
queue<int> q;
q.push(x);
for(int i=1;i<=n;++i) d[i]=100000;
d[x]=0;
memset(inque,false,sizeof(inque));
inque[x]=true;
do
{
int u=q.front();
q.pop();
inque[u]=false;
for(re i=0;i<edge[u].size();++i) {
int v=edge[u][i],w=e_w[u][i];
if(d[v]>d[u]+w) {
d[v]=d[u]+w;
if(!inque[v]) {
inque[v]=true;
q.push(v);
}
}
}
}
while(!q.empty());
}

【题解】Luogu P3110 [USACO14DEC]驮运Piggy Back的更多相关文章

  1. Luogu P3110 [USACO14DEC]驮运Piggy Back

    解题思路 看到下面很多人都在说什么遇到了之后要不要背着走,其实根本不需要,同样的我也是跑了三遍$SPFA$,求出了以$1$为起点到个点的$dist$,和以$2$为起点到个点的$dist$,还有以$n$ ...

  2. luogu P3110 [USACO14DEC]驮运Piggy Back |最短路

    题目描述 Bessie and her sister Elsie graze in different fields during the day, and in the evening they b ...

  3. 洛谷P3110 [USACO14DEC]驮运Piggy Back

    P3110 [USACO14DEC]驮运Piggy Back 题目描述 贝西和她的妹妹艾尔斯白天在不同的地方吃草,而在晚上他们都想回到谷仓休息.聪明的牛仔,他们想出了一个计划,以尽量减少他们在步行时花 ...

  4. P3110 [USACO14DEC]驮运Piggy Back

    传送门 做过次短路后,再来做这题感觉轻松不少. 这题看着就像最短路模板题. 思路: 虽说题目看起来比较水,但是码起来还是有点难度的.(对我这个蒟蒻来说) 这道题,跟"路障"一题差不 ...

  5. [USACO14DEC]驮运Piggy Back

    题目描述 Bessie 和 Elsie在不同的区域放牧,他们希望花费最小的能量返回谷仓.从一个区域走到一个相连区域,Bessie要花费B单位的能量,Elsie要花费E单位的能量. 如果某次他们两走到同 ...

  6. 2018.08.17 洛谷P3110 [USACO14DEC]驮运(最短路)

    传送门 一道sb最短路,从两个起点和终点跑一边最短路之后直接枚举两人的汇合点求最小值就行了. 代码: #include<bits/stdc++.h> #define N 40005 #de ...

  7. [luoguP3110] [USACO14DEC]驮运Piggy Back(SPFA || BFS)

    传送门 以 1,2,n 为起点跑3次 bfs 或者 spfa 那么 ans = min(ans, dis[1][i] * B + dis[2][i] * E + dis[3][i] * P) (1 & ...

  8. [题解] Luogu P5446 [THUPC2018]绿绿和串串

    [题解] Luogu P5446 [THUPC2018]绿绿和串串 ·题目大意 定义一个翻转操作\(f(S_n)\),表示对于一个字符串\(S_n\), 有\(f(S)= \{S_1,S_2,..., ...

  9. 洛谷 [P3110] 驮运

    题目略带一点贪心的思想,先跑三遍最短路(边权为一,BFS比SPFA高效) 一起跑总比分开跑高效,枚举两人在何点汇合,输出最小值. #include <iostream> #include ...

随机推荐

  1. mysql-创建用户并授权,设置允许远程连接

    一.创建用户并授权 1.登录mysql mysql -u root -q 2.创建数据库 create database dbdata;//以创建dbdata为例 3.创建用户 创建user01,只能 ...

  2. Base64文件上传(Use C#)

    Base64是网络上最常见的用于传输8Bit字节码的编码方式之一,它是一种基于64个可打印字符来表示二进制数据的方法. 使用base64进行文件上传的具体流程是:前台使用js将文件转换为base64格 ...

  3. Git 系列教程(7)- 撤销操作

    撤销操作 在任何一个阶段,你都有可能想要撤销某些操作 有些撤销操作是不可逆的 --amend 修补提交 有时候我们提交完了才发现漏掉了几个文件没有添加,或者提交信息写错了. 此时,就可以运行带有 -- ...

  4. OO第一单元总结——表达式求导

    第一次作业 (1) UML结构图 (2)结构分析 Polynomial 类是对输入的字符串进行预处理,其中包括判断格式是否合法,运算符简化,分割成项等方法. Polynomial处理后得到的每一个项的 ...

  5. Beta——发布声明

    Beta阶段 1. 新功能: 介绍页面 用户点击软件右上角的 ? 按钮即可看到软件的操作说明! 项目模式 目前软件支持三种模式 空白表单模式.该模式可以生成基于模板的表单数据,也支持生成数据直接训练模 ...

  6. css背景|列表样式

    背景样式 背景区包含内容.padding 和 boder 不包含外边距 background-color 设置元素的背景颜色 background-image 把图像设置为背景,包含内边距和边框,不包 ...

  7. [bug] Unable to create initial connections of pool.

    原因1 pom中mysql依赖的版本不对,导致无法连接mysql 原因2 SSL设置问题 参考 https://blog.csdn.net/qq_26346457/article/details/79 ...

  8. mysql集群无法启动成功

    场景:两台数据库运行一段时间后发现集群挂了,一台服务正常,一台不正常. 日志如下: [ERROR] InnoDB: Attempted to open a previously opened tabl ...

  9. 测试 USB 存储设备读写性能(Mb/s),平均读写速度等

    1.将U盘(USB3.0)插入被测试机器,假定识别设备为sdc2.创建vfat文件系统分区/dev/sdb1分区容量大于30GBumount /dev/sdc1mkfs -t vfat /dev/sd ...

  10. 039.Python使用TCP实现多用户并发

    使用TCP实现多用户并发 在前面的实验中,TCP建立连接时,只能允许一个用户连接,当第二个用户建立连接时,发送的信息,服务端是没有办法接受,只有当第一个用户退出时,才能接受到第二个用户的请求,并实现通 ...