模板复习

Posted wangyifan124

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了模板复习相关的知识,希望对你有一定的参考价值。

并查集

 1 /*
 2 problem:fuxi
 3 date:2019/5/26
 4 author:Lonely.Devil
 5 */
 6 
 7 #include<iostream>
 8 #include<cstdio>
 9 #include<cstring>
10 using namespace std;
11 const int maxn = 1e5+5;
12 int father[maxn];
13 int n,m;
14 inline int find(int x){
15     if(father[x] == x)return x;
16     return father[x] = find(father[x]);
17 }
18 int main(){
19     cin>>n>>m;
20     for(int i = 1;i <= n;i++)father[i] = i;
21     for(int i = 1,op,x,y;i <= m;i++){
22         scanf("%d%d%d",&op,&x,&y);
23         if(op == 1)
24             father[find(x)] = find(y);
25         else if(find(x) == find(y))puts("Y");
26              else puts("N");
27     }
28     return 0;
29 }

 

spfa

 1 /*
 2 problem:fuxi
 3 date:2019/5/26
 4 author:Lonely.Devil
 5 */
 6 
 7 #include<iostream>
 8 #include<cstdio>
 9 #include<queue>
10 using namespace std;
11 const int maxn = 1e5+5,maxm = 1e6+5,inf = 1e9+5;
12 priority_queue< pair<int,int> >q;
13 int dist[maxn],p[maxn],begin[maxn],next[maxm],to[maxm],w[maxm],e;
14 int n,m,s;
15 inline void add(int x,int y,int z){
16     next[++e] = begin[x];
17     begin[x] = e;
18     to[e] = y;
19     w[e] = z;
20 }
21 inline void spfa(){
22     for(int i = 1;i <= n;i++)dist[i] = inf;
23     dist[s] = 0;
24     q.push(make_pair(0,s));
25     while(!q.empty()){
26         int tmp = q.top().second;
27         q.pop();
28         if(p[tmp])continue;
29         p[tmp] = 1;
30         for(int i = begin[tmp];i;i = next[i]){
31             int x = to[i];
32             if(dist[x] > dist[tmp]+w[i])
33                 dist[x] = dist[tmp]+w[i],q.push(make_pair(-dist[x],x));
34         }
35     }
36 }
37 int main(){
38     cin>>n>>m>>s;
39     for(int i = 1,x,y,z;i <= m;i++){
40         scanf("%d%d%d",&x,&y,&z);
41         add(x,y,z);
42     }
43     spfa();
44     for(int i = 1;i <= n;i++)
45         printf("%d%c",dist[i] == inf ? 2147483647 : dist[i],i == n ?\n :  );
46     return 0;
47 }

 

dijkstra

 1 /*
 2 problem:fuxi
 3 date:2019/5/26
 4 author:Lonely.Devil
 5 */
 6 
 7 #include<iostream>
 8 #include<cstdio>
 9 #include<queue>
10 using namespace std;
11 const int maxn = 1e5+5,maxm = 1e6+5,inf = 1e9+5;
12 struct edge{
13     int to,next,w;
14 }eg[maxm];
15 int e,begin[maxn];
16 int dist[maxn];
17 int n,m,s;
18 struct node{
19     int u,v;
20     bool operator <(const node &b) const{
21         return u > b.u;
22     }
23 };
24 priority_queue<node> Q;
25 inline void add(int x,int y,int z){
26     e++;
27     eg[e].next = begin[x];
28     begin[x] = e;
29     eg[e].to = y;
30     eg[e].w = z;
31 }
32 inline void dijkstra(){
33     for(int i = 1;i <= n;i++)dist[i] = inf;
34     dist[s] = 0;
35     node t;
36     t.u = 0;
37     t.v = s;
38     Q.push(t);
39     while(!Q.empty()){
40         int x = Q.top().v;
41         int y = Q.top().u;
42         Q.pop();
43         if(y != dist[x])continue;
44         for(int i = begin[x];i;i = eg[i].next){
45             int u = eg[i].to;
46             int v = eg[i].w;
47             if(dist[u] > dist[x]+v){
48                 dist[u] = dist[x]+v;
49                 node tmp;
50                 tmp.u = dist[u],tmp.v = u;
51                 Q.push(tmp);
52             }
53         }
54     }
55 }
56 int main(){
57     cin>>n>>m>>s;
58     for(int i = 1,x,y,z;i <= m;i++){
59         scanf("%d%d%d",&x,&y,&z);
60         add(x,y,z);
61     }
62     dijkstra();
63     for(int i = 1;i <= n;i++)
64         printf("%d%c",dist[i],i == n ?\n :  );
65     return 0;
66 }

 

