当前位置:网站首页>[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;
}
边栏推荐
- Fastadmin controls the length of fields in the table
- Basic number theory -- Gauss elimination
- Minimize the error
- Error creating bean with name ‘stringRedisTemplate‘ defined in class path re
- The first "mobile cloud Cup" empty publicity meeting, looking forward to working with developers to create a new world of computing!
- error: . repo/manifests/: contains uncommitted changes
- 6-2 vulnerability exploitation - inevitable problems of FTP
- Brief description of grafana of # yyds dry goods inventory # Prometheus
- Two TVs
- [Chongqing Guangdong education] Tianshui Normal University universe exploration reference
猜你喜欢

【疾病检测】基于BP神经网络实现肺癌检测系统含GUI界面
![[rust web rokcet Series 1] Hello, world and get, post, put, delete](/img/d8/7dd5fe409d349a13128b6af554f952.jpg)
[rust web rokcet Series 1] Hello, world and get, post, put, delete

Docker安装Oracle_11g

卷积神经网络(包含代码与相应图解)

技术大佬准备就绪,话题C位由你决定
![[dynamic planning] interval dp:p3205 Chorus](/img/25/3dc7132e1aaa5c0eca87382692fc12.jpg)
[dynamic planning] interval dp:p3205 Chorus

How does schedulerx help users solve the problem of distributed task scheduling?

6-2漏洞利用-ftp不可避免的问题

MySQL application day02

卷積神經網絡(包含代碼與相應圖解)
随机推荐
Datawhale 社区黑板报(第1期)
MySQL application day02
What are the affordable Bluetooth headsets? Student party parity Bluetooth headset recommendation
机器学习基本概念
Exclusive delivery of secret script move disassembly (the first time)
Comprehensive broadcast of global and Chinese markets 2022-2028: Research Report on technology, participants, trends, market size and share
浅浅了解Servlet
How does schedulerx help users solve the problem of distributed task scheduling?
[image enhancement] vascular image enhancement based on frangi filter with matlab code
Using tabbar in wechat applet
Réseau neuronal convolutif (y compris le Code et l'illustration correspondante)
Global and Chinese market of ancillary software 2022-2028: Research Report on technology, participants, trends, market size and share
10 minutes to get started quickly composition API (setup syntax sugar writing method)
Learning note 3 -- Key Technologies of high-precision map (Part 1)
970 golang realizes the communication between multithreaded server and client
Global and Chinese market of safety detection systems 2022-2028: Research Report on technology, participants, trends, market size and share
Docker installing Oracle_ 11g
[rust web rokcet Series 2] connect the database and add, delete, modify and check curd
[IVX junior engineer training course 10 papers] 06 database and services
I Brief introduction of radio energy transmission technology