当前位置:网站首页>DS图—图的最短路径(不含代码框架)
DS图—图的最短路径(不含代码框架)
2022-07-21 16:26:00 【奔跑的星黛露】
题目描述
给出一个图的邻接矩阵,输入顶点v,用迪杰斯特拉算法求顶点v到其它顶点的最短路径。
输入
第一行输入t,表示有t个测试实例
第二行输入顶点数n和n个顶点信息
第三行起,每行输入邻接矩阵的一行,以此类推输入n行
第i个结点与其它结点如果相连则为距离,无连接则为0,数据之间用空格
隔开。第四行输入v0,表示求v0到其他顶点的最短路径距离
以此类推输入下一个示例
输出
对每组测试数据,输出:
每行输出v0到某个顶点的最短距离和最短路径
每行格式:v0编号-其他顶点编号-最短路径值----[最短路径]。没有路径输出:v0编号-其他顶点编号--1。具体请参考示范数据
样例输入
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
V0
样例输出
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 ]
代码
#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;
}
边栏推荐
- 开发动态 | StoneDB 2022年版本发布里程碑
- js原生——数组排序 去重 找出最大数 字符串出现最多的字符
- cmake使用boost静态库,错误提示找Could NOT find Boost (missing: system thread filesystem
- ROS机械臂 Movelt 学习笔记1 | 基础准备
- Lombok comments @accessories
- Tianzheng T20 v8.0 complete software installation package download and installation tutorial
- 智能运维场景解析:如何通过异常检测发现业务系统状态异常
- 单片机入门:LED双向流水灯
- 网易邮箱(126/163):授权码获取攻略
- Multithreading and concurrent programming (3)
猜你喜欢
Dynamics CRM: several situations in which the display fields in a form become read-only
How to open multiple wechat on the computer
Tapdata 与优炫数据库完成产品兼容性互认证
迪拜推出国家元宇宙战略
How to upload wechat applet developed by uniapp to wechat applet platform - complete and simple steps
Comment changer la police de la console en console?
微信支付Native(一)准备和相关知识
kubernetes 静态存储与动态存储
C语言实型数据的存在形式以及合法性(浮点数)
uniapp开发的微信小程序如何上传至微信小程序平台-完整简单步骤
随机推荐
Web3 n'a jamais échappé à ces vieux géants.
同花顺开户哪个券商好请问,手机开户股票开户安全吗?
Information sharing of programming learning
How to isomorphism + cross end, knowing applet +kbone+finclip is enough!
The pit trodden by real people tells you to avoid the 10 mistakes that novices in automated testing often make
MLX90640 红外热成像仪测温模块开发笔记(三)
Huawei cloud koomessage is a new marketing weapon in the hot public beta
Introduction to single chip microcomputer: LED lights cycle to the right and turn on
15. 谈谈这几个常见的多线程面试题
uniapp开发的微信小程序如何上传至微信小程序平台-完整简单步骤
国家互联网信息办公室有关负责人就对滴滴全球股份有限公司依法作出网络安全审查相关行政处罚的决定答记者问
单片机入门:LED双向流水灯
Front line engineers tell you the real status and development prospects of embedded "suggestions collection"
使用OpenCv+Arduino实现挂机自动打怪
Dynamics crm: how to create an automatic numbering attribute for an entity
华为云KooMessage火热公测中,一款新的营销利器
跟我读论文丨Multi-Model Text Recognition Network
Notes on the establishment of the official website of the public network (V): the complete process of filing the domain name with the Ministry of industry and information technology and resolving the
易观分析《2022年中国数据安全市场数据监测报告》正式启动
wallys/new product/DR7915/MT7915+MT7975/WiFi6 MiniPCIe Module 2T2R