当前位置:网站首页>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;
}
边栏推荐
- AcWing 1140. 最短网络 (最小生成树)
- Neon Optimization: About Cross access and reverse cross access
- Drag to change order
- Asset security issues or constraints on the development of the encryption industry, risk control + compliance has become the key to breaking the platform
- 子网划分、构造超网 典型题
- 2022 Google CTF SEGFAULT LABYRINTH wp
- Neon Optimization: summary of performance optimization experience
- Appium自动化测试基础 — uiautomatorviewer定位工具
- POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)
- 一起看看matlab工具箱内部是如何实现BP神经网络的
猜你喜欢

ClickHouse字段分组聚合、按照任意时间段粒度查询SQL

1123. 最深叶节点的最近公共祖先

Yunna | work order management measures, how to carry out work order management

Transformation transformation operator

云呐|工单管理软件,工单管理软件APP

黑马笔记---异常处理

LeetCode. 剑指offer 62. 圆圈中最后剩下的数

Can the system hibernation file be deleted? How to delete the system hibernation file

2022 Google CTF SEGFAULT LABYRINTH wp
![[advanced C language] 8 written questions of pointer](/img/d4/c9bb2c8c9fd8f54a36e463e3eb2fe0.png)
[advanced C language] 8 written questions of pointer
随机推荐
C # method of calculating lunar calendar date 2022
Add the applet "lazycodeloading": "requiredcomponents" in taro,
grep查找进程时,忽略grep进程本身
Let's see through the network i/o model from beginning to end
Asset security issues or constraints on the development of the encryption industry, risk control + compliance has become the key to breaking the platform
Taro applet enables wxml code compression
Taro2.* 小程序配置分享微信朋友圈
Metauniverse urban legend 02: metaphor of the number one player
AcWing 904. 虫洞 题解(spfa求负环)
2022 Google CTF segfault Labyrinth WP
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
Appium自动化测试基础 — uiautomatorviewer定位工具
C语言实例_2
[chip scheme design] pulse oximeter
Body mass index program, entry to write dead applet project
THREE.AxesHelper is not a constructor
Taro 小程序开启wxml代码压缩
增加 pdf 标题浮窗
Comparison of picture beds of free white whoring
Transplant DAC chip mcp4725 to nuc980