AcWing 345. 牛站 Cow Relays
由于我太菜了,不会矩阵乘法,所以给同样不会矩阵乘法同学的福利
首先发现这题点很多边很少,实际上有用的点 \(<= 2 * T\)(因为每条边会触及两个点嘛)
所以我们可以把点的范围缩到 \(2 * T\)来,然后...
算法1 Bellman - Ford O(NT)
什么,限制边数?那不就是可爱的 \(BellmanFord\)吗?
看看复杂度,嗯嗯 \(10 ^ 8\) 海星,常数超小的我肯定不用吸氧的
#pragma GCC optimize(2)
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
const int N = 205, M = 105;
struct Edge{
int u, v, w;
}e[M];
int m, n, s, t, adj[N], dis[N], bDis[N], tot;
void inline read(int &x) {
x = 0;
char s = getchar();
while(s > '9' || s < '0') s = getchar();
while(s <= '9' && s >= '0') x = x * 10 + s - '0', s = getchar();
}
int inline get(int &x) {
return lower_bound(adj + 1, adj + 1 + tot, x) - adj;
}
int inline bellmanFord(){
memset(dis, 0x3f, sizeof dis);
dis[s] = 0;
for(register int i = 1; i <= n; i++){
memcpy(bDis, dis, sizeof dis);
memset(dis, 0x3f, sizeof dis);
for(register int j = 1; j <= m; j++){
dis[e[j].v] = min(dis[e[j].v], bDis[e[j].u] + e[j].w);
dis[e[j].u] = min(dis[e[j].u], bDis[e[j].v] + e[j].w);
}
}
return dis[t];
}
int main(){
read(n); read(m); read(s); read(t);
for (register int i = 1; i <= m; i++) {
read(e[i].w); read(e[i].u); read(e[i].v);
adj[++tot] = e[i].u;
adj[++tot] = e[i].v;
}
sort(adj + 1, adj + 1 + tot);
tot = unique(adj + 1, adj + 1 + tot) - adj - 1;
for (register int i = 1; i <= m; i++) {
e[i].u = get(e[i].u), e[i].v = get(e[i].v);
}
s = get(s), t = get(t);
printf("%d\n", bellmanFord());
return 0;
}
真香
算法2 倍增 + Floyd O(T ^ 3 * log_2N)
据说这题正解要用矩阵乘法,可我不会,咋办呢?
不如用倍增的思想,把\(N\)拆成二进制下的多个\(1\),我们把每个\('1'\)最短路搞出来,然后拼出来最终的最短路,先预处理:
\(d[i][j][l]\) 表示从 \(i\) 到 \(j\) 恰好经过 \(2 ^ l\) 条边的最短路。
初始化 \(d[i][j][0] = w[i][j]\),剩下为正无穷(注意是恰好 \(N\) 条边,所以 \(d[i][i][0]\) 也是非法状态)
转移也很好想:
\(d[i][j][l] = min(d[i][k][l - 1] + d[k][j][l - 1])\),对于一个状态 \(d[i][j][l]\),枚举中间点 \(k\) 即可,所以预处理复杂度 \(O(T ^ 3 * log_2N)\)
接下来用二进制拼起来就行辣~,设 \(g[i]\) 为这前几部走完后,从 \(s\) 到 \(i\) 的最短路, \(f[i]\) 为当前到 \(i\) 的最短路,与保卫王国的拼凑法思想差不多,即:
\(f[i] = min(g[j] + d[j][i][c])\) 若 \(N\) 的二进制第 \(c\) 位为 \(1\)。
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
const int N = 205, M = 105;
struct Edge{
int u, v, w;
}e[M];
int m, n, s, t, adj[N], tot, d[N][N][20], f[N], g[N];
int L;
int inline get(int x) {
return lower_bound(adj + 1, adj + 1 + tot, x) - adj;
}
int main(){
memset(d, 0x3f, sizeof d);
scanf("%d%d%d%d", &n, &m, &s, &t);
L = log2(n);
for (int i = 1; i <= m; i++) {
scanf("%d%d%d", &e[i].w, &e[i].u, &e[i].v);
adj[++tot] = e[i].u;
adj[++tot] = e[i].v;
}
sort(adj + 1, adj + 1 + tot);
tot = unique(adj + 1, adj + 1 + tot) - adj - 1;
for (int i = 1; i <= m; i++) {
int u = get(e[i].u), v = get(e[i].v), w = e[i].w;
d[u][v][0] = d[v][u][0] = min(d[u][v][0], w);
}
s = get(s), t = get(t);
for (int c = 1; c <= L; c++) {
for (int i = 1; i <= tot; i++) {
for (int j = 1; j <= tot; j++) {
for (int k = 1; k <= tot; k++) {
d[i][j][c] = min(d[i][j][c], d[i][k][c - 1] + d[k][j][c - 1]);
}
}
}
}
memset(g, 0x3f, sizeof g);
g[s] = 0;
for (int c = 0; c <= L; c++) {
if(n >> c & 1) {
memset(f, 0x3f, sizeof f);
for (int i = 1; i <= tot; i++)
for (int j = 1; j <= tot; j++)
f[i] = min(f[i], g[j] + d[j][i][c]);
memcpy(g, f, sizeof g);
}
}
printf("%d\n", f[t]);
return 0;
}
AcWing 345. 牛站 Cow Relays的更多相关文章
- P2886 [USACO07NOV]牛继电器Cow Relays
题目描述 For their physical fitness program, N (2 ≤ N ≤ 1,000,000) cows have decided to run a relay race ...
- [洛谷P2886] 牛继电器Cow Relays
问题描述 For their physical fitness program, N (2 ≤ N ≤ 1,000,000) cows have decided to run a relay race ...
- [USACO07NOV]牛继电器Cow Relays
题目描述 给出一张无向连通图,求S到E经过k条边的最短路. 输入输出样例 输入样例#1: 2 6 6 4 11 4 6 4 4 8 8 4 9 6 6 8 2 6 9 3 8 9 输出样例#1: 10 ...
- 洛谷P2886 [USACO07NOV]牛继电器Cow Relays
题意很简单,给一张图,把基本的求起点到终点最短路改成求经过k条边的最短路. 求最短路常用的算法是dijkstra,SPFA,还有floyd. 考虑floyd的过程: c[i][j]=min(c[i][ ...
- Luogu 2886 [USACO07NOV]牛继电器Cow Relays
BZOJ 1706权限题. 倍增$floyd$. 首先这道题有用的点最多只有$200$个,先离散化. 设$f_{p, i, j}$表示经过$2^p$条边从$i$到$j$的最短路,那么有转移$f_{p, ...
- [USACO07NOV]牛继电器Cow Relays (最短路,DP)
题目链接 Solution 非正解 似乎比较蛇啊,先个一个部分分做法,最短路+\(DP\). 在求最短路的堆或者队列中存储元素 \(dis_{i,j}\) 代表 \(i\) 这个节点,走了 \(j\) ...
- 洛谷 [P2886] 牛继电器Cow Relays
最短路 + 矩阵快速幂 我们可以改进矩阵快速幂,使得它适合本题 用图的邻接矩阵和快速幂实现 注意 dis[i][i] 不能置为 0 #include <iostream> #include ...
- [LUOGU] P2886 [USACO07NOV]牛继电器Cow Relays
https://www.luogu.org/problemnew/show/P2886 给定无向连通图,求经过k条边,s到t的最短路 Floyd形式的矩阵乘法,同样满足结合律,所以可以进行快速幂. 离 ...
- [luoguP2886] [USACO07NOV]牛继电器Cow Relays(矩阵)
传送门 矩阵快速幂,本质是floyd 把 * 改成 + 即可 注意初始化 因为只有100条边,所以可以离散化 #include <cstdio> #include <cstring& ...
随机推荐
- 剑指offer刷题(算法类_2)
排序 035-数组中的逆序对(归并排序) 题目描述 题解 代码 复杂度 029-最小的K个数(堆排序) 题目描述 题解 代码 复杂度 029-最小的K个数(快速排序) 题目描述 题解 代码 复杂度 位 ...
- 慢话crush-各种crush组合
前言 ceph已经是一个比较成熟的开源的分布式存储了,从功能角度上来说,目前的功能基本能够覆盖大部分场景,而社区的工作基本上是在加入企业级的功能和易用性还有性能等方面在发力在,不管你是新手还是老手,都 ...
- Ceph Bluestore首测
Bluestore 作为 Ceph Jewel 版本推出的一个重大的更新,提供了一种之前没有的存储形式,一直以来ceph的存储方式一直是以filestore的方式存储的,也就是对象是以文件方式存储在o ...
- 分布式 task_master / task_worker
17:08:0317:08:04 在Thread(线程)和Process(进程)中,应当优选Process,因为Process更稳定,而且,Process可以分布到多台机器上,而Thread最多只能分 ...
- 2、Spring Boot配置
1.配置文件 SpringBoot使用一个全局的配置文件,配置文件名是固定的: •application.properties •application.yml 配置文件的作用:修改SpringBoo ...
- tp5 统一返回json格式
控制器调用 public function json(){ if (request()->isPost()) { return jsonData(1,'转换成功',数据(可不填)); } } 公 ...
- tp5 删除图片以及文件
控制器调用 /** * [delimg 删除单张图片] * @return [type] [description] */ public function delimg(){ if (request( ...
- Guitar Pro 功能介绍之RSE引擎
众所周知,Guitar Pro是一个集多音轨丰富音色库的一款软件,并且拥有MIDI 音序器,但是光拥有MIDI是无法让我们制作的音乐可以如此逼真,而Guitar Pro能创造制作这么逼真的音乐,多亏了 ...
- 使用Mac清理工具CleanMyMac对Mac电脑进行维护
CleanMyMac是Mac系统下的一款苹果电脑清理软件,同时也是一款优秀的电脑维护软件,它能通过用户手动运行CleanMyMac内置脚本文件,释放电脑内存,帮助电脑缓解卡顿现象,保证电脑的良好持续运 ...
- 从维基百科等网站复制公式到MathType中
在写论文的时候你会想要一些比书本上更好的实例,所以你会在网上寻找资源.当你发现一个你想要的公式时,发现网页公式复制粘贴后太模糊而不适合打印或者投影.这种问题在MathType中如何解决呢? 你可以将网 ...