network of emergency contacts---BFS

本题目的主要难点在如何寻找最后一层的数,利用book标记数组可以很好的解决问题。book=0的时候说明没被连接过,若要连接时,将book等于上一层book+1.

需要注意的是,对于图的问题建立邻接矩阵是非常省时间的方法。类似这个道题,我没有用邻接矩阵,因此每次都要进入150的for循环。但是如果用矩阵,最多则是每次100的循环。这样对于n*n*n.....来说则会剩下很多时间。

#include<stdio.h>
#include<stdlib.h>
typedef struct{
    int a[300];
    int rear;
    int head;
}queue;
queue q;
int A[150],B[150];
int book[300];
int test(int data[300],int startNum);
void build(int data[300]){
    for(int i=0;i<300;i++)
        data[i]=rand()%100+1;
}
void main(){
    //freopen("intput.txt","r",stdin);
    int data[300];
    for(int l=0;l<10;l++){
        build(data);
        printf("%d\n",test(data,data[0]));
    }
}
int test(int data[300],int startNum){
    for(int i=0;i<300;i++)
        book[i]=0;
    for(int i=0,j=0;i<299;j++){          //将数据分为两个数组中A表示边的出发点,B表示边的终点
        A[j]=data[i];
        B[j]=data[i+1];
        i=i+2;
    }
    q.rear=0;
    q.head=0;
    q.a[q.rear++]=startNum;
    book[startNum]=1;
    while(q.head<q.rear){
        int temp=q.a[q.head++];
        for(int i=0;i<150;i++){
            if(A[i]==temp&&book[B[i]]==0){
                q.a[q.rear++]=B[i];
                book[B[i]]=book[A[i]]+1;     //标记层数
            }
        }
    }
    int c=0;
    int max=0;
    for(int i=0;i<=299;i++){            //寻找最大层数
        if(c<book[i]){
            c=book[i];
        }
    }
    for(int i=299;i>=0;i--){            //寻找最大层数总最大值
        if(book[i]==c){
            max=i;
            break;
        }
    }
    return max;
}
时间: 2024-08-03 23:05:55

network of emergency contacts---BFS的相关文章

CSU 1633: Landline Telephone Network (最小生成树)

1633: Landline Telephone Network Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 63  Solved: 8 [Submit][Status][Web Board] Description The mayor of RMRCity wants to create a secure landline telephone network for emergency use in case of serious disas

CSU1633: Landline Telephone Network

Description The mayor of RMRCity wants to create a secure landline telephone network for emergency use in case of serious disasters when the city is cut off from the outside world. Some pairs of buildings in the city can be directly connected with a

鲁棒性、可读的android架构(I)

Since the early days of Android, I've been looking for a robust way to build Android apps, keep the IO operations out of the UI Thread, avoid duplicated network calls, cache relevant things, update the cIache at the right time, etc... with the cleane

寥灾苌诼伺s66r9ebx5n742g8u

新华社瓦莱塔4月10日电(记者李拯宇 李佳)全国政协主席俞正声10日在前往非洲三国进行正式友好访问途中过境马耳他,在瓦莱塔会见马耳他议长法鲁贾. 俞正声说,中马保持长期友好关系,政治上相互信任,经济上密切合作,人文交流不断深化.中方感谢马方在中国撤侨行动中给予的支持和帮助.中方愿同马方一道,落实两国领导人达成的共识,弘扬中马传统友好,拓展在科技.渔业.旅游等领域互利合作,打造合作新亮点.中国全国政协愿与马耳他议会和社会各界保持密切交往,加强治国理政经验交流,为两国扩大务实合作营造良好环境,共同促

事吕茉匮矍k91rzv72

新华社瓦莱塔4月10日电(记者李拯宇 李佳)全国政协主席俞正声10日在前往非洲三国进行正式友好访问途中过境马耳他,在瓦莱塔会见马耳他议长法鲁贾. 俞正声说,中马保持长期友好关系,政治上相互信任,经济上密切合作,人文交流不断深化.中方感谢马方在中国撤侨行动中给予的支持和帮助.中方愿同马方一道,落实两国领导人达成的共识,弘扬中马传统友好,拓展在科技.渔业.旅游等领域互利合作,打造合作新亮点.中国全国政协愿与马耳他议会和社会各界保持密切交往,加强治国理政经验交流,为两国扩大务实合作营造良好环境,共同促

疗了厣砸律ns33ue9l5

新华社瓦莱塔4月10日电(记者李拯宇 李佳)全国政协主席俞正声10日在前往非洲三国进行正式友好访问途中过境马耳他,在瓦莱塔会见马耳他议长法鲁贾. 俞正声说,中马保持长期友好关系,政治上相互信任,经济上密切合作,人文交流不断深化.中方感谢马方在中国撤侨行动中给予的支持和帮助.中方愿同马方一道,落实两国领导人达成的共识,弘扬中马传统友好,拓展在科技.渔业.旅游等领域互利合作,打造合作新亮点.中国全国政协愿与马耳他议会和社会各界保持密切交往,加强治国理政经验交流,为两国扩大务实合作营造良好环境,共同促

HDU 4039 The Social Network bfs

算了下复杂度好像是n^3 就感觉不大好做.结果n^31a... #include <cstdio> #include <algorithm> #include <iostream> #include <string.h> #include <map> #include <vector> #include <string> #include <queue> using namespace std; #define

UVALive3902 Network[贪心 DFS&amp;&amp;BFS]

UVALive - 3902 Network Consider a tree network with n nodes where the internal nodes correspond to servers and the terminal nodes correspond to clients. The nodes are numbered from 1 to n. Among the servers, there is an original server S which provid

1003 Emergency

As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are