模板复习

并查集

 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 }

原文地址:https://www.cnblogs.com/wangyifan124/p/10928533.html

时间: 2024-10-17 06:08:11

模板复习的相关文章

模板复习【updating】

马上就要noi了--可能滚粗已经稳了--但是还是要复习模板啊 LCT: bzoj2049 1A 7min # include <stdio.h> # include <string.h> # include <iostream> # include <algorithm> // # include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long dou

强连通分量tarjan模板复习

对于一个有向图定点的子集,在该子集中任取两点u与v,都能找到一条从u到v的路径,则称该子集是强连通的.若该集合加入到任意点集中,它都不再强连通,则称这个子集是原图的一个强连通分量.任意一张图都可以分解成若干个不相交的强连通分量.这是强连通分量分解.把分解后的强连通分量缩成一个顶点,就可以得到一个有向无环图. 如图: 求一张图的强连通分量的个数,常用tarjan算法,它是基于对图深度优先搜索的算法,每个强连通分量为搜索树中的一棵子树.搜索时,把当前搜索树中未处理的节点加入一个堆栈,回溯时可以判断栈

C++函数模板复习

#include <iostream> #include <typeinfo> using namespace std; template <class T> T add(T one, T two) { cout << "类型:" << typeid(T).name() << endl; return one + two; // 函数模板只有在调用时才编译,有的编译器在初次编译时就会编译 } int add(int

省选前模板复习

PREFACE 也许是OI生涯最后一场正式比赛了,说是省选前模板,其实都是非常基础的东西,穿插了英文介绍和部分代码实现 祝各位参加JXOI2019的都加油吧 也希望今年JX能翻身,在国赛中夺金 数学知识 见数学知识小结 字符串 KMP算法Knuth-Morris-Pratt Algorithm KMP算法,又称模式匹配算法,是用来在一个文本串(text string)s中找到所有模式串(pattern)w出现的位置. 它是通过当失配(mismatch)发生时,模式串本身能提供足够的信息来决定下一

c++异常模板复习

1 // ConsoleApplication1.cpp : 定义控制台应用程序的入口点. 2 // 3 4 #include "stdafx.h" 5 #include<iostream> 6 #include"TEST2.h" 7 #include<exception> 8 using namespace std; 9 using std::cout; using std::endl; 10 11 template<typename

模板复习计划

20161111 BZOJ3224 普通平衡树|treap //BZOJ 3224 //by Cydiater //2016.11.11 #include <iostream> #include <cstring> #include <string> #include <algorithm> #include <queue> #include <map> #include <ctime> #include <cmat

hdu 1863 [【最小生成树】+hdu2544【floyed】+hdu1874【dijdtra】~~~模板复习~~~

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1863 #include<stdio.h> #include<string.h> #define inf 0x3f3f3f3f #define N 120 int book[N],w[N][N],min,dis[N]; int main() {     int m,n,i,j,t1,t2,t3,s,e,u,flag,sum;     while(scanf("%d%d"

线段树简单操作模板复习(忘了。)

// 参考博客 https://www.cnblogs.com/TheRoadToTheGold/p/6254255.html#4175712 // 懒人标记: 表示当前结点区间值已经改变,但是下面的区间还没改变.每次询问到有懒人标记的结点时,在进一步询问他的子节点时,下放标记(下放的同时改变了子结点的值,并且赋予子节点懒人标记)// 因此 当将要查询对应区间时候 之前修改区间的操作在此时才进行 -- 因此叫懒人标记 1 #include<cstdio> 2 using namespace s

复习啦。。

模板复习计划.图论:最短路:Dijkstra SPFA Floyed最小圈(*) 二维Dijkstra.SPFA倍增Floyed(*)最小生成树,各种生成树(里面有些还不会) 最小树形图二分图:各种概念,二分图最大权匹配(KM), HC算法(*)网络流:(把所有建模都看一遍,,,不管会不会的)费用流(最小/大费用最大流) ZKW费用流(*)最大流上下界网络流 动态规划:树型Dp(*).数位Dp决策单调性(*)插头DP斯坦纳树(*)记忆化搜索 字符串:后缀自动机.AC自动机.回文自动机,字符串 +