图论进程表

电脑重装,少了很多代码

P3371 【模板】单源最短路径(弱化版)

#include<bits/stdc++.h>
using namespace std;
const long long inf = 2147483647;
const int maxn=10005;
const int maxm=500005;
using namespace std;
int n,m,s,sum_edge=0;
int dis[maxn],vis[maxn],head[maxn];
struct Edge
{
    int next,to,dis;
}edge[maxm];
void addedge(int from,int to, int diss)
{
    edge[++sum_edge].next=head[from];
    edge[sum_edge].to=to;
    edge[sum_edge].dis=diss;
    head[from]=sum_edge;
}
void spfa()
{
    queue <int>q;
    for(int i=1;i<=n;i++)
    {
        dis[i]=inf;
        vis[i]=0;
    }
    q.push(s);dis[s]=0;vis[s]=1;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        vis[u]=0;
        for(int i=head[u];i;i=edge[i].next)
        {
            int v=edge[i].to;
            if(dis[v]>dis[u]+edge[i].dis)
            {
                dis[v]=dis[u]+edge[i].dis;
                if(vis[v]==0)
                {
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
}
int main()
{
    cin>>n>>m>>s;
    for(int i=1;i<=m;i++)
    {
        int f,g,w;
        cin>>f>>g>>w;
        addedge(f,g,w);
    }
    spfa();
    for(int i=1;i<=n;i++)
    {
        //cout<<i<<"  "<<head[i]<<endl;
        if(s==i)  cout<<0<<" ";
        else cout<<dis[i]<<" ";
     }
}

P4779 【模板】单源最短路径(标准版)

#include<iostream>
using namespace std;
struct edge 
{ 
    int next;
    int to;
    int wei;
}edge[MAXM];
int head[MAXN];//head[i]为i点的第一条边
int cnt=0;
void addedge(int u,int v,int w) //起点,终点,权值 
{
    edge[++cnt].next=head[u];//更新cnt
    edge[cnt].to=v;
    edge[cnt].w=w;
    head[u]=cnt;
}
int main()
{
    int n;
    for(int i=1;i<=n;i++)
    {
        int a,b,wei;
        addedge(a,b,wei);
        //如果是无向图,还要addedge(b,a,wei);
    }
}

通知

#include<bits/stdc++.h>
using namespace std;
int du[101];
#define R register
int n,a,b;
int main()
{
    freopen("notice.in","r",stdin);
    freopen("notice.out","w",stdout);
    scanf("%d",&n);
    for( R int i=1;i<=n;i++)
    {
        while(1)
        {
            scanf("%d",&a);
            if(a==0) break;
            du[a]++;
        }
    }
    int ans=0;
    for(R int i=1;i<=n;i++) if(du[i]==0) ans++;
        if(ans==0) printf("1\n");
    else
    printf("%d\n",ans);
    return  0;
}

搭配购买

#include<bits/stdc++.h>
using namespace std;
void fin(){freopen("buy.in","r",stdin);freopen("buy.out","w",stdout);}
void fc(){fclose (stdin);fclose (stdout);}
int w0[10010],v0[10010];
int f[10010];int n,m,w;int tot=0;
int w1[10010],v1[10000];
int w2[10010],v2[10010];
long long fafafa[10000];
int fa( int x )
{
    int p=x,k;
    while( x != f[x])x=f[x];
    while( p != f[p])k=p,p=f[p],f[k]=x;
    return x;
}
int main()
{
    fin();
    cin>>n>>m>>w;
    for(int i=1;i<=n;i++)
    {cin>>w0[i]>>v0[i];f[i]=i;}
    for(int i=1,a,b;i<=m;i++)cin>>a>>b,f[fa(b)]=f[fa(a)];
    for(int i=1;i<=n;i++)
    {
        w1[fa(i)]+=w0[i];
        v1[fa(i)]+=v0[i];
    }
    for(int i=1;i<=n;i++)
    {
        if(w1[i]!=0||v1[i]!=0)
        tot++,w2[tot]=w1[i],v2[tot]=v1[i];
    }
    for(int i=1;i<=tot;i++)
        for(int j=w;j>=w2[i];j--)
            fafafa[j]=max(fafafa[j],fafafa[j-w2[i]]+v2[i]);
    cout<<fafafa[w]<<endl;
    fc();
    return  0;
}

P3373 【模板】线段树 2

#include<bits/stdc++.h>
using namespace std;
int p;
long long a[100007];
struct node 
{
    long long v,mul,add;
}st[400007];
void bt(int root,int l,int r)
{
    st[root].mul=1;
    st[root].add=0;
    if(l==r)
    {
        st[root].v=a[l];
    }
    else 
        {
            int m=(l+r)>>1;
            bt(root*2,l,m);
            bt(root*2+1,m+1,r);
            st[root].v=st[root<<1].v+st[root<<1|1].v;
        }
    st[root].v%=p;
    return ;
}
void pushdown(int root,int l,int r)
{
    int m=(l+r)>>1;
    st[root<<1].v=(st[root<<1].v*st[root].mul+st[root].add*(m-l+1))%p;
    st[root<<1|1].v=(st[root<<1|1].v*st[root].mul+st[root].add*(r-m))%p;
    st[root<<1].mul=(st[root<<1].mul*st[root].mul)%p;
    st[root<<1|1].mul=(st[root<<1|1].mul*st[root].mul)%p;
    st[root<<1].add=(st[root<<1].add*st[root].mul+st[root].add)%p;
    st[root<<1|1].add=(st[root<<1|1].add*st[root].mul+st[root].add)%p;
    st[root].mul=1;
    st[root].add=0;
    return ;
}
void updatechengfa(int root,int stdl,int stdr,int l,int r,int k)
{
    if(r<stdl||stdr<l)  return;
    if(l<=stdl && stdr<=r)
    {
        st[root].v=(st[root].v*k)%p;
        st[root].mul=(st[root].mul*k)%p;
        st[root].add=(st[root].add*k)%p;
        return ;
    }
    pushdown(root, stdl, stdr);
    int m=(stdl+stdr)/2;
    updatechengfa(root*2, stdl, m, l, r, k);
    updatechengfa(root*2+1, m+1, stdr, l, r, k);
    st[root].v=(st[root*2].v+st[root*2+1].v)%p;
    return ;
}
void updatejiafa(int root,int stdl,int stdr,int l,int r,long long k)
{
    if(r<stdl||stdr<l)  return;
    if(l<=stdl && stdr<=r)
    {
        st[root].add=(st[root].add+k)%p;
        st[root].v=(st[root].v+k*(stdr-stdl+1))%p;
        return ;
    }
    pushdown(root, stdl, stdr);
    int m=(stdl+stdr)/2;
    updatejiafa(root*2, stdl, m, l, r, k);
    updatejiafa(root*2+1, m+1, stdr, l, r, k);
    st[root].v=(st[root*2].v+st[root*2+1].v)%p;
    return ;
}
long long query(int root, int stdl, int stdr, int l, int r){
    if(r<stdl || stdr<l){
        return 0;
    }
    if(l<=stdl && stdr<=r){
        return st[root].v;
    }
    pushdown(root, stdl, stdr);
    int m=(stdl+stdr)/2;
    return (query(root*2, stdl, m, l, r)+query(root*2+1, m+1, stdr, l, r))%p;
}
int main()
{
    int n,m;
    cin>>n>>m>>p;
    for(int i=1;i<=n;i++)
    scanf("%lld",&a[i]);
    int x,y;
    long long k;
    bt(1,1,n);
    int op;
    while(m--)
    {
        cin>>op;
        if(op==1)
        {
            scanf("%d%d%lld",&x,&y,&k);
            updatechengfa(1,1,n,x,y,k);
        }
        if(op==2)
        {
            scanf("%d%d%lld",&x,&y,&k);
            updatejiafa(1,1,n,x,y,k);
        }
        else if(op==3)
        {
            scanf("%d%d%",&x,&y);
            printf("%lld\n",query(1,1,n,x,y)%p);
        }
    }
    return 0;
}

P3372 【模板】线段树 1

//1363889591
#include<bits/stdc++.h>
using namespace std;
inline int get()
{
    char c;
    int sign=1;
    while((c=getchar())<'0'||c>'9')
    if(c=='-') sign=-1;
    int res=c-'0';
    while ((c=getchar())>='0'&&c<='9')
    {
        /* code */
        res=res*10+c-'0';
    }
    return res*sign;
}
const int N=1e5+5;
int n,m,a[N];
int add[4*N];
long long sum[N*4];
void build(int k, int l, int r)
{
    if(l==r)
    {
        sum[k]=a[l];
        return ;
    }
    int mid=(l+r)>>1;
    build(k<<1,l,mid);
    build(k<<1|1,mid+1,r);
    sum[k]=sum[k<<1]+sum[k<<1|1];
}
void AD (int k,int l,int r ,int v)
{
    add[k]+=v;
    sum[k]+=(long long)v*(r-l+1);
}
void pushdown(int k,int l,int r,int mid)
{
    if(add[k]==0) return ;
    AD  (k<<1,l,mid,add[k]);
    AD  (k<<1|1,mid+1,r,add[k]);
    add[k]=0;
}
long long query (int k,int l, int r ,int x,int y)
{
    if (l>=x&&r<=y)  return sum[k];
    int mid=(l+r)>>1;
    long long res=0;
    pushdown(k,l,r,mid);
    if(x<=mid) res+=query(k<<1,l,mid,x,y);
    if(y>mid) res+=query(k<<1|1,mid+1,r,x,y);
    return res;
}
void modifty(int k,int l,int r,int x,int y,int v)
{
    if(l>=x&&r<=y)  return AD(k,l,r,v);
    int mid=(l+r)>>1;
    pushdown(k,l,r,mid);
    if(x<=mid) modifty(k<<1,l,mid,x,y,v);
    if(mid<y) modifty (k<<1|1,mid+1,r,x,y,v);
    sum[k]=sum[k<<1]+sum[k<<1|1];
}
int main()
{
    int op;
    int A,B,C;
    n=get(),m=get();
    for(int i=1;i<=n;i++)  a[i]=get();
    build(1,1,n);
    while (m--)
    {
        /* code */
    
    op=get();
    if(op==1)
        {
            A=get();
            B=get();
            C=get();
            modifty(1,1,n,A,B,C);
        }
    else 
        {
            A=get();
            B=get();
            printf("%lld\n",query(1,1,n,A,B));
        }
    }
    return 0;
}

转载于:https://www.cnblogs.com/luotianyi20120712/p/11213755.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值