Links: https://nanti.jisuanke.com/t/11217
The official:
Enumerationd (x, Y, z) < Span class= "Mclose" > in y< span class= "Strut" > , put y from this figure, Then the full source of the shortest possible, using the Floyd algorithm to do the above procedure < Span class= "base textstyle uncramped" >< Span class= "mpunct" > < Span class= "Strut bottom" >< Span class= "Katex-mathml" >.
< Span class= "base textstyle uncramped" >< Span class= "katex-html" >< The span class= "Mord mathit" >floyd algorithm can be an incremental process, although the first dimension is generally from 1k But the order of this enumeration does not affect the final result.
So if you can preprocess the y, only y does not have a matrix enumerated in the first dimension of Floyd, the value of this matrix is not < Span class= "base textstyle uncramped" >y Point's full source shortest path.
So using divide and conquer, each time the point set is split into two halves, first with the first half of the points in the Floyd algorithm roll, and then recursive half point.
Then backtrack, using the last half of the points in the Floyd algorithm roll, recursion to the first half of the point. So that each state with only one point gets the matrix that only this point does not roll in the Floyd algorithm.
Time complexity of O(n^? 3?? logn).
Spit Groove: Before writing this problem, CDQ division only wrote three-dimensional partial-order template problem, the whole two-part of the writing is very few, later should be more write some
#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<string>#include<stack>#include<vector>#include<map>#include<queue>#include<algorithm>#include<utility>using namespaceStd;typedefLong LongLL;Const intn=305;Const intinf=0x3f3f3f3f;intdp[ -][n][n],n,mp[n][n]; LL ret;voidCPYDP (intDEP) { for(intI=1; i<=n;++i) for(intj=1; j<=n;++j) Dp[dep][i][j]=dp[dep-1][i][j];}voidUpdateintDepintLintR) { for(intk=l;k<=r;++k) for(intI=1; i<=n;++i) for(intj=1; j<=n;++j)if(dp[dep][i][j]>dp[dep][i][k]+Dp[dep][k][j]) dp[dep][i][j]=dp[dep][i][k]+dp[dep][k][j];}voidCdqintDepintLintR) { if(l==R) { for(intI=1; i<=n;++i) for(intj=1; j<=n;++j) { if(i==l| | J==L)Continue; if(Dp[dep][i][j]==inf) dp[dep][i][j]=-1; RET+=1ll*Dp[dep][i][j]; } return; } intM=l+r>>1; CPYDP (DEP+1), Update (dep+1, m+1, R); CDQ (DEP+1, l,m); CPYDP (DEP+1), Update (dep+1, l,m); CDQ (DEP+1, m+1, R);}intMain () {scanf ("%d",&N); for(intI=1; i<=n;++i) for(intj=1; j<=n;++j) {scanf ("%d", &dp[0][i][j]); if(dp[0][i][j]==-1) dp[0][i][j]=INF; } CDQ (0,1, N); cout<<ret<<Endl; return 0;}
View Code
2016 Garlic Road FLOYD+CDQ Baidu Map of the real-time road traffic and sub-treatment