chenxi16

导航

数据结构1 -06-图3 六度空间

 

 

 1 #include<stdio.h>
 2 #include<string.h>
 3 #define MAXN 1004
 4 int g[MAXN][MAXN];
 5 int n,m;
 6 int queue[MAXN*10000],front,rear;
 7 void read();
 8 void BFS();
 9 int isempty();
10 int addq(int x);
11 int enq();
12 int main(){
13     int i;
14     double t;
15     read();
16     BFS();
17     for(i=1;i<=n;i++){
18         t = (g[0][i]/ (double) n) * 100;
19         printf("%d: %.2f%%\n",i,t);
20     }
21     return 0;
22 } 
23 void read(){
24     scanf("%d %d",&n,&m);
25     int i,j,v1,v2;
26     for(i=1;i<=m;i++){
27         scanf("%d %d",&v1,&v2);
28         g[v1][v2] = 1;
29         g[v2][v1] = 1;
30     }
31 }
32 
33 int addq(int x){
34     if((rear+1)%MAXN==front) return -1;
35     rear = (rear+1)%MAXN;
36     queue[rear] = x;
37     return 1;
38 } 
39 int enq(){
40     if(front==rear) return -1;
41     front = (front+1)%MAXN;
42     return queue[front];
43 }
44 int isempty(){
45     if(front==rear) return 1;
46     else return 0;
47 }
48 void BFS(){
49     int vis[MAXN],level[MAXN],value,i,j;
50     
51     for(i=1;i<=n;i++){
52         g[0][i] = 0; memset(level,0,sizeof(level));
53         memset(vis,0,sizeof(vis));
54         front = -1; rear = -1; addq(i);
55         vis[i] = 1;
56         while(isempty()==0){
57             value = enq();    
58             if(level[value]>=7) break;
59             else g[0][i]++;
60             for(j=1;j<=n;j++){
61                 if(g[value][j]!=0&&value!=j&&vis[j]==0){
62                     addq(j);
63                     vis[j] = 1;
64                     level[j] = level[value] + 1;
65                 }
66             }    
67         }    
68     }
69 }
View Code

 

posted on 2020-04-05 16:25  chenxi16  阅读(378)  评论(0编辑  收藏  举报