当前位置:网站首页>AcWing 345. Cattle station solution (nature and multiplication of Floyd)
AcWing 345. Cattle station solution (nature and multiplication of Floyd)
2022-07-07 01:36:00 【Mr. Qiao Da】
AcWing 345. Niu station
Their thinking : Used by the body floyd And find the shortest floyd Different in nature , Although they are all triple cycles , But in this question d[k][i]j] Indicates that the i~j after k The shortest distance of the edge , After confirming this , Further push d[a+b][i][j] = min(d[a+b][i][j],d[a][i]k] + d[b][k][j]), because d[a][i]k] and d[b][k][j] They do not interact with each other , So we can think of the shortest distance obtained by fast power
Far away solution
#include<bits/stdc++.h>
using namespace std;
const int N = 210;
map<int, int>mp;
int n, m, S, E, k;
int g[N][N]; //g[i][j] Store i~j The shortest distance across an edge
int res[N][N]; // After fast exponentiation, storage passes k This cycle is k Value of the edge
void mul(int c[][N], int a[][N], int b[][N]){
static int temp[N][N];// Create a new array , Make the state not inherited
memset(temp, 0x3f, sizeof temp);
for(int k = 1; k <= n; k ++ ){
for(int i = 1; i <= n; i ++ ){
for(int j = 1; j <= n; j ++ ){
temp[i][j] = min(temp[i][j], a[i][k] + b[k][j]);
}
}
}
memcpy(c, temp, sizeof temp);
}
void qmi(){
// Fast power
memset(res, 0x3f, sizeof res); // Initialize array ( Equivalent to initializing the distance array )
for(int i = 1; i <= n; i ++ ) res[i][i] = 0; //res It's the shortest distance , So initialize to 0
//res[x][y] The record is x To y Distance of , Cycle through k After times, it will pass k The distance between the edges
while(k){
// The first cycle passes through an edge , The second cycle passes through two sides , The first k The second cycle is through k side
if(k & 1) mul(res, res, g); // res = res * g
mul(g, g, g); // g = g * g Give Way g The weight on the array keeps accumulating 1->2->4->8
k >>= 1;
}
}
int main()
{
cin>>k>>m>>S>>E;
// The graph contains m side , From the starting point S To the end point E, after k The shortest path of repeatable edges
memset(g, 0x3f, sizeof g); //g It is the shortest distance to determine that there is at least one way to go , So it is not initialized to 0
// Right starting point 、 End point discretization
if(!mp.count(S)) mp[S] = ++ n;
if(!mp.count(E)) mp[E] = ++ n;
S = mp[S], E = mp[E];
while(m -- ){
int a, b, c;
cin>>c>>a>>b;
if(!mp.count(a)) mp[a] = ++ n;
if(!mp.count(b)) mp[b] = ++ n;
a = mp[a], b = mp[b];
g[a][b] = g[b][a] = min(g[a][b], c); //g[x][y] What is recorded here is from x To y The distance recorded through an edge
}
qmi();
cout<<res[S][E]<<endl;
return 0;
}
边栏推荐
- What are the differences between Oracle Linux and CentOS?
- 图片打水印 缩放 和一个输入流的转换
- ZOJ Problem Set – 2563 Long Dominoes 【如压力dp】
- AcWing 1148. 秘密的牛奶运输 题解(最小生成树)
- AI 从代码中自动生成注释文档
- Appium基础 — Appium Inspector定位工具(一)
- Appium automation test foundation uiautomatorviewer positioning tool
- LeetCode:1175. 质数排列
- Taro2.* applet configuration sharing wechat circle of friends
- 增加 pdf 标题浮窗
猜你喜欢
Appium foundation - appium inspector positioning tool (I)
如何管理分布式团队?
Transplant DAC chip mcp4725 to nuc980
AcWing 345. 牛站 题解(floyd的性质、倍增)
AI automatically generates annotation documents from code
How to manage distributed teams?
Transformation transformation operator
Instructions for using the domain analysis tool bloodhound
js如何快速创建一个长度为 n 的数组
移植DAC芯片MCP4725驱动到NUC980
随机推荐
454-百度面经1
图片打水印 缩放 和一个输入流的转换
1123. The nearest common ancestor of the deepest leaf node
分享一个通用的so动态库的编译方法
swiper组件中使用video导致全屏错位
AcWing 361. 观光奶牛 题解(spfa求正环)
增加 pdf 标题浮窗
Match VIM from zero (0) -- Introduction to vimscript
Dark horse notes - create immutable sets and streams
安全保护能力是什么意思?等保不同级别保护能力分别是怎样?
AcWing 345. 牛站 题解(floyd的性质、倍增)
一起看看matlab工具箱内部是如何实现BP神经网络的
MySQL script batch queries all tables containing specified field types in the database
736. Lisp 语法解析 : DFS 模拟题
Comparison of picture beds of free white whoring
736. LISP syntax parsing: DFS simulation questions
golang 基础 —— 数据类型
Taro applet enables wxml code compression
mongodb查看表是否导入成功
AcWing 904. 虫洞 题解(spfa求负环)