[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 <= i <= n)
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#define N 40001
#define LL long long
#define min(x, y) ((x) < (y) ? (x) : (y)) int B, E, P, n, m, cnt;
int dis[4][N], head[N], to[N << 1], next[N << 1];
bool vis[N];
std::queue <int> q;
LL ans = ~(1 << 31); inline int read()
{
int x = 0, f = 1;
char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
return x * f;
} inline void add(int x, int y)
{
to[cnt] = y;
next[cnt] = head[x];
head[x] = cnt++;
} inline void spfa(int s, int k)
{
int i, v, u;
memset(vis, 0, sizeof(vis));
memset(dis[k], 127, sizeof(dis[k]));
q.push(s);
dis[k][s] = 0;
while(!q.empty())
{
u = q.front();
q.pop();
vis[u] = 0;
for(i = head[u]; i ^ -1; i = next[i])
{
v = to[i];
if(dis[k][v] > dis[k][u] + 1)
{
dis[k][v] = dis[k][u] + 1;
if(!vis[v])
{
q.push(v);
vis[v] = 1;
}
}
}
}
} int main()
{
int i, x, y;
B = read();
E = read();
P = read();
n = read();
m = read();
memset(head, -1, sizeof(head));
for(i = 1; i <= m; i++)
{
x = read();
y = read();
add(x, y);
add(y, x);
}
spfa(1, 1);
spfa(2, 2);
spfa(n, 3);
for(i = 1; i <= n; i++)
ans = min(ans, (LL)(dis[1][i] * B + dis[2][i] * E + dis[3][i] * P));
printf("%lld\n", ans);
return 0;
}
[luoguP3110] [USACO14DEC]驮运Piggy Back(SPFA || BFS)的更多相关文章
- 洛谷P3110 [USACO14DEC]驮运Piggy Back
P3110 [USACO14DEC]驮运Piggy Back 题目描述 贝西和她的妹妹艾尔斯白天在不同的地方吃草,而在晚上他们都想回到谷仓休息.聪明的牛仔,他们想出了一个计划,以尽量减少他们在步行时花 ...
- 【题解】Luogu P3110 [USACO14DEC]驮运Piggy Back
[题解]Luogu P3110 [USACO14DEC]驮运Piggy Back 题目描述 Bessie and her sister Elsie graze in different fields ...
- [USACO14DEC]驮运Piggy Back
题目描述 Bessie 和 Elsie在不同的区域放牧,他们希望花费最小的能量返回谷仓.从一个区域走到一个相连区域,Bessie要花费B单位的能量,Elsie要花费E单位的能量. 如果某次他们两走到同 ...
- Luogu P3110 [USACO14DEC]驮运Piggy Back
解题思路 看到下面很多人都在说什么遇到了之后要不要背着走,其实根本不需要,同样的我也是跑了三遍$SPFA$,求出了以$1$为起点到个点的$dist$,和以$2$为起点到个点的$dist$,还有以$n$ ...
- P3110 [USACO14DEC]驮运Piggy Back
传送门 做过次短路后,再来做这题感觉轻松不少. 这题看着就像最短路模板题. 思路: 虽说题目看起来比较水,但是码起来还是有点难度的.(对我这个蒟蒻来说) 这道题,跟"路障"一题差不 ...
- luogu P3110 [USACO14DEC]驮运Piggy Back |最短路
题目描述 Bessie and her sister Elsie graze in different fields during the day, and in the evening they b ...
- 2018.08.17 洛谷P3110 [USACO14DEC]驮运(最短路)
传送门 一道sb最短路,从两个起点和终点跑一边最短路之后直接枚举两人的汇合点求最小值就行了. 代码: #include<bits/stdc++.h> #define N 40005 #de ...
- ACM/ICPC 之 最短路-Floyd+SPFA(BFS)+DP(ZOJ1232)
这是一道非常好的题目,融合了很多知识点. ZOJ1232-Adventrue of Super Mario 这一题折磨我挺长时间的,不过最后做出来非常开心啊,哇咔咔咔 题意就不累述了,注释有写,难点在 ...
- Educational Codeforces Round 54 (Rated for Div. 2) D Edge Deletion (SPFA + bfs)
题目大意:给定你一个包含n个点m条边的无向图,现在最多在图中保留k条边,问怎么删除多的边,使得图中良好的节点数最多,求出保留在图中的边的数量和编号. 良好的节点定义为:删除某条边后该点到点1的最短距离 ...
随机推荐
- awk累加
{a+=substr($14,1,1)}END{a=(a=="")?0:a;print a}' 对a进行累加,如果最后a=0的话,结果为0,否则为a,最后输出a
- SQL 数学串函数
数学函数 ceiling 取上限 floor 取下限 round 四舍五入 len 长度 abs 绝对值 PI()圆周率 sqrt 开根号 qwuare 平方根 select 10 ...
- Javascript的一些经验总结
JavaScript作用域 1.作用域 JavaScript的作用域与C.Java等语言不同,它不是以花括号包围的块级作用域,这个特性经常被大多数人忽视.例如下面代码,在大多数类C的语言中会出现变量未 ...
- php接受axios数据
var params = { username: 'admin', password: '123456' } axios.post('test.php', params).then(res => ...
- Spring中的事务传播行为与隔离级别
事务传播行为 事务传播行为(为了解决业务层方法之间互相调用的事务问题): 当事务方法被另一个事务方法调用时,必须指定事务应该如何传播.例如:方法可能继续在现有事务中运行,也可能开启一个新事务,并在自己 ...
- MAC进入文件夹快捷键
common + O common+up common+Down shift + common +G
- Python基础篇 -- 小数据池和再谈编码
小数据池 1. id() 通过id()可以查看到一个变量表示的值在内存中的地址 s = "Agoni" print(id(s)) # 2410961093272 2. is 和 = ...
- 五. web开发基础
一.HTML 二.CSS 三.JavaScript 四.web框架 1.web框架本质 众所周知,对于所有的Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端 ...
- C# 使用Epplus导出Excel [2]:导出动态列数据
C# 使用Epplus导出Excel [1]:导出固定列数据 C# 使用Epplus导出Excel [2]:导出动态列数据 C# 使用Epplus导出Excel [3]:合并列连续相同数据 C# 使用 ...
- c++ 递归求一个数的阶乘
#include <iostream> using namespace std; long factorial(int value); int main() { int value; co ...