Luogu P3110 [USACO14DEC]驮运Piggy Back
解题思路
看到下面很多人都在说什么遇到了之后要不要背着走,其实根本不需要,同样的我也是跑了三遍$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的更多相关文章
- 【题解】Luogu P3110 [USACO14DEC]驮运Piggy Back
[题解]Luogu P3110 [USACO14DEC]驮运Piggy Back 题目描述 Bessie and her sister Elsie graze in different fields ...
- luogu P3110 [USACO14DEC]驮运Piggy Back |最短路
题目描述 Bessie and her sister Elsie graze in different fields during the day, and in the evening they b ...
- 洛谷P3110 [USACO14DEC]驮运Piggy Back
P3110 [USACO14DEC]驮运Piggy Back 题目描述 贝西和她的妹妹艾尔斯白天在不同的地方吃草,而在晚上他们都想回到谷仓休息.聪明的牛仔,他们想出了一个计划,以尽量减少他们在步行时花 ...
- P3110 [USACO14DEC]驮运Piggy Back
传送门 做过次短路后,再来做这题感觉轻松不少. 这题看着就像最短路模板题. 思路: 虽说题目看起来比较水,但是码起来还是有点难度的.(对我这个蒟蒻来说) 这道题,跟"路障"一题差不 ...
- [USACO14DEC]驮运Piggy Back
题目描述 Bessie 和 Elsie在不同的区域放牧,他们希望花费最小的能量返回谷仓.从一个区域走到一个相连区域,Bessie要花费B单位的能量,Elsie要花费E单位的能量. 如果某次他们两走到同 ...
- 2018.08.17 洛谷P3110 [USACO14DEC]驮运(最短路)
传送门 一道sb最短路,从两个起点和终点跑一边最短路之后直接枚举两人的汇合点求最小值就行了. 代码: #include<bits/stdc++.h> #define N 40005 #de ...
- [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 & ...
- 洛谷 [P3110] 驮运
题目略带一点贪心的思想,先跑三遍最短路(边权为一,BFS比SPFA高效) 一起跑总比分开跑高效,枚举两人在何点汇合,输出最小值. #include <iostream> #include ...
- luogu P3111 [USACO14DEC]牛慢跑Cow Jog_Sliver |贪心+模拟
有N (1 <= N <= 100,000)头奶牛在一个单人的超长跑道上慢跑,每头牛的起点位置都不同.由于是单人跑道,所有他们之间不能相互超越.当一头速度快的奶牛追上另外一头奶牛的时候,他 ...
随机推荐
- Application Warm-up Module IIS7.5 也有Warm Up功能,让ASP.NET 第一次Request不变慢
Application Warm-up Module: 應用程式的暖機代理人 http://www.microsoft.com/taiwan/technet/iis/expand/Applicatio ...
- Unicode and .NET
http://csharpindepth.com/Articles/General/Unicode.aspx Scope of this page This is a big topic. Don't ...
- mysql中decimal的使用
float,double,decimal区别 创建表test_float_double_decimal CREATE TABLE `test_float_double_decimal` ( `id` ...
- luogu 3865 【模板】ST表
我太菜了 今天才学会现场脑补ST表静态RMQ #include<iostream> #include<cstdio> #include<algorithm> #in ...
- 将数据从数据仓库Hive导入到MySQL
1.启动Hadoop,hive,mysql 2.在mysql中建表(需要导入数据的) mysql> CREATE TABLE `dbtaobao`.`user_log` (`user_id` v ...
- 19.Extjs主页面显示js
1. /** * @author sux * @time 2011-1-11 * @desc main page */ var mainPage = Ext.extend(Ext.Viewport,{ ...
- WebService基于soapheader的身份验证
用WebService开发接口十分方便.但接口提供的数据不应是对所有人可见的,我们来利用SoapHeader写一个简单的身份验证Demo 目录 创建WebService项目(带SoapHeader) ...
- bzoj 1233: [Usaco2009Open]干草堆tower【dp+单调栈】
参考:https://www.cnblogs.com/N-C-Derek/archive/2012/07/11/usaco_09_open_tower.html 虽然长得很像斜率优化,但是应该不算-- ...
- hdu2033
http://acm.hdu.edu.cn/showproblem.php?pid=2033 1 #include<stdio.h> #include<string.h> #i ...
- Mysql阿里数据源配置参考
maven pom.xml配置 <dependency> <groupId>com.alibaba</groupId> <artifactId>drui ...