当前位置:网站首页>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;
}边栏推荐
猜你喜欢

Summary of feature selection: filtered, wrapped, embedded

Not configured in app.json (uni releases wechat applet)

Deep learning 1 perceptron and implementation of simple back propagation network

Performance test - analyze requirements

Bibliometrix: dig out the one worth reading from thousands of papers!

Similarities and differences between nor flash and NAND flash

The accuracy of yolov7 in cracking down on counterfeits, not all papers are authentic

打假Yolov7的精度,不是所有的论文都是真实可信

Leetcode · daily question · 1184. distance between bus stops · simulation

Introduction to Xiaoxiong school
随机推荐
Rasa 3.x 学习系列-Rasa [3.2.4] - 2022-07-21 新版本发布
Beijing all in one card listed and sold 68.45% of its equity at 352.888529 million yuan, with a premium rate of 84%
Notes on the use of IEEE transaction journal template
Rasa 3.x learning series -rasa [3.2.3] - new version released on July 18, 2022
Under multi data source configuration, solve org.apache.ibatis.binding Bindingexception: invalid bound statement (not found) problem
Problem handling of repeated restart during Siemens botu installation
Performance test - Test Execution
Unity 使用NVIDIA FleX for Unity插件实现制作软体、水流流体、布料等效果学习教程
PrestoUserError: PrestoUserError(type=USER_ERROR, name=INVALID_FUNCTION_ARGUMENT, message=“Escape st
Rasa 3.x 学习系列-Rasa FallbackClassifier源码学习笔记
Sword finger offer II 001. integer division
Rest style
Solve the problem that uni starter can log in to wechat with local functions, but fails to log in with cloud functions
spark:获取日志中每个时间段的访问量(入门级-简单实现)
pytorch with torch.no_grad
Atcoder beginer contest 261e / / bitwise thinking + DP
Isprs2018/ cloud detection: cloud/shadow detection based on spectral indexes for multi/hyp multi / hyperspectral optical remote sensing imager cloud / shadow detection
How vscode debug nodejs
Research Summary / programming FAQs
基于ABP实现DDD--实体创建和更新