a+b problem 从入门到跳楼

1.普通做法

 

3.dijkstra

 

4.spfa

 

5.kruskal

 

6.prim

 

7.线段树

 
  • #include<bits/stdc++.h>
  • using namespace std;
  • int main(){
  • int a,b;
  • cin>>a>>b;
  • cout<<a+b;
  • return 0;
  • }
  • 2.二分

     
  • #include<bits/stdc++.h>
  • using namespace std;
  • int main(){
  • int a,b;
  • cin>>a>>b;
  • long long l=-3e9,r=3e9,ans;
  • while(l<=r){
  • long long mid=(l+r)>>1;
  • if(a+b>=mid)ans=mid,l=mid+1;
  • else r=mid-1;
  • }
  • cout<<ans;
  • return 0;
  • }
  • #include<bits/stdc++.h>
  • using namespace std;
  • int head[4],cnt,dst[4];
  • struct zqs{int to,w,nxt;} edge[4];
  • void add(int u,int v,int w){edge[++cnt]={v,w,head[u]};head[u]=cnt;}
  • struct H{
  • int id,dis;
  • bool operator<(const H &o)const{return dis>o.dis;}
  • };
  • bool vis[4];
  • void dijkstra(){
  • priority_queue<H> q;
  • memset(dst,0x7f,sizeof dst);
  • q.push({1,0}),dst[1]=0;
  • while(!q.empty()){
  • H t=q.top(); q.pop();
  • for(int i=head[t.id];i;i=edge[i].nxt){
  • int v=edge[i].to;
  • if(dst[v]>dst[t.id]+edge[i].w){
  • dst[v]=dst[t.id]+edge[i].w;
  • if(!vis[v])vis[v]=true,q.push({v,dst[v]});
  • }
  • }
  • }
  • }
  • int main(){
  • int a,b;
  • cin>>a>>b;
  • add(1,2,a),add(2,3,b);
  • dijkstra(),cout<<dst[3];
  • return 0;
  • }
  • #include<bits/stdc++.h>
  • using namespace std;
  • int head[5],cnt,dst[5];
  • struct zqs{int to,w,nxt;} edge[5];
  • void add(int u,int v,int w){edge[++cnt]={v,w,head[u]};head[u]=cnt;}
  • bool vis[5];
  • void spfa(){
  • queue<int> q; q.push(1);
  • fill(dst+1,dst+4,2e9),dst[1]=0;
  • memset(vis,false,sizeof vis);
  • while(!q.empty()){
  • int u=q.front();
  • q.pop(),vis[u]=false;
  • for(int i=head[u];i;i=edge[i].nxt){
  • int v=edge[i].to;
  • if(dst[v]>dst[u]+edge[i].w){
  • dst[v]=dst[u]+edge[i].w;
  • if(!vis[v])vis[v]=true,q.push(v);
  • }
  • }
  • }
  • }
  • int main(){
  • int a,b;
  • cin>>a>>b;
  • add(1,2,a),add(2,3,b);
  • spfa(),cout<<dst[3];
  • return 0;
  • }
  • #include<bits/stdc++.h>
  • using namespace std;
  • int fa[3];
  • struct zqs{int u,v,w;} edge[3];
  • int find(int x){
  • if(fa[x]==x)return x;
  • return fa[x]=find(fa[x]);
  • }
  • bool cmp(zqs x,zqs y){return x.w<y.w;}
  • int main(){
  • int a,b;
  • cin>>a>>b;
  • for(int i=1;i<3;i++)fa[i]=i;
  • edge[1]={1,2,a},edge[2]={2,3,b};
  • sort(edge+1,edge+3,cmp);
  • int ans=0;
  • for(int i=1;i<3;i++){
  • int x=find(edge[i].u),y=find(edge[i].v);
  • if(x==y)continue;
  • fa[x]=y,ans+=edge[i].w;
  • }
  • cout<<ans;
  • return 0;
  • }
  • #include<bits/stdc++.h>
  • using namespace std;
  • int head[3],cnt,dst[3];
  • struct zqs{int to,w,nxt;} edge[3];
  • void add(int u,int v,int w){edge[++cnt]={v,w,head[u]};head[u]=cnt;}
  • struct H{
  • int id,dis;
  • bool operator<(const H &o)const{return dis>o.dis;}
  • };
  • bool vis[3];
  • void prim(){
  • fill(dst+1,dst+4,2e9),dst[1]=0;
  • memset(vis,false,sizeof vis);
  • priority_queue<H> q;
  • q.push({1,0});
  • while(!q.empty()){
  • H t=q.top(); q.pop();
  • if(vis[t.id])continue;
  • vis[t.id]=true;
  • for(int i=head[t.id];i;i=edge[i].nxt){
  • int v=edge[i].to;
  • if(dst[v]>edge[i].w){
  • dst[v]=edge[i].w;
  • if(!vis[v])q.push({v,dst[v]});
  • }
  • }
  • }
  • }
  • int main(){
  • int a,b,ans=0;
  • cin>>a>>b;
  • add(1,2,a),add(2,3,b),prim();
  • for(int i=1;i<4;i++)ans+=dst[i];
  • cout<<ans;
  • return 0;
  • }
  • #include<bits/stdc++.h>
  • using namespace std;
  • int a[3];
  • struct zqs{int l,r,val;} tree[3];
  • void build(int k,int l,int r){
  • if(l==r){tree[k]={l,r,a[l]};return;}
  • int mid=(l+r)>>1;
  • build(k*2,l,mid),build(k*2+1,mid+1,r);
  • tree[k]={l,r,tree[k*2].val+tree[k*2+1].val};
  • }
  • int query(int k,int l,int r){
  • if(tree[k].l>=l&&tree[k].r<=r)return tree[k].val;
  • int mid=(l+r)>>1,ans=0;
  • if(l<=mid)ans+=query(k*2,l,mid);
  • if(r>mid)ans+=query(k*2+1,mid+1,r);
  • return ans;
  • }
  • int main(){
  • cin>>a[1]>>a[2];
  • build(1,1,2);
  • cout<<query(1,1,2);
  • return 0;
  • ```

    8.树状数组

     
  • #include<bits/stdc++.h>
  • using namespace std;
  • int a[3];
  • int lowbit(int x){return x&-x;}
  • void add(int id,int num){
  • while(id<3)a[id]+=num,id+=lowbit(id);
  • }
  • int sum(int id){
  • int ans=0;
  • while(id>0)ans+=a[id],id-=lowbit(id);
  • return ans;
  • }
  • int main(){
  • int a,b;
  • cin>>a>>b;
  • add(1,a),add(2,b);
  • cout<<sum(2);
  • return 0;
  • }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值