王道考研 ++++ BFS单源最短路径

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

#define MaxSize 100
#define INF 1000001

typedef struct EageTable  //锟竭憋拷
{
  int eageData;           //值
  struct EageTable *next; //指锟斤拷锟斤拷一锟斤拷锟斤拷
}EageTable;
typedef struct HeadTable  //头锟斤拷
{
  int headData;           //值
  EageTable *first;       //指锟斤拷头锟节碉拷锟斤拷锟斤拷一锟斤拷锟节碉拷
}HeadTable,HeadList[MaxSize];
typedef struct{
  HeadList Graph;
  int headNum,arcNum;     //头锟节碉拷锟斤拷 锟斤拷锟斤拷
}Graph;

typedef struct LQueue     //锟斤拷锟斤拷
{
  int Queue[MaxSize];
  int front,rear;
}LQueue;

bool vis[MaxSize] = {false};
int dis[MaxSize];

void InitQueue(LQueue *Q);
bool JudgeEmpty(LQueue *Q);
void EnQueue(LQueue *Q,int num);
int DeQueue(LQueue *Q);
int GetFront(LQueue *Q);
void InitGraph(Graph *G);
void CreateGraph(Graph *G,int flag,int a,int b);
void BFS(LQueue *Q,Graph *G,int front);
void TraverBFS(LQueue *Q,Graph *G,int start);
/*锟斤拷始锟斤拷锟斤拷锟斤拷*/
void InitQueue(LQueue *Q)
{
  Q->front = Q->rear = 0;
}
/*锟叫断讹拷锟斤拷锟角凤拷为锟斤拷*/
bool JudgeEmpty(LQueue *Q)
{
  if(Q->front == Q->rear)return true;
  else return false;
}
/*锟斤拷锟斤拷*/
void EnQueue(LQueue *Q,int num)
{
  Q->Queue[Q->rear++] = num;
}
/*锟斤拷锟斤拷*/
int DeQueue(LQueue *Q)
{
  int temp = Q->Queue[Q->front];
  Q->front++;
  return temp;
}
int GetFront(LQueue *Q)
{
  return Q->Queue[Q->front];
}
/*锟斤拷始锟斤拷锟节接憋拷锟斤拷头*/
void InitGraph(Graph *G)
{
  int i;
  for(i = 1;i <= G->headNum;i++)
  {
    G->Graph[i].headData = i;
    G->Graph[i].first = NULL;
  }
}
/*锟斤拷锟斤拷锟节接憋拷*/
void CreateGraph(Graph *G,int flag,int a,int b)
{
  EageTable* eage;
  eage = (EageTable*)malloc(sizeof(EageTable));
  eage->eageData = b;
  //头锟斤拷锟斤拷
  eage->next = G->Graph[a].first;
  G->Graph[a].first = eage;
  //图为锟斤拷锟斤拷图时
  if(flag == 2)
  {
    EageTable* eagee;
    eagee = (EageTable*)malloc(sizeof(EageTable));
    eagee->eageData = a;
    //头锟斤拷锟斤拷
    eagee->next = G->Graph[b].first;
    G->Graph[b].first = eagee;
  }
}
/*锟斤拷锟斤拷*/
void BFS(LQueue *Q,Graph *G,int front)
{
    EageTable *eage = G->Graph[front].first;//eage锟斤拷front锟节碉拷锟侥诧拷锟斤拷
    while(eage)//锟斤拷锟斤拷
    {
      if(vis[eage->eageData] == false)//没锟斤拷锟斤拷锟绞的节碉拷锟斤拷栈 锟斤拷 锟斤拷志锟节碉拷锟窖讹拷
      {
          EnQueue(Q,eage->eageData);
          vis[eage->eageData] = true;
          dis[eage->eageData] = dis[front] + 1;
      }
      eage = eage->next;
    }
}
void TraverBFS(LQueue *Q,Graph *G,int start)
{
  InitQueue(Q);
  EnQueue(Q,start);
  vis[start] = true;//锟斤拷志锟节碉拷锟窖讹拷
  dis[start] = 0;
  while(!JudgeEmpty(Q))
  {
    int front = GetFront(Q);//锟斤拷取锟斤拷锟叫碉拷一锟斤拷锟节碉拷
    printf("%-2d",front);
    BFS(Q,G,front);
    DeQueue(Q);
  }
}
int main(int argc, char const *argv[]) {
  Graph G;
  LQueue Q;
  int i,flag,start;

  memset(dis,INF,sizeof(int)*MaxSize);
  printf("锟斤拷锟斤拷图锟斤拷锟斤拷1锟斤拷锟斤拷锟斤拷图锟斤拷锟斤拷2:");
  scanf("%d",&flag);
  printf("锟斤拷锟斤拷锟斤拷锟节碉拷锟斤拷:");
  scanf("%d",&G.headNum);
  InitGraph(&G);
  printf("锟斤拷锟斤拷锟诫弧锟斤拷锟斤拷锟斤拷(锟斤拷1锟斤拷始):");
  scanf("%d",&G.arcNum);
  printf("锟斤拷锟斤拷%d锟斤拷锟斤拷锟斤拷:\n",G.arcNum);
  for(i = 0;i < G.arcNum;i++)
  {
    int a,b;
    scanf("%d %d",&a,&b);
    CreateGraph(&G,flag,a,b);
  }
  printf("锟斤拷锟斤拷锟诫开始锟侥节碉拷:");
  scanf("%d",&start);
  printf("BFS锟斤拷锟斤拷:\n");
  TraverBFS(&Q,&G,start);
  printf("\n锟斤拷锟姐单源锟斤拷锟斤拷路锟斤拷锟斤拷:\n");
  for(i = 1;i <= G.headNum;i++)
    printf("%-d%c",dis[i]," \n"[i == G.headNum]);
  return 0;
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值