当前位置:网站首页>[Floyd] post disaster reconstruction
[Floyd] post disaster reconstruction
2022-07-02 01:27:00 【Code chess】
1️⃣ Title Description
Topic link :
https://www.luogu.com.cn/problem/P1119

The conscience of this problem is that the reconstruction time is sorted from small to large , And the reconstruction time of inquiry is also sorted from small to large , So we can operate forward , There is no need to sort .
2️⃣ Simple ideas
The main problem is Floyd An understanding and practice of the essence of Algorithm .
Floyd In the algorithm k It means Intermediate nodes , Taking this as the intermediate node can update the shortest distance between two points .
f [ i ] [ j ] f[i][j] f[i][j]: It stands for i To j The shortest distance
Whenever an inquiry is made , If a village is rebuilt before the time point asked , Take this village as the transit node , Update the shortest distance of all points .
Updated code :
while(a[k] <= t && k < n)
{
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
{
if(f[i][k] + f[k][j] < f[i][j])
f[j][i] = f[i][j] = f[i][k] + f[k][j];
}
k ++;
}
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
vector<int> a(n);
for(int i = 0; i < n; i++) cin >> a[i];
vector f(n, vector(n, 1e9));
for(int i = 0; i < n; i++) f[i][i] = 0;
for(int i = 1; i <= m; i++)
{
int a, b, c;
cin >> a >> b >> c;
f[a][b] = f[b][a] = c;
}
int q;
int k = 0;
cin >> q;
while(q--)
{
int x, y, t;
cin >> x >> y >> t;
while(a[k] <= t && k < n)
{
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
{
if(f[i][k] + f[k][j] < f[i][j])
f[j][i] = f[i][j] = f[i][k] + f[k][j];
}
k ++;
}
if(a[x] > t || a[y] > t) cout << -1 << "\n";
else
{
if(f[x][y] == 1e9) cout << -1 << "\n";
else cout << f[x][y] << "\n";
}
}
return 0;
}
边栏推荐
- Docker安装Oracle_11g
- 6-3 vulnerability exploitation SSH environment construction
- Global and Chinese markets of edge AI software 2022-2028: Research Report on technology, participants, trends, market size and share
- Have you stepped on the nine common pits in the e-commerce system?
- MySQL application day02
- Data visualization in medical and healthcare applications
- Memorabilia of domestic database in June 2022
- [Obsidian] wechat is sent to Obsidian using remotely save S3 compatibility
- ACM教程 - 快速排序(常规 + 尾递归 + 随机基准数)
- GL Studio 5 installation and experience
猜你喜欢

LeetCode、3无重复最长子序列

The first "mobile cloud Cup" empty publicity meeting, looking forward to working with developers to create a new world of computing!

Datawhale 社区黑板报(第1期)

Datawhale community blackboard newspaper (issue 1)

【图像增强】基于Frangi滤波器实现血管图像增强附matlab代码

电商系统中常见的9大坑,你踩过没?
![Private project practice sharing [Yugong series] February 2022 U3D full stack class 009 unity object creation](/img/eb/b1382428d6578b8561d7fcc1a2a5cd.jpg)
Private project practice sharing [Yugong series] February 2022 U3D full stack class 009 unity object creation

学习笔记3--高精度地图关键技术(上)

Learning notes 25 - multi sensor front fusion technology

2022年6月国产数据库大事记
随机推荐
CEPH buffer yyds dry inventory
Global and Chinese markets for freight and logistics 2022-2028: Research Report on technology, participants, trends, market size and share
Leetcode, 3 repeatless longest subsequence
Global and Chinese market of ancillary software 2022-2028: Research Report on technology, participants, trends, market size and share
uTools
Luogu p1775 stone merger (weakened version)
首场“移动云杯”空宣会,期待与开发者一起共创算网新世界!
学习笔记25--多传感器前融合技术
Global and Chinese markets for maritime services 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese markets for distributed generation and energy storage in telecommunications networks 2022-2028: Research Report on technology, participants, trends, market size and share
游戏思考15:全区全服和分区分服的思考
Self drawing of menu items and CListBox items
Convolutional neural network (including code and corresponding diagram)
The technology boss is ready, and the topic of position C is up to you
Global and Chinese markets of digital crosspoint switches and mux/demux 2022-2028: Research Report on technology, participants, trends, market size and share
学习笔记3--高精度地图关键技术(上)
Using tabbar in wechat applet
Entrepreneurship is a little risky. Read the data and do a business analysis
三分钟学会基础k线图知识
How to compress video size while adding watermark with one click?