圖結構練習——BFS——從起始點到目標點的最短步數(鄰接表+BFS),bfs鄰接
圖練習-BFS-從起點到目標點的最短步數Time Limit: 1000ms Memory limit: 65536K 有疑問?點這裡^_^題目描述在古老的魔獸傳說中,有兩個軍團,一個叫天災,一個叫近衛。在他們所在的地區,有n個隘口,編號為1..n,某些隘口之間是有通道串連的。其中近衛軍團在1號隘口,天災軍團在n號隘口。某一天,天災軍團的領袖巫妖王決定派兵攻打近衛軍團,天災軍團的部隊如此龐大,甚至可以填江過河。但是巫妖王不想付出不必要的代價,他想知道在不修建任何通道的前提下,部隊是否可以通過隘口及其相關通道到達近衛軍團展開攻擊;如果可以的話,最少需要經過多少通道。由於n的值比較大(n<=100000),於是巫妖王找到了擅長編程的你 =_=,請你幫他解決這個問題,否則就把你吃掉變成他的魔法。為了拯救自己,趕緊想辦法吧。輸入輸入包含多組,每組格式如下。第一行包含兩個整數n(n <= 100000),m(m <= 200000)(分別代表n個隘口,這些隘口之間有m個通道)。下面m行每行包含兩個整數a,b;表示從a出發有一條通道到達b隘口(注意:通道是雙向的)。輸出如果天災軍團可以不修建任何通道就到達1號隘口,那麼輸出最少經過多少通道,否則輸出NO。樣本輸入
2 11 22 12 1
樣本輸出
11
提示第一次用鄰接表,無奈結構體數組類比的沒看懂,又不想敲鏈表,只好用vector實現了,期間我發現了一個問題是,vector定義完要clear一下才能用!不然就是WA
#include <iostream>#include <cstdio>#include <cstring>#include <vector>#include <queue>#include <algorithm>using namespace std;typedef struct node{ int point; int step;};vector <int> p[100010];bool vis[100010];void bfs(int n){queue <node> Q;node t;t.step=0;t.point=n;vis[n]=1;Q.push(t);while(!Q.empty()){node v=Q.front();Q.pop(); if(v.point==1){printf("%d\n",v.step);return ;}vector <int>::iterator it=p[v.point].begin();while(it!=p[v.point].end()){if(!vis[*it]){vis[*it]=1;t.point=*it;t.step=v.step+1;Q.push(t);}it++;}}puts("NO");}int main(){int n,m,u,v;while(~scanf("%d%d",&n,&m)){memset(vis,0,sizeof(vis));for(int i=1;i<=n;i++)p[i].clear();//很重要while(m--){scanf("%d%d",&u,&v);if(u!=v){ p[u].push_back(v); p[v].push_back(u);}}bfs(n);}return 0;}
用c語言編程 1建立圖的鄰接矩陣與鄰接表 2驗證圖的深度優先、廣度優先遍曆演算法 3驗證最短路徑
這些是c++的代碼不知是否滿足你的要求。
1、鄰接表表示的圖中分別用DFS和BFS遍曆
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Description: 圖的鄰接表的結點
struct Edge
{
int dest; // 目標結點下標
// int value; // 路徑長度
Edge *link; // 下一個結點
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////......餘下全文>>
廣度優先法(BFS)演算法
程式碼:#include <iostream.h>
#include <stdlib.h>
const int verMax=9;
const int queMax=10;
struct node//聲明圖形頂點結構
{
int vertex;
struct node *next;
};
typedef struct node *graph;
struct node head[verMax];//聲明頂點結構數組int que[queMax];
int front=-1;
int rear=-1;int visit[verMax];//尋找記錄//隊列的存入
int enque(int v)
{
if(rear>=queMax)//隊列已滿
return -1;
else
{
rear++;//對了尾端指標後移
que[rear]=v;//將值存入隊列中
return 1;
}
}
//隊列的取出
int deque()
{
if(front==rear)//隊列已空
return -1;
else
{
front++;
return que[front];
}
}
//廣度優先搜尋法
void DFS(int v)
{
graph point;
enque(v);//存入隊列
visit[v]=1;//已尋找
cout<<"["<<v<<"]==>";
while(front!=rear)
{
v=deque();
point=head[v].next;
while(point!=NULL)//讀入鄰接列表所有頂點
{
if(visit[point->vertex]==0)
{
enque(point->vertex);//存入隊列
visit[point->vertex]=1;//已尋找過的頂點
cout<<"["<<point->vertex<<"]==>";
}
point=point->next;
}
}
}
//建立鄰接頂點至鄰接列表內
void create_graph(int v1,int v2)
{
graph point,New;
New=(graph)malloc(sizeof(struct node));
if(New!=NULL)
{
New->vertex=v2;//鄰近頂點
New->next=NULL;
point=&(head[v1]);
while(point->next!=NULL)
point=point->next;
point->next=New;//串聯在列表尾端
}
}
//輸出鄰接列表內資料
void print_graph(struct node *head)
{
graph p......餘下全文>>