题目来源:

https://www.luogu.org/problemnew/show/P3110

题目描述:

题目描述

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 

解题思路:

这题就是求两个人到n的最短的距离和,因为每个人走的时长不一样a,b,而且两个人一起走的时候,可能可以花更短的时长p<a+b;一开始想怎么处理一起走的问题,还想过要判断是否为最短路径和交点,后来发现,两个人相遇后一定会一起到终点,因为这一定是最短的,那么我们可以先处理出以p为边权求出n到1-n的dis1【i】最短路,然后在求出A人从1到n的最短路dis2【i】,B人从2到n的最短路dis3【i】,那么我们可以枚举每个点为相遇点,那么ans=min(ans,dis1+dis2+dis3),这样就行了,可以画画图加深理解。。

代码:

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <queue>
#include <stack>
#include <cmath>
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
const int  maxn=1e5+10;
struct newt
{int to,next;
}e[maxn*2];
int fy[3];
int head[maxn],dis[3][maxn],cnt=0,vis[maxn],B,E,P,n,m;
void addedge(int u,int v)
{e[cnt].to=v;e[cnt].next=head[u];head[u]=cnt++;
}
void spfa(int id,int S)
{for(int i=1;i<=n;i++)dis[id][i]=inf,vis[i]=0;queue<int>q;q.push(S);vis[S]=1;dis[id][S]=0;while(!q.empty()){int now=q.front();q.pop();vis[now]=0;for(int i=head[now];i!=-1;i=e[i].next){int v=e[i].to;if(dis[id][v]>dis[id][now]+fy[id]){dis[id][v]=dis[id][now]+fy[id];if(vis[v])continue;vis[v]=1;q.push(v);}}}}
int main()
{scanf("%d%d%d%d%d",&B,&E,&P,&n,&m);for(int i=1;i<=n;i++)head[i]=-1;for(int i=1,a,b;i<=m;i++){scanf("%d%d",&a,&b);addedge(a,b);addedge(b,a);}fy[0]=B,fy[1]=E,fy[2]=P;spfa(0,1);spfa(1,2);spfa(2,n);int ans=1e9;for(int i=1;i<=n;i++){ans=min(ans,dis[0][i]+dis[1][i]+dis[2][i]);}printf("%d\n",ans);return 0;
}

洛谷 P3110 [USACO14DEC]驮运Piggy Back ( spfa) 题解相关推荐

  1. 洛谷 P3373 【模板】线段树 2 题解

    洛谷 P3373 [模板]线段树 2 题解 题面 题目链接:[戳这里](https://www.luogu.org/problemnew/show/P3373) 题目描述 输入输出格式 输入输出样例 ...

  2. 洛谷P2738 [USACO4.1]篱笆回路Fence Loops 题解

    洛谷P2738 [USACO4.1]篱笆回路Fence Loops 题解 题目链接:P2738 [USACO4.1]篱笆回路Fence Loops 题意:农夫布朗的牧场上的篱笆已经失去控制了.它们分成 ...

  3. 洛谷P3131 [USACO16JAN]Subsequences Summing to Sevens S 题解

    洛谷P3131 [USACO16JAN]Subsequences Summing to Sevens S 题解 题目链接:P3131 [USACO16JAN]Subsequences Summing ...

  4. 洛谷试炼场 P1553 数字反转(升级版)题解

    洛谷试炼场 P1553 数字反转(升级版)题解 [c] 题目描述 给定一个数,请将该数各个位上数字反转得到一个新数. 这次与NOIp2011普及组第一题不同的是:这个数可以是小数,分数,百分数,整数. ...

  5. 洛谷月赛T2 P6858[深海少女与胖头鱼]题解

    目录 题面 前置知识 数学期望 快速幂 逆元 题解 分析 AC Code 题面 洛谷十月月赛II T2 深海少女与胖头鱼 总共有 nnn 条带 「圣盾」的「胖头鱼」和 mmm 条不带圣盾的胖头鱼,每次 ...

  6. 【洛谷OJ C++】洛谷题单100 入门1顺序结构 题解及学习笔记

    洛谷平台题单100链接:https://www.luogu.com.cn/training/100#problems 目录 学习笔记: P1001 A+B Problem P1000 超级玛丽游戏 P ...

  7. 【LGR-142-Div.4】洛谷入门赛 #13 考后分析与题解

    洛谷入门赛 #Round 13 比赛分析与总结 T1 魔方 题目背景 题目描述 输入格式 输出格式 样例 #1 样例输入 #1 样例输出 #1 提示 数据规模与约定 分析 AC代码 注意 T2 教学楼 ...

  8. 【洛谷OJ C++】洛谷题单101 入门2分支结构 题解及学习笔记

    洛谷题单101链接:https://www.luogu.com.cn/training/101#problems 笔记及题解目录: 学习笔记: P5710 [深基3.例2]数的性质 P5711 [深基 ...

  9. 【洛谷3110】【USACO14DEC】驮运Piggy Back

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

  10. 驮运Piggy Back

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

最新文章

  1. 独家 | 播客:入场券便是你的脸(附链接)
  2. 使用Entity Framework和WCF Ria Services开发SilverLight之4:Map之主外键映射
  3. 支持多编程语言的自动测试系统
  4. 微信小程序_Bug解决_setData失效
  5. Android 国际化
  6. VFP访问外部数据源的几种方法
  7. 【转】在C#中读写INI配置文件
  8. HDU 1063 [Exponentiation]高精度
  9. xposed hook 静态函数_Xposed 实现原理分析
  10. 网易云音乐歌曲带时间轴歌词的提取
  11. PLINK-GWAS学习9------对于二元数据的关联分析
  12. 5.Linux系统中解压缩详解
  13. Ant Design Pro从零到一(Mock使用)
  14. 特殊符号“.”对命令识别的影响:bam样本名报错 和 转换gene id时‘ENSEMBL’ keys无法识别
  15. C语言的goto语句,scanf的注意点以及好玩的指令
  16. python数据中元素可以改变的是_下列Python数据中其元素可以改变的是( )。 (2.0分)_学小易找答案...
  17. php 课程设计总结心得体会,课程设计心得体会
  18. 小游戏算年龄(Java)
  19. NOJ 1581.最佳加法式
  20. 二进制取位操作。(愚蠢的人:书到用时方恨少)

热门文章

  1. 科技爱好者周刊:第 61 期
  2. 乱世王者服务器维护,乱世王者千变万化开服时间表_乱世王者新区开服预告_第一手游网手游开服表...
  3. linux 2.6.32文件系统的dentry父子关系
  4. JAVA项目 畅购商城 框架搭建
  5. 最新后盾网Smarty框架教程 Smarty重入门到实战教程 共14课
  6. 系统结构考点之CRAY-1向量处理机
  7. 2022-08-13 00:00:00 - 2022-08-13 23:59:59这种格式,后端如何处理成为date类型
  8. 解读加拿大综合类大学排名 提升雅思成绩刻不容缓
  9. 新版男神女神完整投票系统源码V5.5.21版本
  10. Python基础数据类型:字符串,列表,元组,集合,字典用法总结