1. 程式人生 > >資料結構實驗圖論:基於鄰接矩陣/鄰接表的廣度優先搜尋遍歷(BFS)

資料結構實驗圖論:基於鄰接矩陣/鄰接表的廣度優先搜尋遍歷(BFS)

//#include <iostream>
//#include <stdlib.h>
//#include <stdio.h>
//#include <string.h>
//#include <queue>
//#define M 20
//#define swap(x,y,t) ((t)=(x),(x)=(y),(y)=(t))
//using namespace std;
//struct node
//{
//    int u,v;
//    node *next;
//}*head[M];
//int vv[M],f=1;
//int cmpp(const void *a,const void *b)
//{
//    return (*(int *)a-*(int *)b);
//}
//void add(int u,int v)
//{
//    node *p=new node;
//    p->v=v;
//    p->next=head[u];
//    head[u]=p;
//}
//int b[100],x[100];
//void bfs(int t)
//{
//    int q,k=0;
//    queue<int>Q;
//    Q.push(t);
//    vv[t]=1;
//    f=1;
//    while(!Q.empty())
//    {
//        q=Q.front();
//        Q.pop();
//        x[++k]=q;
//        int i=0;
//        for(node *p=head[q];p!=NULL;p=p->next)
//        {
//            if(vv[p->v]==0)
//            {
//                b[i++]=p->v;
//                vv[p->v]=1;
//            }
//        }
//        if(i>=1)qsort(b,i,sizeof(b[0]),cmpp);
//        for(int j=0;j<=i-1;j++)
//            Q.push(b[j]);
//    }
//    int j;
//    for( j=1;j<=k-1;j++)
//        printf("%d ",x[j]);
//    printf("%d",x[j]);
//}
//
//struct N
//{
//    int u,v;
//} num[100];
//int cmp(const void *a,const void *b)
//{
//    return (((N *)b)->v-((N *)a)->v);
//}
//int main()
//{
//    int n,k,m;
//    int i,j;
//    int u,v,t;
//    scanf("%d",&n);
//    while(n--)
//    {
//        scanf("%d %d %d",&k,&m,&t);
//        memset(head,NULL,sizeof(head));
//        memset(vv,0,sizeof(vv));
//        for(i=0; i<m; i++)
//        {
//            scanf("%d%d",&u,&v);
//            add(u,v);
//            add(v,u);
//        }
//        bfs(t);
//        printf("\n");
//    }
//    return 0;
//}
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <queue>
#define M 20
#define swap(x,y,t) ((t)=(x),(x)=(y),(y)=(t))
using namespace std;
struct node
{
    int u,v;
    node *next;
}*head[M];
int vv[M],f=1;

void add(int u,int v)
{
    node *p=new node;
    p->v=v;
    p->next=head[u];
    head[u]=p;
}
int n,k,m;
int b[100],x[100];
void bfs(int t)
{
    int q,k=0;
    queue<int>Q;
    Q.push(t);
    vv[t]=1;
    f=1;

    while(!Q.empty())
    {
        q=Q.front();
        Q.pop();
        if(f)
        {
            f=0;
            printf("%d",q);
        }
        else printf(" %d",q);
        for(node *p=head[q]; p!=NULL; p=p->next)
        {
            if(vv[p->v]==0)
            {
                Q.push(p->v);
                vv[p->v]=1;
            }
        }
    }
}

struct N
{
    int u,v;
} num[100];

int main()
{

    int i,j;
    int u,v,t;
    scanf("%d",&n);
    while(n--)
    {
        scanf("%d %d %d",&k,&m,&t);
        memset(head,NULL,sizeof(head));
        memset(vv,0,sizeof(vv));
        for(i=0; i<m; i++)
        {
            scanf("%d %d",&u,&v);
            add(u,v);
            add(v,u);
        }
        int tt;
        for(int i=0; i<m; i++)//對鄰接連結串列排序
        {
            node *p,*q;
            for(p=head[i]; p; p=p->next)
                for(q=p->next; q; q=q->next)
                {
                    if(p->v>q->v)
                        swap(p->v,q->v,tt);
                }
        }
        bfs(t);
        printf("\n");
    }
    return 0;
}


相關推薦

資料結構實驗基於鄰接矩陣/鄰接廣度優先搜尋BFS

//#include <iostream> //#include <stdlib.h> //#include <stdio.h> //#include <string.h> //#include <queue> //#define M 20 //#

sdut oj2141 資料結構實驗基於鄰接矩陣廣度優先搜尋BFS

