当前位置:网站首页>倍增Floyd「建议收藏」
倍增Floyd「建议收藏」
2022-07-25 10:30:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
有这样的一道题:
给定一张图,求其中恰好经过 m m条边的路径的长度最小值。 (n<=200,m<=109) (n<=200,m<=10^9)
对于这种题型,可以使用倍增Floyd求解。
由于Floyd算法的奇特性质:每次加入一个点进行更新。如果我们把它改写为:
for(int i=0;i<=n;i++)
for(int j=0;j<=n;j++)
for(int k=0;k<=n;k++)
check(d[i][j],d[i][k]+d[k][j]);那么这得到的就是经过两条边的最短距离的,同样的,我们就可以将这个拓展为倍增,就可以解决这个问题了。附上部分代码。
(黄学长的代码写的真飘逸,学习了)
struct Floyd{
int d[N][N];
Floyd(){
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
d[i][j]=INF;
}
Floyd operator *(const Floyd &a)const{
Floyd res;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
for(int k=1;k<=n;k++)
check(res.d[i][j],d[i][k]+a.d[k][j]);
return res;
}
}ans,A;
void solve(){
Init();//A设为原图
while(m){
if(m&1)ans=ans*A;
A=A*A;
m>>=1;
}
}发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/127096.html原文链接:https://javaforall.cn
边栏推荐
猜你喜欢

HCIP(11)

mysql高级语句(一)(总有一个人的出现,让你的生活不再继续糟糕)

HCIA experiment (06)
Learn NLP with Transformer (Chapter 4)
Learn NLP with Transformer (Chapter 2)
Learn NLP with Transformer (Chapter 3)

AI system frontier dynamics issue 43: ONEFLOW V0.8.0 officially released; GPU finds human brain connections; AI doctoral online crowdfunding research topic

I, AI doctoral student, online crowdfunding research topic

SQL语言(一)

BGP联邦实验
随机推荐
AI系统前沿动态第43期:OneFlow v0.8.0正式发布;GPU发现人脑连接;AI博士生在线众筹研究主题
Syncronized lock upgrade process
tensorflow入门
代码的表示学习:CodeBERT及其他相关模型介绍
MLX90640 红外热成像仪测温模块开发笔记(五)
HCIP (01)
Learn NLP with Transformer (Chapter 4)
复习背诵整理版
BGP联邦实验
The University of Gottingen proposed clipseg: a model that can perform three segmentation tasks simultaneously using text and image prompts
BeautifulSoup的一些用法
Last week's hot review (7.18-7.24)
性能测试中TPS的计算【杭州多测师】【杭州多测师_王sir】
How to optimize the performance when the interface traffic increases suddenly?
Dataset and dataloader data loading
HCIA experiment (10) nat
30000 word express Servlet
【云享新鲜】社区周刊·Vol.72- 2022华为开发者大赛中国区首场开幕式启动;华为云KooMessage火热公测中…
HCIA experiment (07) comprehensive experiment
Flask framework - session and cookies