C++竞赛模板自取
·
Cpp template
From BJHAK
基础模板template
#include<bits/stdc++.h>
#define inf (long long)0x3f3f3f3f3f3f3f3f
#define sup (long long)0xc0c0c0c0c0c0c0c0
#define int long long
using namespace std;
const int e=1e3+10;
void file(const string topic){
freopen((topic+".in").c_str(),"r",stdin);
freopen((topic+".out").c_str(),"w",stdout);
return;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cout<<fixed<<setprecision(0);
// file("template");
return 0;
}
随机数据Random data
#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
signed main(){
srand(time(0)+clock());
cout<<(rand()%mod+1)<<'\n';
return 0;
}
对拍他测Edisonba
#include<bits/stdc++.h>
#define data (string)"data"
#define std_ (string)"std"
using namespace std;
const int e=10;
bool edisonba(const string num){
system((std_+".exe < "+data+num+".in > "+std_+".out").c_str());
return !system(("fc "+data+num+".out "+std_+".out"));;
}
signed main(){
for(int i=1;i<=e;i++)
if(edisonba(to_string(i))==0){
cout<<i<<'\n';
break;
}
return 0;
}
对拍自测Edisonba
#include<bits/stdc++.h>
#define std_ (string)"std"
#define force (string)"force"
#define data (string)"data"
using namespace std;
const int n=10;
bool edisonba(){
system((data+".exe > "+data+".in").c_str() );
system((force+".exe < "+data+".in > "+std_+".ans").c_str() );
system((std_+".exe < "+data+".in > "+std_+".out").c_str() );
return !system(("fc "+std_+".out "+std_+".ans").c_str());
}
signed main(){
for(int i=1;i<=n;i++)
if(edisonba()==0){
cout<<i<<'\n';
break;
}
return 0;
}
数据结构
并查集 Unite_find set
const int e=1e3+10;
int n;
class uf_set{
private:
vector<int>d;
public:
uf_set(int size_){
d.resize(e);
for(int i=1;i<=size_;i++)
d[i]=i;
}
int find(int h){
return d[h]==h?h:d[h]=find(d[h]);
}
void unite(int u,int v){
int fu=find(u),fv=find(v);
d[fu]=fv;
return;
}
};
使用方法:
uf_set a(10); //创建一个大小为10的并查集
a.find(1);//寻找祖先
a.unite(1,2);//合并1,2子集
手打堆 Heap
用法同并查集(那我咋不用优先队列)
const int e=1e3+10;
int n;
class heap{// max_root
private:
vector<int>a;
int top;
public:
heap(){
a.resize(e);
a.push_back(0);
top=0;
}
void push_up(int h){
int root=h;
while(root>1&&a[root]>a[root/2]){
swap(a[root],a[root/2]);
root/=2;
}
return;
}
void push_down(int h){
int root=h;
while(root*2<=top){
root*=2;
if(root+1<=top&&a[root+1]>a[root])
root++;
if(a[root]<=a[h])break;
swap(a[root],a[h]);
h=root;
}
return;
}
void insert(int x){
a[++top]=x;
push_up(top);
return;
}
bool del(){
if(top==0)return 0;
swap(a[1],a[top]);
top--;
push_down(1);
return 1;
}
};
数论
欧拉筛Prime
const int e=1e3+10;
int n;
int isprime[e],prime[e],top;
void init(){
for(int i=1;i<=n;i++)
isprime[i]=1;
return;
}
void find_prime(){
init();
for(int i=2;i<=n;i++){
if(isprime[i]==1)
prime[++top]=i;
for(int j=1;j<=top&&i*prime[j]<=n;j++){
isprime[i*prime[j]]=0;
if(i%prime[j]==0)break;
}
}
return;
}
快速幂Fast_pow
const int mode=1e9+7;
int fast_pow(int x,int y){
int res=1;
while(y){
if(y%2==1)
res=res*x%mod;
x=x*x%mod,y/=2;
}
return res;
}
图论
多源最短路Floyd
const int e=1e3+10;
int n,m,mp[e][e];
void read(){
memset(mp,0x3f,sizeof(mp));
for(int i=1;i<=m;i++){
int u,v,w;
cin>>u>>v>>w;
mp[u][v]=mp[v][u]=w;
}
return;
}
void init(){
for(int i=1;i<=n;i++)
mp[i][i]=0;
return;
}
void floyd(){
read();
init();
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
mp[i][j]=min(mp[i][j],mp[i][k]+mp[k][j]);
return;
}
单源最短路Dijkstra
int n,m;
int dis[e];
bool vis[e];
struct edge{
int v,w;
};vector<edge>mp[e];
struct node{
int h,w;
bool operator < (const node &y)const{
return w<y.w;
}
bool operator > (const node &y)const{
return w>y.w;
}
};
void init(){
memset(dis,0x3f,sizeof(dis));
dis[1]=0;
return;
}
void dijkstra(){
init();
priority_queue<node,vector<node>,greater<node> >q;
q.push({1,0});
while(!q.empty()){
node p=q.top();
q.pop();
if(vis[p.h])continue;
vis[p.h]=1;
for(edge i:mp[p.h])
if(i.w+p.w<dis[i.v]){
dis[i.v]=i.w+p.w;
q.push({i.v,dis[i.v]});
}
}
return;
}
最小生成树Kruskal
const int e=5e5+10;
int n,m,ans;
class uf_set{
private:
vector<int>d;
public:
uf_set(int size_){
d.resize(e);
for(int i=1;i<=size_;i++)
d[i]=i;
}
int find(int h){
return d[h]==h?h:d[h]=find(d[h]);
}
void unite(int u,int v){
int fu=find(u),fv=find(v);
d[fu]=fv;
return;
}
bool f(int u,int v){
int fu=find(u),fv=find(v);
return fu==fv;
}
};
struct edge{
int u,v,w;
bool operator < (const edge &y)const{
return w<y.w;
}
bool operator > (const edge &y)const{
return w>y.w;
}
}b[e];
void kruskal(){
uf_set a(n);
sort(b+1,b+m+1,less<edge>());
int cnt=0;
for(int i=1;i<=m;i++){
int u=b[i].u,v=b[i].v;
if(a.f(u,v))continue;
a.unite(u,v);
ans+=b[i].w,cnt++;
if(cnt==n-1)break;
}
if(cnt!=n-1)
ans=-1;
return;
}
最小生成树Prim
int n,m,ans;
int dis[e];
bool vis[e];
struct edge{
int v,w;
};vector<edge>mp[e];
struct node{
int h,w;
bool operator < (const node &y)const{
return w<y.w;
}
bool operator > (const node &y)const{
return w>y.w;
}
};
void init(){
memset(dis,0x3f,sizeof(dis));
dis[1]=0;
return;
}
void prim(){
init();
priority_queue<node,vector<node>,greater<node> >q;
q.push({1,0});
int cnt=0;
while(!q.empty()){
node p=q.top();
q.pop();
if(vis[p.h])continue;
vis[p.h]=1,ans+=dis[p.h],cnt++;
for(edge i:mp[p.h])
if(i.w<dis[i.v]){
dis[i.v]=i.w;
q.push({i.v,dis[i.v]});
}
}
if(cnt!=n)
ans=-1;
return;
}
拓扑排序Topo
const int e=1e3+10;
int n,dg[e],a[e],top;
vector<int>mp[e];
void init(){
for(int i=1;i<=n;i++)
for(auto j:mp[i])
dg[j]++;
return;
}
void topo(){
init();
queue<int>q;
for(int i=1;i<=n;i++)
if(dg[i]==0)
q.push(i);
while(q.size()){
int p=q.front();
q.pop();
a[++top]=p;
for(auto i:mp[p]){
dg[i]--;
if(dg[i]==0)
q.push(i);
}
}
return;
}
更多推荐

所有评论(0)