码迷,mamicode.com
首页 > 其他好文 > 详细

模板-深度优先搜索的前向星实现

时间:2017-10-07 19:38:33      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:模板   dfs   rom   div   using   col   tor   push   pac   

  最近学了前向星,非常爽,什么都想重新写一遍,哈哈哈......

  不说了,先拿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 }

 

模板-深度优先搜索的前向星实现

标签:模板   dfs   rom   div   using   col   tor   push   pac   

原文地址:http://www.cnblogs.com/wuyuema/p/7635405.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!