• 数据结构实验之图论一:基于邻接矩阵的广度优先搜索遍历 (SDUT 2141)


    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #include <queue>
    using namespace std;
    typedef long long ll;
    const int inf = 0x3fffff;
    int gra[200][200];
    int vis[200];
    int path[200];
    int n,m,k,t;
    void bfs(int s)
    {
        memset(vis,0,sizeof(vis));
        memset(path,-1,sizeof(path));
        queue<int>q;
        vis[s] = 1;
        q.push(s);
        int top = 0;
        path[top ++ ] = s;
        while(!q.empty())
        {
            int now = q.front();
            q.pop();
            for(int i = 0; i < k; i ++)
            {
                if(gra[now][i] == 1&& vis[i] == 0)
                {
                    q.push(i);
                    vis[i] = 1;
                    path[top ++] = i;
                }
            }
        }
        for(int i = 0; i < top; i ++)
        {
            if(i == 0)
                printf("%d",path[i]);
            else
                printf(" %d",path[i]);
        }
        printf("
    ");
    }
    int main()
    {
        int u,v;
        scanf("%d",&n);
        while(n --)
        {
            scanf("%d%d%d",&k,&m,&t);
            memset(gra,0,sizeof(gra));
            for(int i = 0; i < m; i ++)
            {
                scanf("%d%d",&u,&v);
                gra[u][v] = gra[v][u] = 1;
            }
            bfs(t);
        }
        return 0;
    }
    
  • 相关阅读:
    js 实现图片上传
    关于IOS不能使用JQUERY的ON事件
    js实现复制
    订单列表倒计时
    小程序实现倒计时
    微信小程序服务消息推送
    python爬虫七
    python爬虫六
    python爬虫五
    python爬虫四
  • 原文地址:https://www.cnblogs.com/lcchy/p/10139457.html
Copyright © 2020-2023  润新知