当前位置:网站首页>acwing854. Floyd finds the shortest path
acwing854. Floyd finds the shortest path
2022-04-23 05:33:00 【Ice Cream~】
Original link :https://www.acwing.com/problem/content/856/
Unclear floyd Students of the shortest path principle can refer to this article blog:https://blog.csdn.net/weixin_52797843/article/details/121643355?spm=1001.2014.3001.5501
Catalog
Given a n A little bit m Directed graph of strip edge , There may be double edges and self rings in the graph , The edge weight may be negative .
Re given k A asked , Each query contains two integers x and y, Indicates that the query is from point x point-to-point y The shortest distance , If the path doesn't exist , The output
impossible
.There is no negative weight loop in the data assurance diagram .
Input format
The first line contains three integers n,m,k.
Next m That's ok , Each line contains three integers x,y,z Indicates that there is a from point x point-to-point y The directed side of , Side length is z.
Next k That's ok , Each line contains two integers x,y, Indicates a query point x point-to-point y The shortest distance .
Output format
common k That's ok , Output an integer per line , Indicates the result of the inquiry , If there is no path between two points , The output
impossible
.Data range
1≤n≤200
1≤k≤n2
1≤m≤20000
The absolute value of side length involved in the figure shall not exceed 10000.sample input :
3 3 2 1 2 1 2 3 2 1 3 1 2 1 1 3
sample output :
impossible 1
Code implementation :
#include<iostream>
using namespace std;
const int INF=0x3f3f3f3f;
int n,m,k;
int a[310][310];
int main()
{
cin>>n>>m>>k;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==j)a[i][j]=0;// Diagonal distance 0
else a[i][j]=INF;// Initialization infinity
}
}
int x,y,z;
for(int i=1;i<=m;i++)
{
cin>>x>>y>>z;
a[x][y]= min(a[x][y],z);
}
for(int k1=1;k1<=n;k1++){// Update the shortest path
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
a[i][j]=min(a[i][j],a[i][k1]+a[k1][j]);// If the weight of other paths is smaller , The distance is shorter to update
}
}
}
while(k--)
{
int dx,dy;
cin>>dx>>dy;
if(a[dx][dy]>=15000)// The title says that the side length involved does not exceed 10000 Try to make it bigger
{
cout<<"impossible"<<endl;
}
else cout<<a[dx][dy]<<endl;
}
return 0;
}
版权声明
本文为[Ice Cream~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220534524725.html
边栏推荐
- 弘玑|数字化时代下,HR如何进行自我变革和组织变革?
- Nécessité de précharger les cookies dans le sélénium
- Deep learning object detection
- Excel sets row and column colors according to cell contents
- Note: unordered_ Understanding and use of map
- Log introduction and building web application
- Wbpack configuring production development environment
- 巴普洛夫与兴趣爱好
- How to realize adaptive layout
- Common interview questions - 4 (MySQL)
猜你喜欢
Arithmetic and logical operations
Data mining -- understanding data
SQL Server检索SQL和用户信息的需求
The title bar will be pushed to coincide with the status bar
Hongji micro classroom | cyclone RPA's "flexible digital employee" actuator
Cross platform packaging of QT packaging program
Three methods of list rendering
selenium预先加载cookie的必要性
跨域CORS的情缘~
Uniapp wechat sharing
随机推荐
Box collapse and margin collapse
Traversal array, object parent-child communication props / $emit
uni使用的一些坑
The QT debug version runs normally and the release version runs crash
Three methods of list rendering
‘EddiesObservations‘ object has no attribute ‘filled‘
QT drawpixmap and DrawImage blur problem
CMake基础教程(39)pkgconfig
分支与循环语句
JVM memory and memory overflow exceptions (personal summary)
(十一)vscode代码格式化配置
Usage and difference of shellexecute, shellexecuteex and winexec in QT
修仙真实世界与游戏世界
Escape characters \ splicing of data formats
Use pagoda + Xdebug + vscode to debug code remotely
what is wifi6?
Several examples of pointer transfer, parameter transfer, value transfer, etc
On the use of constant pointer and pointer constant -- exercise (record)
JSON.
X86 assembly syntax: at & T and Intel