当前位置:网站首页>[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;
}
边栏推荐
- 浅浅了解Servlet
- Global and Chinese markets of edge AI software 2022-2028: Research Report on technology, participants, trends, market size and share
- 学习笔记24--多传感器后融合技术
- Recommend an online interface mock tool usemock
- Altium designer measure distance (ctrl+m)
- 只是以消费互联网的方式和方法来落地和实践产业互联网,并不能够带来长久的发展
- 6-2漏洞利用-ftp不可避免的问题
- Basic number theory -- Gauss elimination
- How to compress video size while adding watermark with one click?
- Raspberry pie 4B learning notes - IO communication (1-wire)
猜你喜欢

Based on Simulink and FlightGear, the dynamic control of multi rotor UAV in equilibrium is modeled and simulated

XMIND mind map

Since I understand the idea of dynamic planning, I have opened the door to a new world

What are the affordable Bluetooth headsets? Student party parity Bluetooth headset recommendation

学习笔记24--多传感器后融合技术

About asp Net core uses a small detail of datetime date type parameter

关于ASP.NET CORE使用DateTime日期类型参数的一个小细节

Edge computing accelerates live video scenes: clearer, smoother, and more real-time

II Basic structure of radio energy transmission system
![[IVX junior engineer training course 10 papers to get certificates] 09 chat room production](/img/a8/25215e74162b7ab3f29df65681932b.jpg)
[IVX junior engineer training course 10 papers to get certificates] 09 chat room production
随机推荐
Just using the way and method of consuming the Internet to land and practice the industrial Internet will not bring long-term development
Basic usage of shell script
Daily work and study notes
学习笔记25--多传感器前融合技术
Global and Chinese market of wireless chipsets 2022-2028: Research Report on technology, participants, trends, market size and share
Hcip day 14 (MPLS protocol)
基于SSM实现微博系统
10 minutes to get started quickly composition API (setup syntax sugar writing method)
Single chip microcomputer -- hlk-w801 transplant NES simulator (III)
LeetCode、3无重复最长子序列
The concept and application of Cartland number
现货黄金分析的技巧有什么呢?
Global and Chinese markets for freight and logistics 2022-2028: Research Report on technology, participants, trends, market size and share
No converter found for return value of type: class
Mathematics - feelings -20220215
I'll teach you to visit Amazon RDS for a year and build a MySQL cloud database (only 10 minutes, really fragrant)
Design and control of multi rotor aircraft (VII) -- sensor calibration and measurement model
Global and Chinese markets for context and location-based services 2022-2028: Research Report on technology, participants, trends, market size and share
Keepalived introduction and installation
CTF daily question day45 sensor