搜索与图论(三)

一、最小生成树

1.1Prim算法

朴素版Prim

一般用于稠密图

算法流程:

集合表示当前已经在连通块的点

1.初始化距离,把所有距离都初始化为正无穷

2.n次迭代,找到集合外距离最小的点 ->t

3.用t来更新其它点到集合的距离

cpp 复制代码
#include<iostream>
#include<cstring>
#include<algorithm>

using namespace std;

const int N = 510,INF = 0x3f3f3f3f;

int n,m;
int g[N][N];
int dist[N];
bool st[N];

int prim()
{
    memset(dsit,0x3f,sizeof dsit);
    
    int res = 0;
    for(int i = 0;i < n;i ++)
    {
        int t = -1;
        for(int j = 1;j <= n;j ++)
        {
            if(! st[j] && (t == -1 || dist[t] > dist[j]))
                t = j;
        }
        
        if(i && dist[t] == INF) return INF;
        
        for(int j = 1;j <=n;j ++) dist[j] = min(dist[j],g[t][j]);
        st[t] = true;
    }
    return res;
}
int main()
{
    scanf("%d%d",&n,&m);
    
    memset(g,0x3f,sizeof g);
    
    while(m --)
    {
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        g[a][b] = g[b][a] = min(g[a][b],c);
    }
    
    int t = prim();
    
    if(t == INF) puts("impossible");
    else printf("%d\n",t);
    
    return 0;
}

1.2Kruskal算法

一般用于稀疏图

算法流程:

1.将所有边按照权重从小到大排序

2.枚举每一条边(a,b),权重为c

如果(a,b)不连通,则将这条边加入集合中

cpp 复制代码
#include<iostream>
#include<algorithm>

using namespace std;

const int N = 100010;

int n,m;
//并查集的集合
int p[N];

struct Edge
{
    int a,b,w;

    bool operator < (const Edge &W)const
    {
        return w < W.w;
    }
}edges[N];

int find(int x)
{
    if(p[x] != x) p[x] = find(p[x]);
    return p[x];
}
int main()
{
    scanf("%d%d",&n,&m);

    for(int i = 0;i < m;i ++)
    {
        int a,b,w;
        scanf("%d%d%d",&a,&b,&w);
        edges[i] = {a,b,w};
    }

    sort(edges,edges + m);

    for(int i = 1;i <= n;i ++) p[i] = i;

    int res = 0,cnt = 0;

    for(int i = 0; i < m; i ++)
    {
        //从小到大枚举所有边
        int a = edges[i].a,b = edges[i].b,w = edges[i].w;

        //知道a与b的祖宗节点
        a = find(a),b = find(b);

        //判断a与b是否连通
        if(a != b)
        {
            //集合合并
            p[a] = b;
            res += w;
            cnt ++;
        }
    }

    if (cnt < n - 1) puts("impossible");
    else printf("%d\n",res);

    return 0;
}

二、二分图

二分图当且仅当图中不含奇数环

2.1染色法

cpp 复制代码
#include<iostream>
#include<cstring>
#include<algorithm>

using namespace std;

const int N = 100010,M = 200010;

int n,m;
int h[N],e[M],ne[M],idx;
int color[N];

void add(int a,int b)
{
    e[idx] = b,ne[idx] = h[a],h[a] = idx ++;
}

bool dfs(int u,int c)
{
    //当前点的颜色是c
    color[u] = c;

    for(int i = h[u];i != -1;i = ne[i])
    {
        int j = e[i];
        if(!color[j])
        {
            if(!dfs(j,3 - c)) return false;
        }
        else if (color[j] == c) return false;
    }
    return true;
}

int main()
{
    scanf("%d%d",&n,&m);

    memset(h,-1,sizeof h);

    while(m --)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        add(a,b),add(b,a);
    }

    bool flag = true;
    for(int i = 1;i <=n;i ++)
    {
        if(!color[i])
        {
            if(!dfs(i,1))
            {
                flag = false;
                break;
            }
        }
    }

    if(flag) puts("Yes");
    else puts("No");

    return 0;
}

2.2匈牙利算法

cpp 复制代码
#include<iostream>
#include<algorithm>
#include<cstring>

using namespace std;

const int N = 510,M = 100010;

int n1,n2,m;
int h[N],e[M],ne[M],idx;
int match[N];
bool st[N];

void add(int a,int b)
{
    e[idx] = b,ne[idx] = h[a],h[a] = idx ++;
}
bool find(int x)
{
    for(int i = h[x];i != -1;i = ne[i])
    {
        int j = e[i];
        if(!st[j])
        {
            st[j] = true;
            if(match[j] == 0 || find(match[j]))
            {
                match[j] = x;
                return true;
            }
        }
    }
    return false;
}
int main()
{
    scanf("%d%d%d",&n1,&n2,&m);

    memset(h,-1,sizeof h);

    while(m --)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        add(a,b);
    }

    int res = 0;
    for(int i = 0;i <= n1;i ++)
    {
        memset(st,false,sizeof st);

        if(find(i)) res ++;
    }
    printf("%d\n",res);

    return 0;
}
相关推荐
查理零世16 分钟前
【蓝桥杯集训·每日一题2025】 AcWing 6134. 哞叫时间II python
python·算法·蓝桥杯
仟濹17 分钟前
【二分搜索 C/C++】洛谷 P1873 EKO / 砍树
c语言·c++·算法
紫雾凌寒25 分钟前
解锁机器学习核心算法|神经网络:AI 领域的 “超级引擎”
人工智能·python·神经网络·算法·机器学习·卷积神经网络
京东零售技术1 小时前
AI Agent实战:打造京东广告主的超级助手 | 京东零售技术实践
算法
MiyamiKK572 小时前
leetcode_位运算 190.颠倒二进制位
python·算法·leetcode
C137的本贾尼2 小时前
解决 LeetCode 串联所有单词的子串问题
算法·leetcode·c#
青橘MATLAB学习2 小时前
时间序列预测实战:指数平滑法详解与MATLAB实现
人工智能·算法·机器学习·matlab
lingllllove2 小时前
matlab二维艾里光束,阵列艾里光束,可改变光束直径以及距离
开发语言·算法·matlab
88号技师2 小时前
2025年2月一区SCI-海市蜃楼搜索优化算法Mirage search optimization-附Matlab免费代码
开发语言·人工智能·算法·机器学习·matlab·优化算法
在雨中6123 小时前
【找工作】C++和算法复习(自用)
c++·算法