城市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;