【题解】Cow Relays
题目大意
求在一张有\(m\)条边无向连通图中,点\(s\)到点\(t\)的经过\(k\)条边的最短路(\(1 \leq m \leq 100\),\(1 \leq k \leq 10^6\))。
题解
边数\(m\)很小,显然点数\(n\)也很小,不超过\(200\),我们可以先离散化处理。
我们可以得到\(n\)个点之间的邻接矩阵,其中矩阵中的\(a[i][j]\)就相当于点\(i\)到点\(j\)的经过\(1\)条边的最短路。
此时我们设\(dp[i][j][k']\)表示点\(i\)到点\(j\)的经过\(k'\)条边的最短路,显然有
\]
且
\]
显然,朴素的dp复杂度为\(O(kn^2)\)。
观察这个过程,其实很像矩阵乘法,同样的,我们也可以用矩阵快速幂来优化,这样复杂度就降到$O(n^2 \log{k}) $了。
#include <iostream>
#include <cstring>
#include <algorithm>
#define MAX_M (100 + 5)
using namespace std;
int K, m, s, t;
int u[MAX_M], v[MAX_M];
long long w[MAX_M];
int tmp[MAX_M << 1];
int to[1000005], n;
struct Matrix
{
long long mat[MAX_M][MAX_M];
Matrix()
{
memset(mat, 0x3f, sizeof mat);
return;
}
friend Matrix operator * (Matrix a, Matrix b)
{
Matrix c;
for(int i = 1; i <= n; ++i)
{
for(int j = 1; j <= n; ++j)
{
for(int k = 1; k <= n; ++k)
{
c.mat[i][j] = min(c.mat[i][j], a.mat[i][k] + b.mat[k][j]);
}
}
}
return c;
}
friend Matrix operator *= (Matrix & a, Matrix b)
{
return a = a * b;
}
};
Matrix a;
int main()
{
cin >> K >> m >> s >> t;
for(int i = 1; i <= m; ++i)
{
cin >> w[i] >> u[i] >> v[i];
tmp[i << 1 ^ 1] = u[i];
tmp[i << 1] = v[i];
}
sort(tmp + 1, tmp + m + m + 1);
for(int i = 1; i <= (m << 1); ++i)
{
if(tmp[i] != tmp[i - 1]) to[tmp[i]] = ++n;
}
for(int i = 1; i <= m; ++i)
{
u[i] = to[u[i]];
v[i] = to[v[i]];
a.mat[u[i]][v[i]] = min(a.mat[u[i]][v[i]], w[i]);
a.mat[v[i]][u[i]] = min(a.mat[v[i]][u[i]], w[i]);
}
--K;
Matrix res = a;
while(K)
{
if(K & 1) res *= a;
a *= a;
K >>= 1;
}
cout << res.mat[to[s]][to[t]];
return 0;
}
【题解】Cow Relays的更多相关文章
- 2021.11.03 P2886 [USACO07NOV]Cow Relays G(矩阵+floyed)
2021.11.03 P2886 [USACO07NOV]Cow Relays G(矩阵+floyed) [P2886 USACO07NOV]Cow Relays G - 洛谷 | 计算机科学教育新生 ...
- poj 3613 Cow Relays
Cow Relays Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5411 Accepted: 2153 Descri ...
- POJ3613 Cow Relays [矩阵乘法 floyd类似]
Cow Relays Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7335 Accepted: 2878 Descri ...
- poj3613 Cow Relays【好题】【最短路】【快速幂】
Cow Relays Time Limit: 1000MS Memory Limit: 65536K Total Submissions:9207 Accepted: 3604 Descrip ...
- Cow Relays 【优先队列优化的BFS】USACO 2001 Open
Cow Relays Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tota ...
- poj3613:Cow Relays(倍增优化+矩阵乘法floyd+快速幂)
Cow Relays Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7825 Accepted: 3068 Descri ...
- 「POJ3613」Cow Relays
「POJ3613」Cow Relays 传送门 就一个思想:\(N\) 遍 \(\text{Floyd}\) 求出经过 \(N\) 个点的最短路 看一眼数据范围,想到离散化+矩阵快速幂 代码: #in ...
- Poj 3613 Cow Relays (图论)
Poj 3613 Cow Relays (图论) 题目大意 给出一个无向图,T条边,给出N,S,E,求S到E经过N条边的最短路径长度 理论上讲就是给了有n条边限制的最短路 solution 最一开始想 ...
- USACO07NOV Cow Relays G 题解
题目 For their physical fitness program, \(N (2 ≤ N ≤ 1,000,000)\) cows have decided to run a relay ra ...
随机推荐
- <el-menu>菜单标签(里面可以包括:<el-submenu>和<el-menu-item>)
<el-menu> 1.router属性,若使用router属性menu-item的index将对应router的path属性 2.mode,下拉菜单的模式分为horizontal和ver ...
- LeetCode--096--不同的二叉搜索树(python)
我的思路比较low直接看官方题解吧... class Solution: def numTrees(self, n: int) -> int: G = [0] * (n+1) G[0],G[1] ...
- Linux学习-通过loganalyzer展示MySQL中rsyslog日志
一.实验环境 系统:CentOS7.6 软件包:apache,php,mariadb-server (都是基于光盘yum源) 源码包:loganalyzer-4.1.7.tar.gz (http:// ...
- APP功能测试注意点
App功能测试的7大注意点 : APP测试 在日常工作的摸索中,我们将如何做好app测试的注意点简单归结为如下内容. 弱网测试,兼容性测试,UI测试.中断测试, 01 运行 1)App安装完成后 ...
- wepy 中文乱码
<view class="loginTitle"> <view class="{{currentTab==0?'select':'default'}}& ...
- webstorm注册码,亲测2016.1.1版
打开webstorm,点击帮助,注册 注册时,在打开的License Activation窗口中选择“License server”,在输入框输入下面的网址: http://idea.iteblog. ...
- Bugku web web2
web2 打开后发现是个大滑稽啊!F12检查元素拿到flag
- 软件安装——internal error2503/2502
安装新的软件后先报internal error 2503,随后报internal error 2502.就是不让我装新的软件,提示说发生严重错误,然后安装失败. Solution for intern ...
- UVALive 6859 Points (凸包)
Points 题目链接: http://acm.hust.edu.cn/vjudge/contest/130303#problem/E Description http://7xjob4.com1.z ...
- [CSP-S模拟测试]:方程的解(小学奥数)
题目描述 给出一个二元一次方程$ax+by=c$,其中$x$.$y$是未知数,求它的正整数解的数量. 输入格式 第一行一个整数$T$,表示有$T$组数据.接下来$T$行,每行$3$个整数$a$.$b$ ...