資料結構實驗圖論一:基於鄰接矩陣的廣度優先搜尋遍歷 Time Limit: 1000MS Memory limit: 65536K 題目描述 給定一個無向連通圖,頂點編號從0到n-1,用廣度優先搜尋(BFS)遍歷,輸出從某個頂點出發的遍歷序列。(同一個結點的

【演算法導論】廣度優先搜尋BFS

       圖的儲存方法:鄰接矩陣、鄰接表        例如:有一個圖如下所示(該圖也作為程式的例項): 則上圖用鄰接矩陣可以表示為: 用鄰接表可以表示如下: 鄰接矩陣可以很容易的用二維陣列表示,下面主要看看怎樣構成鄰接表:         鄰接表儲存方法是一種順

資料結構實驗基於鄰接矩陣/鄰接廣度優先搜尋

資料結構實驗圖論一:基於鄰接矩陣的廣度優先搜尋遍歷 Time Limit: 1000ms   Memory limit: 65536K  有疑問?點這裡^_^ 題目描述 給定一個無向連通圖,頂點編號從0到n-1,用廣度優先搜尋(BFS)遍歷,輸出從某個頂點出發的遍

2141 資料結構實驗基於鄰接矩陣廣度優先搜尋方法2

資料結構實驗之圖論一:基於鄰接矩陣的廣度優先搜尋遍歷 Time Limit: 1000MS Memory Limit: 65536KB Problem Description 給定一個無向連通圖,

資料結構實驗基於鄰接矩陣廣度優先搜尋__BFS

Problem Description 給定一個無向連通圖,頂點編號從0到n-1,用廣度優先搜尋(BFS)遍歷,輸出從某個頂點出發的遍歷序列。(同一個結點的同層鄰接點,節點編號小的優先遍歷) Input 輸入第一行為整數n(0< n <100),表示資料的組數。 對於每組

資料結構實驗基於鄰接廣度優先搜尋

Problem Description 給定一個無向連通圖,頂點編號從0到n-1,用廣度優先搜尋(BFS)遍歷,輸出從某個頂點出發的遍歷序列。(同一個結點的同層鄰接點,節點編號小的優先遍歷) Input 輸入第一行為整數n(0< n <100)

資料結構實驗基於鄰接廣度優先搜尋 sdut 2142

#include <stdio.h> #include <stdlib.h> #include<string.h> struct node { int data; struct node *next; }; struct n

SDUT 2142 資料結構實驗基於鄰接廣度優先搜尋

點選開啟題目連結#include <bits/stdc++.h> using namespace std; struct node { int data; node *next; }; node *head[1010], *p; void B

SDUT OJ 2413 資料結構實驗基於鄰接矩陣廣度優先搜尋

#include<iostream> #include<memory.h> using namespace std; int p[1010][1010]; int visit[110]; int c[1010]; int a=0; int b=

SDUTOJ 2141 ——資料結構實驗基於鄰接矩陣廣度優先搜尋

資料結構實驗圖論一:基於鄰接矩陣的廣度優先搜尋遍歷 Time Limit: 1000ms   Memory limit: 65536K  有疑問?點這裡^_^ 題目描述 給定一個無向連通圖,頂點編號從0到n-1,用廣度優先搜尋(BFS)遍歷,輸出從某個

SDUT 2141 資料結構實驗基於鄰接矩陣廣度優先搜尋

#include <bits/stdc++.h> using namespace std; bool vis[100]; int n, k, m, s, u, v; int Graph

資料結構實驗基於鄰接矩陣廣度優先搜尋 oj

  #include <stdio.h> int map[220][220],vis[220],ans[220]; int a = 0,b = 1; int k,t; void bfs(int n) { a++; for(int i = 0;i <k;i++) {

2141-資料結構實驗基於鄰接矩陣廣度優先搜尋

#include <iostream> #include <cmath> #include <cstdlib> #include <queue> #inc

資料結構實驗基於鄰接矩陣廣度優先搜尋

0 3 4 2 5 1 #include<stdio.h> #include<string.h> #include<stdlib.h> #define max 101 int book[max]; int df[max][max]; int num[max],queue

SDUT 2141 資料結構實驗基於鄰接矩陣廣度優先搜尋

資料結構實驗之圖論一:基於鄰接矩陣的廣度優先搜尋遍歷 Time Limit: 1000MS Memory Limit: 65536KB Problem Description 給定一個無向連通圖

sdutacm-資料結構實驗基於鄰接廣度優先搜尋

資料結構實驗之圖論二:基於鄰接表的廣度優先搜尋遍歷 Time Limit: 1000MSMemory Limit: 65536KB ProblemDescription 給定一個無向連通圖,頂點編號從0到n-1,用廣度優先搜尋(BFS)遍歷,輸出從某個頂點出發的遍歷序列。

資料結構實驗基於鄰接矩陣/鄰接廣度優先搜尋

#include<stdio.h> #include<stdlib.h> #include<string.h> typedef struct node { int u,v; struct node *next; } Lin; Lin *head[105

SDUTACM 資料結構實驗基於鄰接矩陣廣度優先搜尋

題目描述 給定一個無向連通圖,頂點編號從0到n-1,用廣度優先搜尋(BFS)遍歷,輸出從某個頂點出發的遍歷序列。(同一個結點的同層鄰接點,節點編號小的優先遍歷) 輸入 輸入第一行為整數n(0< n <100),表示資料的組數。 對於每組資料,第一行是三個整數k,

資料結構 筆記BFS

時間複雜度的對比分析   MatrixGraph ListGraph addVertex - O(n) removeVertex - O(n^2)