検索とグラフ理論 (3)

1. 最小スパニングツリー

1.1 プリムアルゴリズム

プレーン版プリム

通常、密なグラフに使用されます

アルゴリズムの流れ:

セットは、接続されたブロック内に現在あるポイントを表します。

1. 距離を初期化し、すべての距離を正の無限大に初期化します。

2. n 回の反復で、セットの外側の最小距離を持つ点を見つけます -> t

3. t を使用して、他の点からセットまでの距離を更新します。

#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.2 クラスカルアルゴリズム

通常、疎グラフに使用されます。

アルゴリズムの流れ:

1. すべてのエッジを重みの昇順に並べ替えます

2. 各エッジ (a、b) を重み c で列挙します。

(a,b) が接続されていない場合は、このエッジをセットに追加します

#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. 二部グラフ

グラフ内に奇数サイクルがない場合に限り、2 部グラフ

2.1 染色方法

#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 ハンガリーのアルゴリズム

#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;
}

おすすめ

転載: blog.csdn.net/weixin_64443786/article/details/132069222