题意

给出一张无向图,求出恰巧经过n条边的最短路。

题解

考虑先离散化,那么点的个数只会有202个最多。于是复杂度里面就可以有一个\(n^3\).考虑构造矩阵\(d^1\)表示经过一条边的最短路,那么就会是输入进来的边。那么\(d^k\)表示经过k条边的最短路,则有\(d^k[i][j] = min\{d^k[i][j], d^r[i][k] + d^{k-r}[k][j]\}\)

这玩意其实就是个矩阵乘法的拓展,可以说是广义上的矩阵乘法。

然后就可以处理出2^x次方的所有矩阵,然后\(n^3logn\)得到最终矩阵的答案了。这个算法叫做倍增floyd。

#include <cstdio>
#include <algorithm>
#include <cstring>
#define ll long long
#define inf 0x3f3f3f3f
#define il inline namespace io { #define in(a) a = read()
#define out(a) write(a)
#define outn(a) out(a), putchar('\n') #define I_int ll
inline I_int read() {
I_int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x * f;
}
char F[200];
inline void write(I_int x) {
if (x == 0) return (void) (putchar('0'));
I_int tmp = x > 0 ? x : -x;
if (x < 0) putchar('-');
int cnt = 0;
while (tmp > 0) {
F[cnt++] = tmp % 10 + '0';
tmp /= 10;
}
while (cnt > 0) putchar(F[--cnt]);
}
#undef I_int }
using namespace io; using namespace std; #define N 210 int k,m,s,e;
int x[10*N], y[10*N], v[10*N], a[10*N], lim; struct mat {
int m[N][N];
mat() { memset(m, 0x3f, sizeof(m)); }
mat operator * (const mat x) const {
mat c;
for(int k = 1; k <= lim; ++k) {
for(int i = 1; i <= lim; ++i) {
for(int j = 1; j <= lim; ++j) {
c.m[i][j] = min(c.m[i][j], m[i][k] + x.m[k][j]);
}
}
}
return c;
}
}d[30]; int vis[N*50];
int main() {
in(k);in(m);in(s);in(e); int tot = 0;
a[++tot] = s; a[++tot] = e;
for(int i = 1; i <= m; ++i) {
in(v[i]), in(x[i]), in(y[i]);
a[++tot] = x[i]; a[++tot] = y[i];
}
sort(a+1,a+tot+1);
for(int i = 1; i <= tot; ++i)
if(a[i] != a[i - 1]) vis[a[i]] = ++lim;
s = vis[s]; e = vis[e];
for(int i = 1; i <= m; ++i) {
x[i] = vis[x[i]]; y[i] = vis[y[i]];
d[0].m[x[i]][y[i]] = d[0].m[y[i]][x[i]] = v[i];
}
for(int i = 1; (1 << i) <= k; ++i) d[i] = d[i - 1] * d[i - 1];
mat ans;
for(int i = 1; i <= lim; ++i) ans.m[i][i] = 0;
for(int i = 0; (1 << i) <= k; ++i) if((k>>i)&1) ans = ans * d[i];
outn(ans.m[s][e]);
}

