CSP-S 2019 summary graph theory

CSP-S 2019 summary graph theory

First, the shortest path problem

template


Floyd algorithm

void floyd()
{
    memset(map,0x3f,sizeof(map));
    for(int i=1;i<=n;i++)
        map[i][i]=0;
    for(int k=1;k<=n;k++)
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                map[i][j]=min(map[i][j],map[i][k]+map[k][j]);
}

Dijkstra's algorithm

const int INF=1e9;
void dijkstra(int s)
{
    int temp,k,y;
    memset(dist,0x3f,sizeof(dist));
    memset(v,0,sizeof(v));
    dist[s]=0;
    for(int i=1;i<=n;i++)
    {
        temp=INF;
        for(int j=1;j<=n;j++)
            if(dist[j]<temp && !v[j])
                k=j,temp=dist[j];
        v[k]=1;
        for(int j=head[i];j;j=nxt[j])
        {
            y=to[i];
            if(dist[y]>dist[k]+val[j])
                dist[y]=dist[k]+val[j];
        }
    }
}

SPFA algorithm

void spfa(int s)
{
    memset(dist,0x3f,sizeof(dist));
    memset(v,0,sizeof(v));
    queue<int> q;
    q.push(s);v[s]=1;dist[s]=0;
    while(!q.empty())
    {
        int x=q.front();
        v[x]=0;q.pop();
        for(int i=head[x];i;i=nxt[i])
        {
            int y=to[i];
            if(dist[y]>dist[x]+val[i])
            {
                dist[y]=dist[x]+val[i];
                if(!v[y])
                    v[y]=1,q.push(y);
            }
        }
    }
}

Heap optimization algorithm Dijkstra

void dijkstra()
{
    memset(dist,0x3f,sizeof(dist));
    dist[0]=0;
    q.push(make_pair(0,0));
    while(!q.empty())
    {
        int x=q.top().second;
        if(v[x])
        {
            q.pop();
            continue;
        }
        x=q.top().second;q.pop();v[x]=1;
        for(int i=head[x];i;i=nxt[i])
        {
            int y=to[i];
            if(dist[y]>dist[x]+val[i])
                dist[y]=dist[x]+val[i],q.push(make_pair(-dist[y],y));
        }
    }
}

SPFA optimization

See template:

On optimizing the way SPFA


Common Problems and Solutions

1, selection and application of the shortest path algorithm (analysis of complexity, making cooked template, make it clear that these algorithms can not solve any problem)

2, according to the meaning of the questions to build flexible chart (example: VIJOS the best route-P 1423 )

3, reverse and reverse map building construction drawing application (example: [NOIP2009] Optimal Trade T3 )

4, K short circuit problem

5, the shortest and DP binding (example: [ZJOI2006] transport stream )

6, the shortest path optimization (example: [USACO10DEC] Apple Delivery Delivery the Apple )

7, multiple shortest path problem (example: [USACO14OPEN] the GPS of the GPS's duel Dueling )

8, the processing of the dual edge

9, the recording shortest path


Second, the spanning tree problem

template


Kruskal's algorithm

void kruskal()
{
    sort(e+1,e+m+1,cmp);
    for(int i=1;i<=m;i++)
    {
        int fx=find(e[i].x);
        int fy=find(e[i].y);
        if(fx!=fy)
        {
            fa[fx]=fy;
            cnt++;
        }
        if(cnt==n-1)
            return;
    }
}

Prim's algorithm

void prim()
{
    memset(f,0x3f,sizeof(f));
    memset(v,0,sizeof(v));
    f[1]=0;v[1]=1;
    for(int i=1;i<=n;i++)
    {
        int temp=1<<30,k;
        for(int j=1;j<=n;j++)
            if(!v[j]&&f[j]<temp)
                temp=f[j],k=j;
        v[k]=1;
        ans+=f[k];
        for(int j=head[k];j;j=nxt[j])
        {
            int y=to[j];
            if(val[j] && !v[j] && f[j]>val[j])
                f[j]=val[j];
        }
    }
}

Common Problems and Solutions

1, Multiple Spanning Tree (example: JDOJ 1044 Span , [HNOI2006] road construction issues )

2, the split tree (example: VIJOS pocket-P1234, sky )

3, edged into a tree problem (example: [USACO07DEC] road construction Roads Building )


Third, the disjoint-set

Common Problems and Solutions

1, and processing of the check ring is set

2, disjoint-set maintenance oppositional (example: [BOI2003] gangs )

3, with the right to disjoint-set (example: [NOI2002] Legend of Galactic Heroes )

4, disjoint-set type (example: [NOI2001] food chain )


Fourth, topological sorting

template:


void topsort()
{
    while(!q.empty())
    {
        int x=q.front();
        q.pop();
        for(int i=head[x];i;i=nxt[i])
        {
            int y=to[i];
            if(y==fa[x])
                continue;
            out_degree[y]--;
            if(!out_degree[y])
                q.push(y);
        }
    }
}

Guess you like

Origin www.cnblogs.com/fusiwei/p/11762681.html