当前位置:网站首页>最少转机--图的广度优先遍历
最少转机--图的广度优先遍历
2022-07-16 15:05:00 【一只829】
城市1-n的最少转机数
样例输入:
第一行:五个城市,7条通路(无向),起点,终点
第二行:哪两个城市连通
5 7 1 5
1 2
1 3
2 3
2 4
3 4
3 5
4 5样例输出:
2
完整代码:
#include<iostream>
using namespace std;
struct node {
int x;//城市编号
int s;//转机次数
};
int main() {
int i, j, a, b, n, m,tail, head,start,flag=0,end,cur;
int book[101] = { 0 }, e[101][101];
struct node que[10001];
//输入n个城市,m个航班,起始地点,终点
cin >> n >> m>>start>>end;
for(i=1;i<=n;i++)
for (j = 1;j <= n;j++) {
if (i == j)
e[i][j] = 0;
else
e[i][j] = -1;
}
//无向图赋值
for (i = 1;i <= m;i++) {
cin >> a >> b;
e[a][b] = 1;
e[b][a] = 1;
}
//队列初始化
head = 1;
tail = 1;
//从start号称是出发,将start号城市加入队列
que[tail].x = start;
que[tail].s = 0;
tail++;
book[1] = start;//标记
//当队列不为空时循环
while (head < tail) {
cur = que[head].x;//当前队列中首城市的编号
for (i = 1;i <= n;i++) {
//从城市cur到城市i有航班并且未标记
if (e[cur][i] !=-1 && book[i] == 0) {
que[tail].x= i;
que[tail].s = que[head].s + 1;
tail++;
book[i] = 1;
}
//如果到达目标城市,停止扩展,任务结束,退出循环
if (que[tail].x ==end) {
flag = 1;
break;
}
}
if (flag == 1)
break;
head++;
}
cout << que[tail - 1].s;
return 0;
}边栏推荐
- STM32-中断优先级管理NVIC详解
- 服装ERP怎么选?选型时一定要问这些问题
- ssd训练自己的数据集
- CAN光端机解决泰和安TX3016C消防主机长距离联网问题
- ERROR: Could not install packages due to an OSError: [ Errno 2] No such file or directory: ***
- 【Day3】Rnn
- MySQL 触发器
- 02 review multithreading
- Use of thingjs
- A game research and development company in Shenzhen installed monitoring for each station. Netizen: it's comparable to imprisonment!
猜你喜欢
![ERROR: Could not install packages due to an OSError: [ Errno 2] No such file or directory: ***](/img/81/6f9b7d554e6a47e614107c799229d2.png)
ERROR: Could not install packages due to an OSError: [ Errno 2] No such file or directory: ***

每日一题·剑指Offer || 041.滑动窗口的平均值(346一样)·队列

不知道如何提高视觉语言大模型?浙大与联汇研究院提出新型多维度评测框架...

Here comes the problem! Unplug the network cable for a few seconds and plug it back in. Does the original TCP connection still exist?

TCP拥塞控制详解 | 6. 主动队列管理
![[英雄星球七月集训LeetCode解题日报] 第16日 队列](/img/0d/157ede29995f32163f8db01398de9a.png)
[英雄星球七月集训LeetCode解题日报] 第16日 队列

Can optical transceiver solves the long-distance networking problem of taihe'an tx3016c fire host

An article explains unsupervised learning in images in detail

Exception handling after MySQL IBD file undelete recovery ---- Xi Fenfei

事务隔离级别
随机推荐
认识JVM
2022/7/15 每日一题(二分+dp+贪心思维)
2020/7/13 每日一题(构造)
软件测试面试题:简述什么是静态测试、动态测试、黑盒测试、白盒测试、α测试 β测试?
电流镜自动布局 布局对称性: 量化和应用以消除非线性过程梯度
[LeetCode解题报告] 423. 从英文中重建数字
Let's customize the reflection system
go tool objdump 反汇编使用说明
SAP S4 Material Management 库存模块 MARD 数据库表读取技术细节介绍
List集合
ospf特殊区域
重写equals为什么要重写hashcode
Sword finger offer 52 The first common node of two linked lists
Translation and interpretation of the paper: learning logic rules for reasoning on knowledge graphs [rnnlogic]
MySQL 变量、流程控制与游标练习
三种“榨干”企业装置服装ERP预算的情况
【C语言刷LeetCode】1432. 改变一个整数能得到的最大差值(M)
Three kinds of "squeeze dry" enterprise equipment clothing ERP budget
ospf路由管理
Offline installation: how to build a secure enterprise class harbor service? The content is too detailed.