简介

说点没用的

简单介绍一下代码风格

可能某ghj1222大佬看了会有些不舒服

#include<iostream>
#include<cstdio>
#include<algorithm>

include后面不习惯加空格

头文件尽量简短

using namespace std;

OI中可以using std

const int maxn=1e5+5;

int a[maxn];

定义数组空间必须使用const定义大小

inline lowbit(int x){return x&-x;}

简短的函数可用inline内联定义

typedef pair<int,int> pi;

长的类型名尽量用typedef定义别名

总之少用define多用const,typedef,inline

int main(){
    
    return 0;
}

大括号不需换行,简短时可压行

void func(int x){return x;}

不喜欢多余的空格

int main(){
    int n;
    cin>>n;
    for(int i=1;i<=n;++i){
        cin>>a[i];
        if(a[i]>0)ans++;
    }
    cout<<ans<<endl;
    return 0;
}
int main(){
    int n;
    cin>>n;
    for(int i=1;i<=n;++i){cin>>a[i];if(a[i]>0)ans++;}
    cout<<ans<<endl;
    return 0;
}

个人习惯用++i

bool cmp(const int &a,const int &b){return a<b;}

用于比较的cmp函数必须加const和&(不然cena的垃圾编译器会炸)

链式前向星习惯用指针写(还是指针和链表最配/网络流除外)

struct ED{
    int to;
    int dis;
    ED* nt;
};
ED* head[maxn];
void add(int x,int y,int z){
    ED* p=new ED;
    p->to=y;p->dis=z;p->nt=head[x];head[x]=p;
}

λ表达式可站多行,也可压为一行

sort(a+1,a+1+n,[](const leizi &a,const leizi &b){return a.num<b.num;});

sort(a+1,a+1+n,[](const leizi &a,const leizi &b){
    return a.num<b.num;
});

while(l<r){
    int mid=(l+r>>1)+1;
    if([](int x){
        int pos=0,ans=0;
        for(int i=1;i<=n+1;++i){
            if(a[i]-a[pos]<x){ans++;}
            else pos=i;
        }
        return ans<=m;
    }(mid))l=mid;
    else r=mid-1;
}

对于复杂的类型可用auto自动识别(C++11)

void bfs(int s){
    memset(vis_t,0,sizeof vis_t);
    queue<int>q;
    q.push(s);
    vis_t[s]=1;
    while(!q.empty()){
        auto tp=q.front();q.pop();
        for(auto i=head[tp];i;i=i->nt){
            if(!vis_t[i->to]){
                vis_t[i->to]=1;
                q.push(i->to);
            }
        }
    }
}

自己写的min和max最好放在namespace里面

namespace cmp{
    template<typename T>T min(const T &a,const T &b){return a<b? a:b;}
    template<typename T>T max(const T &a,const T &b){return a<b? b:a;}
    template<typename T>void minn(T &a,const T &b){if(b<a)a=b;}
    template<typename T>void maxx(T &a,const T &b){if(a<b)a=b;}
}

指针类型最好用typedef定义别名(估计只有我才会写指针)

struct pt{
    int siz;
    int cnt;
    int k;
    pt *fa;
    pt *ch[2];
};
typedef pt* ptr;
ptr root;
void build(ptr root){...}//或者
void build(ptr &root){...}

最后放篇代码吧(luogu2296寻找道路)

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int maxn=1e4+5;
const int maxm=2e5+5;
struct ED{
    int to;
    ED* nt;
};
ED* head[maxn];
int n,m,s,t;
int vis_f[maxn];
int vis_s[maxn];
int dis[maxn];
void add(int x,int y){
    ED* p=new ED;
    p->to=y;p->nt=head[x];head[x]=p;
}
void bfs(int s){
    memset(vis_f,0,sizeof vis_f);
    queue<int>q;
    q.push(s);
    vis_f[s]=1;
    while(!q.empty()){
        auto tp=q.front();q.pop();
        for(auto i=head[tp];i;i=i->nt){
            if(!vis_f[i->to]){
                vis_f[i->to]=1;
                q.push(i->to);
            }
        }
    }
}
int main(){
    cin>>n>>m;
    for(int i=1;i<=m;++i){
        int x,y;
        cin>>x>>y;
        if(x!=y){
            add(y,x);
        }
    }
    cin>>s>>t;
    bfs(t);
    for(int i=1;i<=n;++i){
        if(!vis_f[i]){
            vis_s[i]=1;
            for(auto j=head[i];j;j=j->nt){
                vis_s[j->to]=1;
            }
        }
    }
    cout<<[](){
        queue<int> q;
        q.push(t);
        while(!q.empty()){
            auto tp=q.front();q.pop();
            for(auto i=head[tp];i;i=i->nt){
                if(!vis_s[i->to]){
                    dis[i->to]=dis[tp]+1;
                    vis_s[i->to]=1;
                    q.push(i->to);
                }
            }
        }
        return dis[s]==0? -1:dis[s];
    }()<<endl;
    return 0;
}

转载于:https://www.cnblogs.com/eric-walker/p/9574728.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值