P1001_a+b问题算法汇总

搬运洛谷博客中……qaq
以下是a+b问题各种解法的汇总(我真无聊),有些是我自己写的,有些是别人写的。

注意:此篇博客我标了原创,但有些内容是转载的

普通

#include <iostream>

using namespace std;

int main()
{
    cin>>a>>b;
    cout<<a+b;
    return 0;
}

读入优化


#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;

int read()
{
    int out=0,fh=1;
    char cc=getchar();
    if (cc=='-')
        fh=-1;
    while (cc>'9'||cc<'0')
        cc=getchar();
    while (cc>='0'&&cc<='9')
    {
        out=out*10+cc-'0';
        cc=getchar();
    }
    return out*fh;
}

void write(int x)  
{  
    if (x==0)
    {
        putchar('0');
        return;
    }
    int num = 0;
    char c[15];
    while (x)
        c[ ++num ] = (x%10)+48,x /= 10;
    while (num)
        putchar(c[ num-- ]);
    putchar(' '); 
}
int main(){
    write(read()+read());
    return 0;
}

SPFA

#include<cstdio>
#include<iostream>

using namespace std;

int n,m,a,b,op,head[200009],next[200009],dis[200009],len[200009],v[200009],l,r,team[200009],pd[100009],u,v1,e;

int lt(int x,int y,int z)
{
    op++;
    v[op]=y;
    next[op]=head[x],head[x]=op,len[op]=z;
}
int SPFA(int s,int f)
{
    for (int i=1;i<=200009;i++)
        dis[i]=999999999;
    l=0,r=1,team[1]=s,pd[s]=1,dis[s]=0;
    while (l!=r)
    {
        l=(l+1)%90000,u=team[l],pd[u]=0,e=head[u];
        while (e!=0)
        {
            v1=v[e];
            if (dis[v1]>dis[u]+len[e])
            {
                dis[v1]=dis[u]+len[e];
                if (!pd[v1])
                {
                    r=(r+1)%90000,
                    team[r]=v1,
                    pd[v1]=1;
                }
            }
            e=next[e];
        } 
    }
    return dis[f];
}
int main()
{
    cin>>a>>b;
    lt(1,2,a);
    lt(2,3,b);
    cout<<SPFA(1,3);
    return 0;
}

Floyd

#include<iostream>
#include<cstring>

using namespace std;

int main()
{
    long long n=3,a,b,dis[4][4];
    cin>>a>>b;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            dis[i][j]=2147483647;
    dis[1][2]=a;
    dis[2][3]=b;

    for (int k=1;k<=n;k++)
        for (int i=1;i<=n;i++)
            for (int j=1;j<=n;j++)
                dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
    cout<<dis[1][3];
}

压位+高精度

#include <cstdio>  
#include <cstring>  
#include <cstdlib>  
#include <iostream>  

using namespace std;  

const int p=8;
const int carry=100000000;
const int Maxn=50001;  
char s1[Maxn],s2[Maxn];  
int a[Maxn],b[Maxn],ans[Maxn]; 

int change(char s[],int n[])   
{  
    char temp[Maxn];   
    int len=strlen(s+1),cur=0;  
    while(len/p)
    {  
        strncpy(temp,s+len-p+1,p);
        n[++cur]=atoi(temp); 
        len-=p;
    }  
    if(len)
    {
        memset(temp,0,sizeof(temp));  
        strncpy(temp,s+1,len);  
        n[++cur]=atoi(temp);   
    }  
    return cur;
}  
int add(int a[],int b[],int c[],int l1,int l2)  
{  
    int x=0,l3=max(l1,l2);  
    for(int i=1;i<=l3;i++)
    {  
        c[i]=a[i]+b[i]+x;  
        x=c[i]/carry;
        c[i]%=carry;  
    }  
    while(x>0)
    {
        c[++l3]=x%10;
        x/=10;
    }  
    return l3;
}  
void print(int a[],int len)  
{   
    cout<<a[len];
    for(int i=len-1;i>=1;i--)
        printf("%0*d",p,a[i]);
    cout<<endl;  
}  
int main()  
{
    scanf("%s%s",s1+1,s2+1);
    int la=change(s1,a);
    int lb=change(s2,b);
    int len=add(a,b,ans,la,lb);    
    print(ans,len);
}  

线段树

#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iostream>

using namespace std;

struct node
{
    int val,l,r;
}t[5];

int a[5],f[5];
int n,m;

void init()
{
    for(int i=1;i<=2;i++)
        scanf("%d",&a[i]);
}

void build(int l,int r,int node)
{
    t[node].l=l;
    t[node].r=r;
    t[node].val=0;
    if(l==r)
    {
        f[l]=node;
        t[node].val=a[l];
        return;
    }
    int mid=(l+r)>>1;
    build(l,mid,node*2);
    build(mid+1,r,node*2+1);
    t[node].val=t[node*2].val+t[node*2+1].val;
}

