【题解】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 ...
随机推荐
- git详细使用教程
一:Git是什么? Git是目前世界上最先进的分布式版本控制系统. 二:SVN与Git的最主要的区别? SVN是集中式版本控制系统,版本库是集中放在中央服务器的,而干活的时候,用的都是自己的电脑,所以 ...
- 查看jar包内容
查看jar包内容 查看jar包内容的基本命令: jar tf jar-file 参数解释: The t option indicates that you want to view the table ...
- OCP
desc dba_objects; select * from dba_objects where rownum = 6; select owner, object_id from dba_objec ...
- Python3 install pip
原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/12033910.html curl https://bootstrap.pypa.io/get-pip. ...
- HTML中的marquee标签实现滚动效果
一.标签<marquee>简介 通过开始标签<marquee>和结束标签</marquee>的共同配合而实现滚动效果,<marquee>滚动的内容< ...
- 小波神经网络(WNN)
人工神经网络(ANN) 是对人脑若干基本特性通过数学方法进行的抽象和模拟,是一种模仿人脑结构及其功能的非线性信息处理系统. 具有较强的非线性逼近功能和自学习.自适应.并行处理的特点,具有良好的容错能力 ...
- Leetcode 10. Regular Expression Matching(递归,dp)
10. Regular Expression Matching Hard Given an input string (s) and a pattern (p), implement regular ...
- 【CF1247E】Rock Is Push(DP,二分)
题意:有一个n*m的方格,每一格可能为空也可能有石头,要从(1,1)走到(n,m),每次可以往右或往下走 每次走的时候都会将自己面前的所有石头向移动方向推一格,如果碰到了边界就推不过去 问方案数模1e ...
- 【转】Django框架请求生命周期
https://www.cnblogs.com/gaoya666/p/9100626.html 先看一张图吧! 1.请求生命周期 - wsgi, 他就是socket服务端,用于接收用户请求并将请求进行 ...
- Elasticsearch结构化搜索与查询
Elasticsearch 的功能之一就是搜索,搜索主要分为两种类型,结构化搜索和全文搜索.结构化搜索是指有关查询那些具有内在结构数据的过程.比如日期.时间和数字都是结构化的:它们有精确的格式,我们可 ...