Chinaunix首页 | 论坛 | 博客
  • 博客访问: 38574
  • 博文数量: 64
  • 博客积分: 2640
  • 博客等级: 少校
  • 技术积分: 670
  • 用 户 组: 普通用户
  • 注册时间: 2010-01-26 13:15
文章分类
文章存档

2010年(64)

我的朋友
最近访客

分类: C/C++

2010-01-26 14:03:38

2009-03-24 16:24


#include<iostream>
using namespace std;
#define init(x) memset(x,0,sizeof(x) )
#define GRAY -1
#define BLACK 1
#define WHITE 0
const int MAX = 100;
struct{
       int n;
       int adjvex[100];
}Edge[MAX];
struct Node{
       int key;
       Node *next;
       Node(){
           next = 0;
       }
};
class LinkQueue{
      public:
         LinkQueue();
        
         bool IsEmpty();
         void EnQueue(int key);
         void DeQueue(int& key);
     
      private:
         Node *head,*tail;
};
LinkQueue::LinkQueue(){
         head = new Node;
         tail = head;
}
bool LinkQueue::IsEmpty(){
     return head==tail;
}
void LinkQueue::EnQueue(int key){
     Node* t = new Node;
    
     t->key = key;
     tail->next = t;
     tail = t;
}
void LinkQueue::DeQueue(int& key){
     if(IsEmpty() ) return;
    
     Node* p = head->next;
     head->next = p->next;
     key = p->key;
    
     if(p==tail) tail = head;
     delete p;
}
void BFS(int s,int f[],int d[]){
     LinkQueue q;
     int u,v,i,j;
     int color[MAX];
    
     init(f);
     init(d);
     init(color);
     f[s] = 0;
     d[s] = 0;
     color[s] = BLACK;
     q.EnQueue(s);
    
     while(!q.IsEmpty() ){
           q.DeQueue(v);
           color[v] = GRAY;
           for(i=0;i<Edge[v].n;i++){
               u = Edge[v].adjvex[i];
               if(color[u]==WHITE){
                  q.EnQueue(u);
                  color[u] = GRAY;
                  d[u] = d[v]+1;
                  f[u] = v;
               }
           }
           color[v] = BLACK;
     }
}


阅读(199) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~