• 模板复习


    并查集

     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 ?'
    ' : ' ');
    46     return 0;
    47 }

    dijkstra

     1 /*
     2 problem:fuxi
     3 date:2019/5/26
     4 author:Lonely.Devil
     5 */
     6 
     7 #include<bits/stdc++.h>
     8 using namespace std;
     9 const int maxn = 1e5+5,inf = 1e9;
    10 struct node{
    11     int to,w;
    12 };
    13 vector<node>e[maxn];
    14 int d[maxn],p[maxn];
    15 int n,m,s;
    16 inline void add(int x,int y,int z){
    17     node t;
    18     t.to = y;
    19     t.w = z;
    20     e[x].push_back(t);
    21 }
    22 inline void dijkstra(){
    23     for(int i = 1;i <= n;i++)d[i] = inf;
    24     d[s] = 0;
    25     for(int i = 1;i <= n;i++){
    26         int mins = inf,k;
    27         for(int j = 1;j <= n;j++)
    28             if(!p[j] && d[j] < mins){
    29                 mins = d[j];k=j;
    30             }
    31         p[k]=1;
    32         for(int j = 0;j < e[k].size();j++){
    33             node v = e[k][j]; 
    34             if(d[v.to] > d[k]+v.w)d[v.to] = d[k]+v.w;
    35         } 
    36     }
    37 }
    38 int main(){
    39     cin>>n>>m>>s;
    40     for(int i=1,x,y,z;i<=m;i++){        
    41         scanf("%d%d%d",&x,&y,&z);
    42         add(x,y,z);
    43     }
    44     dijkstra();
    45     for(int i = 1;i <= n;i++)
    46         if(d[i] < inf)printf("%d ",d[i]);
    47         else printf("2147483647 ");
    48     return 0;
    49 }

    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
    ",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
    ",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
    ",ask(x));
    37     }
    38     return 0;
    39 }
  • 相关阅读:
    20201208史逸霏第六章学习笔记
    第三章笔记
    实现ls
    mystat
    改进ls
    第十章笔记
    cat userlist
    第七、八章笔记
    mypwd
    类成员函数指针写入map里怎么才能调用它呢?转载
  • 原文地址:https://www.cnblogs.com/wangyifan124/p/10928533.html
Copyright © 2020-2023  润新知