当前位置:网站首页>DS diagram - the shortest path of the diagram (excluding the code framework)
DS diagram - the shortest path of the diagram (excluding the code framework)
2022-07-24 14:49:00 【Running star dailu】
Title Description
The adjacency matrix of a graph is given , Enter vertex v, Use dijestra algorithm to find the vertex v The shortest path to other vertices .
Input
First line input t, Express t A test case
Enter the number of vertices in the second line n and n Vertex information
From the third line , Enter one row of the adjacency matrix for each row , And so on n That's ok
The first i If a node is connected with other nodes, it is the distance , If there is no connection, it is 0, Use spaces between data
separate . Enter... In the fourth line v0, Express and seek v0 The shortest path distance to other vertices
And so on, enter the next example
Output
For each group of test data , Output :
Output per row v0 The shortest distance and shortest path to a vertex
Format per line :v0 Number - Other vertex numbers - Shortest path value ----[ Shortest path ]. No path output :v0 Number - Other vertex numbers --1. Please refer to the demonstration data for details
The sample input
2
5 0 1 2 3 4
0 5 0 7 15
0 0 5 0 0
0 0 0 0 1
0 0 2 0 0
0 0 0 0 0
0
6 V0 V1 V2 V3 V4 V5
0 0 10 0 30 100
0 0 5 0 0 0
0 0 0 50 0 0
0 0 0 0 0 10
0 0 0 20 0 60
0 0 0 0 0 0
V0Sample output
0-1-5----[0 1 ]
0-2-9----[0 3 2 ]
0-3-7----[0 3 ]
0-4-10----[0 3 2 4 ]
V0-V1--1
V0-V2-10----[V0 V2 ]
V0-V3-50----[V0 V4 V3 ]
V0-V4-30----[V0 V4 ]
V0-V5-60----[V0 V4 V3 V5 ]
Code
#include<bits/stdc++.h>
using namespace std;
const int maxn=100,INF=0x3f3f3f3f;
int t;
map<string,int> mp;
map<int,string> ins;
class GRAPH{
public:
int n,w[maxn][maxn],dist[maxn],st;
bool vis[maxn];
string path[maxn];
GRAPH(){
cin>>n;
for(int i=0;i<n;i++){
string x;
cin>>x;
mp[x]=i;
ins[i]=x;
}
for(int i=0;i<n;i++){
vis[i]=false;
dist[i]=INF;
for(int j=0;j<n;j++){
cin>>w[i][j];
}
}
}
void getpath(){
string start;
cin>>start;
st=mp[start];
dist[st]=0;
for(int i=0;i<n;i++){
path[i]+=ins[st]+" ";
if(i!=st && w[st][i]){
path[i]+=ins[i]+" ";
dist[i]=w[st][i];
}
}
vis[st]=true;
for(int k=0;k<n-1;k++){
int res,dis=INF;
for(int i=0;i<n;i++){
if(!vis[i] && dis>dist[i]){
res=i;
dis=dist[i];
}
}
for(int i=0;i<n;i++){
if(!vis[i] && dist[i]>dist[res]+w[res][i] && w[res][i]){
dist[i]=dist[res]+w[res][i];
path[i]=path[res];
path[i]+=ins[i]+" ";
}
}
vis[res]=true;
}
}
void display(){
for(int i=0;i<n;i++){
if(i==st) continue;
if(dist[i]==INF){
dist[i]=-1;
cout<<ins[st]<<"-"<<ins[i]<<"-"<<dist[i]<<endl;
}
else cout<<ins[st]<<"-"<<ins[i]<<"-"<<dist[i]<<"----["<<path[i]<<"]\n";
}
}
};
int main(){
cin>>t;
while(t--){
GRAPH g;
g.getpath();
g.display();
}
return 0;
}边栏推荐
- 看完这篇文章,才发现我的测试用例写的就是垃圾
- Unity uses NVIDIA flex for unity plug-in to realize the effects of making software, water, fluid, cloth, etc. learning tutorial
- Overall testing framework for performance testing
- Leetcode · daily question · 1184. distance between bus stops · simulation
- exchange
- Problem handling of repeated restart during Siemens botu installation
- Which brokerage has the lowest commission? I want to open an account. Is it safe to open an account on my mobile phone
- Differences between C language pointer and array A and &a, &a[0], etc
- Kotlin类与继承
- threw exception [Circular view path [index]: would dispatch back to the current handler URL [/index]
猜你喜欢

Detailed explanation of IO model (easy to understand)
![[oauth2] IV. oauth2authorizationrequestredirectfilter](/img/42/fff83a8d477e2f2d07d1f5ad4e4405.png)
[oauth2] IV. oauth2authorizationrequestredirectfilter

Decrypt "sea Lotus" organization (domain control detection and defense)
![[matlab] matlab drawing Series II 1. Cell and array conversion 2. Attribute cell 3. delete Nan value 4. Merge multiple figs into the same Fig 5. Merge multiple figs into the same axes](/img/4d/b0ba599a732d1390c5eeb1aea6e83c.png)
[matlab] matlab drawing Series II 1. Cell and array conversion 2. Attribute cell 3. delete Nan value 4. Merge multiple figs into the same Fig 5. Merge multiple figs into the same axes

2022 IAA industry category development insight series report - phase II

Explain the edge cloud in simple terms | 2. architecture

Performance test - Test Execution

Grpc middleware implements grpc call retry

Video game design report template and resources over the years

Attributeerror: module 'distutils' has no attribute' version error resolution
随机推荐
多数据源配置下,解决org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)问题
基于ABP实现DDD--实体创建和更新
Activate the newly installed Anaconda in the server
C operator priority memory formula
TS learning record (I) sudo forgets the password (oolong) try changing the 'lib' compiler option to include 'DOM'
交换
Can you buy 6% of financial products after opening a stock account?
REST风格
Machine learning practice notes
SQL的SELF JOIN用法
电赛设计报告模板及
C language large and small end mode judgment function
Tensorflow framework of deep learning realizes vgg/rnn network / verification code generation and recognition / text classification
Ztree tree Metro style mouse through the display user-defined controls add, edit, delete, down, up operations
Kotlin class and inheritance
PrestoUserError: PrestoUserError(type=USER_ERROR, name=INVALID_FUNCTION_ARGUMENT, message=“Escape st
2022 IAA industry category development insight series report - phase II
Overall testing framework for performance testing
清除字符串中所有空格
Complete set of JS array common operations