Floyd

 1 /*
 2 problem:fuxi
 3 date:2019/5/26
 4 author:Lonely.Devil
 5 */
 6 
 7 #include<iostream>
 8 #include<cstdio>
 9 using namespace std;
10 const int maxn = 1e4,inf = 1e9+5;
11 int dist[maxn][maxn];
12 int n,m,s;
13 inline void Floyd(){
14     for(int k = 1;k <= n;k++)
15         for(int i = 1;i <= n;i++)
16             for(int j = 1;j <= n;j++)
17                 if(dist[i][j] > dist[i][k]+dist[k][j])
18                     dist[i][j] = dist[i][k]+dist[k][j]; 
19 }
20 int main(){
21     cin>>n>>m>>s;
22     for(int i = 1;i <= n;i++)
23         for(int j = 1;j <= n;j++)
24             if(i == j)dist[i][j] = 0;
25             else dist[i][j] = inf;
26     for(int i = 1,x,y,z;i <= m;i++){
27         scanf("%d%d%d",&x,&y,&z);
28         if(dist[x][y] != 0)dist[x][y] = min(z,dist[x][y]);
29         else dist[x][y] = z;
30     }
31     dist[s][s] = 0;
32     Floyd();
33     for(int i = 1;i <= n;i++)
34             if(dist[s][i] < inf)printf("%d ",dist[s][i]);
35             else printf("2147483647 ");
36     return 0;
37 }

 

堆排序

 1 /*
 2 problem:fuxi
 3 date:2019/5/26
 4 author:Lonely.Devil
 5 */
 6 
 7 #include<iostream>
 8 #include<cstdio>
 9 using namespace std;
10 int n,len;
11 int a[11100000];
12 inline void insert(int x){
13     a[++len] = x;
14     int k = len,tmp;
15     while(k > 1 && a[k] < a[k>>1]){
16         tmp = a[k];a[k] = a[k>>1];a[k>>1] = tmp;
17         k >>= 1;
18     }
19 }
20 int main(){
21     cin>>n;
22     for(int i = 1,k;i <= n;i++)scanf("%d",&k),insert(k);
23     for(int i = 1,t,tmp,k;i <= n;i++){
24         printf("%d ",a[1]);
25         a[1] = a[len];
26         len--;
27         k = 1;
28         while((a[k] > a[k<<1] && k<<1 <= len) || (a[k] < a[k<<1|1] && k<<1|1 <= len)){
29             tmp = k<<1;
30             if(a[tmp] > a[tmp+1] && tmp+1 <= len)tmp++;
31             t = a[k];a[k] = a[tmp];a[tmp] = t;
32             k = tmp;
33         }
34     }
35     return 0;
36 }

 

Prim

 1 /*
 2 problem:fuxi
 3 date:2019/5/26
 4 author:Lonely.Devil
 5 */
 6 
 7 #include<iostream>
 8 #include<cstdio>
 9 #include<cstring>
10 using namespace std;
11 const int maxn=2e5+5,maxm=12e5+5,inf=1000000000;
12 int e,to[maxm],w[maxm],begin[maxn],next[maxm];
13 int d[maxn],p[maxn],q[maxn],pos[maxn],ans,m,n,l;
14 void add(int x,int y,int z){
15     to[++e] = y;
16     next[e] = begin[x];
17     begin[x] = e;
18     w[e] = z;
19 }
20 inline void Prim(){
21     for(int i = 1;i <= n;i++)d[i] = inf;
22     d[1] = 0;l = n;
23     for(int i = 1;i <= n;i++){q[i]=i;pos[i]=i;}
24     for(int i = 1,k,u,v;i <= n;i++){
25         int tmp;
26         k = q[1];
27         q[1] = q[l];pos[q[l]] = 1;l--;
28         u = 1;
29         while((u*2 <= l && d[q[u]] > d[q[2*u]]) || (u*2+1 <= l && d[q[u]] > d[q[2*u+1]])){
30             v = u*2;
31             if(v+1 <= l && d[q[v]] > d[q[v+1]])v++;
32             tmp = q[u];q[u] = q[v];q[v] = tmp;
33             tmp = pos[q[u]];pos[q[u]] = pos[q[v]];pos[q[v]] = tmp;
34             u = v;
35         }
36         p[k] = 1;
37         ans += d[k];
38         for(int j = begin[k];j;j = next[j]){
39             u = to[j];
40             if(!p[u] && d[u] > w[j])d[u] = w[j];
41             v = pos[u];
42             while(v >= 1 && d[q[v]] < d[q[v/2]]){
43                 tmp = q[v];q[v]=q[v/2];q[v/2] = tmp;
44                 tmp = pos[q[v]];pos[q[v]] = pos[q[v/2]];pos[q[v/2]] = tmp;
45                 v = v/2;
46             }
47         }
48     }
49 }
50 int main(){
51     scanf("%d%d",&n,&m);
52     for(int i = 1,x,y,z;i <= m;i++){
53         scanf("%d%d%d",&x,&y,&z);
54         add(x,y,z);
55         add(y,x,z);                
56     }
57     Prim();
58     printf("%d\n",ans);    
59     return 0;
60 }

 