【bzoj1706】[usaco2007 Nov]relays 奶牛接力跑的更多相关文章

  1. bzoj1706: [Usaco2007 Nov]relays 奶牛接力跑 (Floyd+新姿势)

    题目大意:有t(t<=100)条无向边连接两点,求s到e刚好经过n(n<=10^7)条路径的最小距离. 第一反应分层图,但是一看n就懵逼了,不会写.看了题解之后才知道可以这么玩... 首先 ...

  2. [bzoj1706] [usaco2007 Nov]relays 奶牛接力跑

    大概是叫倍增Floyd? 显然最多200个点...f[i][j][k]表示从j到k,走2^i步的最小路程.就随便转移了.. 查询的话就是把n二进制位上是1的那些都并起来. #include<cs ...

  3. bzoj1706 [usaco2007 Nov]relays 奶牛接力跑 矩阵快速幂

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=1706 题解 换个方法定义矩阵乘法:先加再取 \(\min\). 对于一个 \(n\times ...

  4. 【BZOJ1706】[usaco2007 Nov]relays 奶牛接力跑 矩阵乘法

    [BZOJ1706][usaco2007 Nov]relays 奶牛接力跑 Description FJ的N(2 <= N <= 1,000,000)头奶牛选择了接力跑作为她们的日常锻炼项 ...

  5. BZOJ_[usaco2007 Nov]relays 奶牛接力跑_离散化+倍增弗洛伊德

    BZOJ_[usaco2007 Nov]relays 奶牛接力跑_离散化+倍增弗洛伊德 Description FJ的N(2 <= N <= 1,000,000)头奶牛选择了接力跑作为她们 ...

  6. 【bzoj1706】[usaco2007 Nov]relays 奶牛接力跑 离散化+倍增Floyd

    题目描述 FJ的N(2 <= N <= 1,000,000)头奶牛选择了接力跑作为她们的日常锻炼项目.至于进行接力跑的地点 自然是在牧场中现有的T(2 <= T <= 100) ...

  7. BZOJ 1706: [usaco2007 Nov]relays 奶牛接力跑

    Description FJ的N(2 <= N <= 1,000,000)头奶牛选择了接力跑作为她们的日常锻炼项目.至于进行接力跑的地点 自然是在牧场中现有的T(2 <= T < ...

  8. bzoj 1706: [usaco2007 Nov]relays 奶牛接力跑——倍增floyd

    Description FJ的N(2 <= N <= 1,000,000)头奶牛选择了接力跑作为她们的日常锻炼项目.至于进行接力跑的地点 自然是在牧场中现有的T(2 <= T < ...

  9. 【BZOJ】1706: [usaco2007 Nov]relays 奶牛接力跑

    [题意]给定m条边的无向图,起点s,终点t,要求找出s到t恰好经过n条边的最短路径.n<=10^6,m<=100. [算法]floyd+矩阵快速幂 [题解] 先对点离散化,得到点数N. 对 ...

随机推荐

  1. 【2017-03-13】Tsql 表连接

    笛卡尔积          穷举 在未建立连接的情况下,将car表的name列和brand表的brand_name列进行笛卡尔积查询后,实际是将两列相乘,进行穷举,列举出所有可能性 表连接:将多个表不 ...

  2. Vue:将px转化为rem,适配移动端vant-UI等框架(px2rem-loader)

    转载:https://www.cnblogs.com/WQLong/p/7798822.html 1.下载lib-flexible 使用的是vue-cli+webpack,通过npm来安装的 npm ...

  3. Linux基础命令---防火墙iptables

    iptables iptables指令用来设置Linux内核的ip过滤规则以及管理nat功能.iptables用于在Linux内核中设置.维护和检查IPv4数据包过滤规则表.可以定义几个不同的表.每个 ...

  4. Node.js中环境变量process.env详解

    Node.js中环境变量process.env详解process | Node.js API 文档http://nodejs.cn/api/process.html官方解释:process 对象是一个 ...

  5. Javascript创建类的七种方法

    /* 第一种定义类的方法 */var cls = new Object();cls.name = "wyf";cls.showName = function(){console.l ...

  6. centos 安装arcgis server 10.1

    1.创建新用户,不要在root下面直接安装 [root@localhost ~]# groupadd esri //创建esri组 [root@localhost ~]# useradd ags -g ...

  7. 基于jquery、bootstrap的数据验证插件bootstrapValidator使用

    实时验证用户名是否存在,密码不能和用户名相同,两次密码需要相同,提交之后需要验证返回值: <form id="defaultForm" role="form&quo ...

  8. codeSourcery交叉编译环境

    arm-none-Linux-gnueabi-gcc是 Codesourcery 公司(目前已经被Mentor收购)基于GCC推出的的ARM交叉编译工具.可用于交叉编译ARM系统中所有环节的代码,包括 ...

  9. react复习总结(2)--react生命周期和组件通信

    这是react项目复习总结第二讲, 第一讲:https://www.cnblogs.com/wuhairui/p/10367620.html 首先我们来学习下react的生命周期(钩子)函数. 什么是 ...

  10. P3366 【模板】最小生成树(boruvka/sollin)

    P3366 [模板]最小生成树 boruvka/sollin 复杂度$O(mlogn)$ 简要说明一下过程 引入一个数组$link[i]$表示连通块$i$下一步可更新的最短的边的编号 1.每次枚举所有 ...