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

各种模板(part 2)

时间:2016-11-17 20:26:53      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:images   style   amp   定义   for   and   one   return   ges   

堆:

技术分享
 1 using namespace dui //
 2 {
 3     #include<queue> //需要的库
 4     priority_queue < int > Q; //定义一个Q的大根堆,top()为较大的
 5     priority_queue < int,vector < int >,greater< int > > q; //定义一个q的小根堆。top()为较小的
 6     q.empty() //判断队空
 7     q.push(x) //将x放入队列
 8     q.pop() //将队头删除
 9     q.size() //返回堆中元素个数
10     q.top() //返回队头
11     
12     //在结构体中自己定义优先级
13     struct node
14     {
15         int id,v;
16         friend bool operator < (node n1, node n2)
17         {
18             return n1.v>n2.v; //要满足的条件
19         }
20     }f[maxn];
21     priority_queue<node>q;
22 }
View Code

 

图的建立+遍历

技术分享
 1 using namespace tu //
 2 {
 3     int len=0,linkk[maxn];
 4     struct node //邻接表
 5     {
 6         int x,y,v;
 7     }e[maxn];
 8     init(int xx,int yy,int vv)
 9     {
10         e[++len].y=linkk[xx];linkk[xx]=len;
11         e[len].x=yy;e[len].v=vv;
12     }
13     
14     struct node    // 边表
15     {
16         int x,y;
17     }e[maxn];
18     
19     void dfs(int k) //邻接矩阵的dfs
20     {
21         for(int i=1;i<=n;i++)
22         {
23             if((a[k][i])&&(!f[i]))
24             {
25                 f[i]=1;
26                 dfs(i);
27             }
28         }
29     }
30     
31     void dfs(int k) //邻接表的dfs
32     {
33         for(int i=linkk[k];i;i=e[i].y)
34         {
35             if(!f[e[i].x])
36             {
37                 f[e[i].x]=1;
38                 dfs(e[i].x);
39             }
40         }
41     }
42 }
View Code

 

各种模板(part 2)

标签:images   style   amp   定义   for   and   one   return   ges   

原文地址:http://www.cnblogs.com/zyker/p/6074524.html

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