Kruscal

 1 /*
 2 problem:fuxi
 3 date:2019/5/26
 4 author:Lonely.Devil
 5 */
 6 
 7 #include<iostream>
 8 #include<cstdio>
 9 #include<algorithm>
10 using namespace std;
11 const int maxn = 2e5+5;
12 struct edge{
13     int x,y,z;
14 }e[maxn];
15 int father[maxn];
16 int n,m,cnt,sum;
17 inline bool cmp(edge a,edge b){
18     return a.z < b.z;
19 }
20 inline int find(int x){
21     if(father[x] == x)return x;
22     return father[x] = find(father[x]);
23 }
24 inline void Kruscal(){
25     sort(e+1,e+1+m,cmp);
26     for(int i = 1;i <= n;i++)father[i] = i;
27     for(int i = 1;i <= m;i++){
28         if(find(e[i].x) != find(e[i].y)){
29             sum += e[i].z;
30             cnt++;
31             father[find(e[i].x)] = find(e[i].y);
32         }
33         if(cnt == n-1)break;
34     }
35 }
36 int main(){
37     cin>>n>>m;
38     for(int i = 1,x,y,z;i <= m;i++)
39         scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].z);
40     Kruscal();
41     cout<<sum<<endl;
42     return 0;
43 }

 

tree1

 1 /*
 2 problem:fuxi
 3 date:2019/5/26
 4 author:Lonely.Devil
 5 */
 6 
 7 #include<iostream>
 8 #include<cstdio>
 9 using namespace std;
10 const int maxn = 5e5+5;
11 int tree[maxn<<2];
12 int n,m;
13 inline int lowbit(int x){
14     return x & -x;
15 }
16 inline void add(int x,int val){
17     while(x <= n){
18         tree[x] += val;
19         x += lowbit(x);
20     }
21 }
22 inline int ask(int x){
23     int res = 0;
24     while(x){
25         res += tree[x];
26         x -= lowbit(x);
27     }
28     return res;
29 }
30 int main(){
31     cin>>n>>m;
32     int a;
33     for(int i = 1;i <= n;i++)scanf("%d",&a),add(i,a);
34     for(int i = 1,op,x,y;i <= m;i++){
35         scanf("%d%d%d",&op,&x,&y);
36         if(op == 1)add(x,y);
37         if(op == 2)printf("%d\n",ask(y)-ask(x-1));
38     }
39     return 0;
40 }

 

tree2

 1 /*
 2 problem:fuxi
 3 date:2019/5/26
 4 author:Lonely.Devil
 5 */
 6 
 7 #include<iostream>
 8 #include<cstdio>
 9 using namespace std;
10 const int maxn = 5e5+5;
11 int tree[maxn];
12 int n,m,now,a;
13 inline int lowbit(int x){
14     return x & -x;
15 }
16 inline void add(int x,int val){
17     while(x <= n){
18         tree[x] += val;
19         x += lowbit(x);
20     }
21 }
22 inline int ask(int x){
23     int res = 0;
24     while(x){
25         res += tree[x];
26         x -= lowbit(x);
27     }
28     return res;
29 }
30 int main(){
31     cin>>n>>m;
32     for(int i = 1;i <= n;i++)scanf("%d",&a),add(i,a-now),now = a;
33     for(int i = 1,op,x,y,z;i <= m;i++){
34         scanf("%d",&op);
35         if(op == 1)scanf("%d%d%d",&x,&y,&z),add(x,z),add(y+1,-z);
36         if(op == 2)scanf("%d",&x),printf("%d\n",ask(x));
37     }
38     return 0;
39 }

 

以上是关于模板复习的主要内容,如果未能解决你的问题,请参考以下文章

VSCode自定义代码片段2——.vue文件的模板

VSCode自定义代码片段(vue主模板)

Eclipse 中的通用代码片段或模板

动态SQL基础概念复习(Javaweb作业5)

复习笔记——C++模板

调用模板化成员函数:帮助我理解另一个 *** 帖子中的代码片段