搜索与图论(三)

一、最小生成树

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;
}
相关推荐
yyy(十一月限定版)30 分钟前
寒假集训4——二分排序
算法
星火开发设计30 分钟前
类型别名 typedef:让复杂类型更简洁
开发语言·c++·学习·算法·函数·知识
醉颜凉41 分钟前
【LeetCode】打家劫舍III
c语言·算法·leetcode·树 深度优先搜索·动态规划 二叉树
达文汐43 分钟前
【困难】力扣算法题解析LeetCode332:重新安排行程
java·数据结构·经验分享·算法·leetcode·力扣
一匹电信狗44 分钟前
【LeetCode_21】合并两个有序链表
c语言·开发语言·数据结构·c++·算法·leetcode·stl
User_芊芊君子44 分钟前
【LeetCode经典题解】搞定二叉树最近公共祖先:递归法+栈存路径法,附代码实现
算法·leetcode·职场和发展
算法_小学生1 小时前
LeetCode 热题 100(分享最简单易懂的Python代码!)
python·算法·leetcode
执着2591 小时前
力扣hot100 - 234、回文链表
算法·leetcode·链表
Gorgous—l1 小时前
数据结构算法学习:LeetCode热题100-多维动态规划篇(不同路径、最小路径和、最长回文子串、最长公共子序列、编辑距离)
数据结构·学习·算法
熬夜造bug1 小时前
LeetCode Hot100 刷题路线(Python版)
算法·leetcode·职场和发展