解题思路

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

之后直接枚举两头牛在哪里相遇,相遇之后一起背着走的路程乘以$p+$相遇之前$B$走的距离乘以$b+$相遇之前$E$走的距离乘以$e$,去一个最小的这样的值就是答案。

关于为什么不需要分类讨论,因为你把每个点都枚举了一遍,即使存在$p>b+e$的情况,那这种情况就等价于两头奶牛在$n$点相遇。

附上代码

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
const int maxn = 8e4+, INF = ;
int b, e, p, n, m, fir[maxn], nxt[maxn], u[maxn], v[maxn], cnt;
int dist_n[maxn], dist_1[maxn], dist_2[maxn], w[maxn], Ans = INF;
bool vis[maxn];
inline int read() {
int x = , f = ; char c;
while (c < '' || c > '') {if(c == '-') f = -; c = getchar();}
while (c <= '' && c >= '') {x = x* + c-''; c = getchar();}
return x * f;
}
inline void addedge(int x, int y, int z) {
nxt[++cnt] = fir[x];
fir[x] = cnt;
u[cnt] = x, v[cnt] = y, w[cnt] = z;
}
inline void SPFA(int s, int *dist) {
std::queue<int> Q;
std::memset(vis, , sizeof(vis));
std::fill(dist+, dist++n, INF);
vis[s] = , dist[s] = ;
Q.push(s);
int x, k;
while (!Q.empty()) {
x = Q.front(), Q.pop();
int k = fir[x];
while (k != -) {
if(dist[v[k]] > dist[u[k]] + w[k]) {
dist[v[k]] = dist[u[k]] + w[k];
if(!vis[v[k]]) Q.push(v[k]);
}
k = nxt[k];
}
vis[x] = ;
}
}
int main() {
b = read(), e = read(), p = read(), n = read(), m = read();
std::memset(fir, -, sizeof(fir));
int x, y, z = ;
for(register int i=; i<=m; i++) {
x = read(), y = read();
addedge(x, y, z), addedge(y, x, z);
}
SPFA(, dist_1), SPFA(, dist_2), SPFA(n, dist_n);
for(int i=; i<=n; i++)
Ans = std::min(Ans, dist_1[i] * b + dist_2[i] * e + dist_n[i] * p);
printf("%d", Ans);
}

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

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

    [题解]Luogu P3110 [USACO14DEC]驮运Piggy Back 题目描述 Bessie and her sister Elsie graze in different fields ...

  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. 洛谷 [P3110] 驮运

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

  9. luogu P3111 [USACO14DEC]牛慢跑Cow Jog_Sliver |贪心+模拟

    有N (1 <= N <= 100,000)头奶牛在一个单人的超长跑道上慢跑,每头牛的起点位置都不同.由于是单人跑道,所有他们之间不能相互超越.当一头速度快的奶牛追上另外一头奶牛的时候,他 ...

随机推荐

  1. http ssl

    http ssl

  2. python安装了2.7之后终端无法使用退格,上下左右

    遇到RT问题,原因是由于在编译python的时候去烧readline库造成的 解决办法: yum install readline-devel 然后重新编译安装python,终端控制符可用!

  3. aarch64-linux-gnu交叉编译Qt4.7.3

    到 Qt 官网下载合适的 Qt 版本,地址:http://download.qt-project.org/archive/qt/ 1.环境搭建: 1.安装automake.libtool 和主机上的 ...

  4. HDU1052Tian Ji -- The Horse Racing

    Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  5. 【转载】HTTP协议详解

    [本文转自]http://www.cnblogs.com/EricaMIN1987_IT/p/3837436.html 一.概念 协议是指计算机通信网络中两台计算机之间进行通信所必须共同遵守的规定或规 ...

  6. 微信H5页面 - 调酒

    这是微信H5的一款小游戏(一款酒的推广活动),主要游戏页面如下: 游戏规则: 点击选择2个元素(圈圈图片),放入瓶中,使它们与瓶中已有的三个元素碰撞,调配佳酿. 只有选择正确的2个元素搭配,才可以调配 ...

  7. 微信小程序-wepy-组件模板重复渲染

    微信小程序开发,有使用wepy框架的需求.上手: 安装自己可以到官网查看,飞机票:https://tencent.github.io/wepy/document.html#/ 具体开发模式和Vue开发 ...

  8. CodeIgnitor 配置类的使用

    CI 的配置文件统一放在 application/config/ 目录下面,框架有一个默认的主配置文件 application/config/config.php.其部分内容如下: <?php ...

  9. 添加jar到mvn私服

    1.生成jar文件 2.jar目录下执行: mvn install:install-file -Dfile=jave-1.0.2.jar -DgroupId=joinery -DartifactId= ...

  10. Android 性能优化(15)网络优化( 11)Manipulating Broadcast Receivers On Demand

    Manipulating Broadcast Receivers On Demand This lesson teaches you to Toggle and Cascade State Chang ...