void update(int node)
{
    if (node==1)return;
    int fa=node>>1;
    t[fa].val=t[fa*2].val+t[fa*2+1].val;
    update(fa);
}

int find(int l,int r,int node)
{
    if (t[node].l==l&&t[node].r==r)
        return t[node].val;
    int sum=0;
    int rc=lc+1;
    if (t[lc].r>=l)
    {
        if (t[lc].r>=r)
            sum+=find(l,r,lc);
        else
            sum+=find(l,t[lc].r,lc);
    }
    if (t[rc].l<=r)
    {
        if (t[rc].l<=l)
            sum+=find(l,r,rc);
        else
            sum+=find(t[rc].l,r,rc);
    }
    return sum;
}

int main()
{
    init();
    build(1,2,1);
    printf("%d",find(1,2,1));
}

最小生成树

#include <cstdio>
#include <algorithm>

const int INF=2147483647;

using namespace std;

struct tree
{
    int x,y,t;
}a[10];

bool cmp(const tree&a,const tree&b)
{
    return a.t<b.t;
}

int f[11],i,j,k,n,m,x,y,t,ans;

int root(int x)
{
    if (f[x]==x)
        return x;
    f[x]=root(f[x]);
    return f[x];
}

int main()
{
    for (i=1;i<=10;i++)
        f[i]=i;
    for (i=1;i<=2;i++)
    {
        scanf("%d",&a[i].t);
        k++;
    }
    a[++k].x=1;
    a[k].y=3,a[k].t=INF;
    sort(a+1,a+1+k,cmp);
    for (i=1;i<=k;i++)
    {
        x=root(a[i].x);
        y=root(a[i].y);
        if (x!=y)
            f[x]=y,ans+=a[i].t; 
    }
    printf("%d\n",ans);
}

LCT

#include<cstring>
#include<cstdio>
#include<cstring>
#include<iostream>

using namespace std;

struct node 
{
    int data,rev,sum;
    node *son[2],*pre;
    bool judge();
    bool isroot();
    void pushdown();
    void update();
    void setson(node *child,int lr);
}lct[233];

int top,a,b;

node *getnew(int x)
{
    node *now=lct+ ++top;
    now->data=x;
    now->pre=now->son[1]=now->son[0]=lct;
    now->sum=0;
    now->rev=0;
    return now;
}

bool node::judge()
{
    return pre->son[1]==this;
}

bool node::isroot()
{
    if(pre==lct)
        return true;
    return !(pre->son[1]==this||pre->son[0]==this);
}

void node::pushdown()
{
    if(this==lct||!rev)
        return;
    swap(son[0],son[1]);
    son[0]->rev^=1;
    son[1]->rev^=1;
    rev=0;
}

void node::update()
{
    sum=son[1]->sum+son[0]->sum+data;
}

void node::setson(node *child,int lr)
{
    this->pushdown();
    child->pre=this;
    son[lr]=child;
    this->update();
}

void rotate(node *now)
{
    node *father=now->pre,*grandfa=father->pre;
    if(!father->isroot())
        grandfa->pushdown();
    father->pushdown();
    now->pushdown();
    int lr=now->judge();
    father->setson(now->son[lr^1],lr);
    if(father->isroot())
        now->pre=grandfa;
    else
        grandfa->setson(now,father->judge());
    now->setson(father,lr^1);
    father->update();
    now->update();
    if(grandfa!=lct)
        grandfa->update();
}
void splay(node *now)
{
    if(now->isroot())
        return;
    for(;!now->isroot();rotate(now))
        if(!now->pre->isroot())
            now->judge()==now->pre->judge()?rotate(now->pre):rotate(now);
}

node *access(node *now)
{
    node *last=lct;
    for(;now!=lct;last=now,now=now->pre)
    {
        splay(now);
        now->setson(last,1);
    }
    return last;
}

void changeroot(node *now)
{
    access(now)->rev^=1;
    splay(now);
}

void connect(node *x,node *y)
{
    changeroot(x);
    x->pre=y;
    access(x);
}

void cut(node *x,node *y)
{
    changeroot(x);
    access(y);
    splay(x);
    x->pushdown();
    x->son[1]=y->pre=lct;
    x->update();
}

int query(node *x,node *y)
{
    changeroot(x);
    node *now=access(y);
    return now->sum;
}

int main()
{
    scanf("%d%d",&a,&b);
    node *A=getnew(a);
    node *B=getnew(b);
    connect(A,B);
    cut(A,B);
    connect(A,B);
    printf("%d\n",query(A,B)); 
    return 0;
}

结束了。。。如果你看完了每种方法,,看来你和我一样无聊?

猜你喜欢

转载自blog.csdn.net/cxxchen_/article/details/80770071