当前位置:网站首页>[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;
}
边栏推荐
- error: . repo/manifests/: contains uncommitted changes
- [image enhancement] vascular image enhancement based on frangi filter with matlab code
- Luogu p1775 stone merger (weakened version)
- Memorabilia of domestic database in June 2022
- GL Studio 5 安装与体验
- 三分钟学会基础k线图知识
- Basic usage of shell script
- Fastadmin controls the length of fields in the table
- 现货黄金分析的技巧有什么呢?
- Tencent cloud techo youth dream campus trip into Wuhan University
猜你喜欢
Introduction to ffmpeg Lib
The technology boss is ready, and the topic of position C is up to you
How does schedulerx help users solve the problem of distributed task scheduling?
Unity AssetBundle subcontracting
How to compress video size while adding watermark with one click?
The pain of Xiao Sha
ES6 new method of string
电商系统中常见的9大坑,你踩过没?
Learn C language from scratch day 025 (maze)
6-3 vulnerability exploitation SSH environment construction
随机推荐
SAP ui5 beginner tutorial XXI - trial version of custom formatter of SAP ui5
学习笔记25--多传感器前融合技术
Global and Chinese market of wireless chipsets 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese markets for the application of artificial intelligence in security, public security and national security 2022-2028: Research Report on technology, participants, trends, market size
Global and Chinese markets of beverage seasoning systems 2022-2028: Research Report on technology, participants, trends, market size and share
Game thinking 15: thinking about the whole region and sub region Services
Just using the way and method of consuming the Internet to land and practice the industrial Internet will not bring long-term development
uTools
The first "mobile cloud Cup" empty publicity meeting, looking forward to working with developers to create a new world of computing!
Finally got byte offer, 25-year-old inexperienced experience in software testing, to share with you
Global and Chinese markets for supply chain strategy and operation consulting 2022-2028: Research Report on technology, participants, trends, market size and share
Design and implementation of radio energy transmission system
6-2 vulnerability exploitation - inevitable problems of FTP
Brief introduction to the development of mobile network
Bubble Sort Graph
Sql--- related transactions
What are the affordable Bluetooth headsets? Student party parity Bluetooth headset recommendation
8.8.4-PointersOnC-20220215
SQL injection for Web Security (2)
Laravel artisan 常用命令