1. 程式人生 > >模板-深度優先搜索的前向星實現

模板-深度優先搜索的前向星實現

log from using struct code als max cnblogs 說了

  最近學了前向星,非常爽,什麽都想重新寫一遍,哈哈哈......

  不說了,先拿dfs開刀。

 1 #include <cstdio>
 2 #include <vector>
 3 using namespace std;
 4 
 5 const int MAXN=100;
 6 struct node {
 7     int to;
 8     int w;
 9 };
10 vector <node> map[MAXN];
11 bool s[MAXN]={false};
12 
13 void dfs(int x) {
14     s[x]=true
; 15 printf("%d\n",x); 16 vector <node>::iterator it; 17 for (it=map[x].begin();it!=map[x].end();it++) { 18 node tmp= *it; 19 if (!s[tmp.to]) dfs(tmp.to); 20 } 21 } 22 23 int main() { 24 int n,m; 25 scanf("%d%d",&m,&n); 26 int k,i,j,w; 27 for
(k=1;k<=m;k++) { 28 scanf("%d%d%d",&i,&j,&w); 29 node e; 30 e.to=j; 31 e.w=w; 32 map[i].push_back(e); 33 } 34 dfs(1);//dfs from Node 1 35 return 0; 36 }

模板-深度優先搜索的前向星實現