zoukankan      html  css  js  c++  java
  • poj1325

    最小点集覆盖

    匈牙利算法

    View Code
    #include <iostream>
    #include
    <cstdio>
    #include
    <cstdlib>
    #include
    <cstring>
    using namespace std;

    #define maxn 105

    int n, uN, vN;
    bool g[maxn][maxn];
    int xM[maxn], yM[maxn];
    bool chk[maxn];

    bool SearchPath(int u)
    {
    int v;
    for (v = 0; v < vN; v++)
    if (g[u][v] && !chk[v])
    {
    chk[v]
    = true;
    if (yM[v] == -1 || SearchPath(yM[v]))
    {
    yM[v]
    = u;
    xM[u]
    = v;
    return true;
    }
    }
    return false;
    }

    int MaxMatch()
    {
    int u, ret = 0;
    memset(xM,
    -1, sizeof(xM));
    memset(yM,
    -1, sizeof(yM));
    for (u = 0; u < uN; u++)
    if (xM[u] == -1)
    {
    memset(chk,
    false, sizeof(chk));
    if (SearchPath(u))
    ret
    ++;
    }
    return ret;
    }

    void input()
    {
    scanf(
    "%d%d", &vN, &n);
    memset(g,
    0, sizeof(g));
    for (int i = 0; i < n; i++)
    {
    int a, b, c;
    scanf(
    "%d%d%d", &a, &b, &c);
    if (b && c)
    g[b][c]
    = true;
    }
    }

    int main()
    {
    //freopen("t.txt", "r", stdin);
    while (scanf("%d", &uN), uN)
    {
    input();
    int ans = MaxMatch();
    printf(
    "%d\n", ans);
    }
    return 0;
    }

  • 相关阅读:
    构造函数、原型、实例化对象
    JS闭包的理解
    JQuery知识点
    面向对象
    学习使用Vuex
    Runtime详解
    ffmpeg各结构体之间关联 ---- AVPacket
    AVFrame
    block的底层原理
    performSelector
  • 原文地址:https://www.cnblogs.com/rainydays/p/2087516.html
Copyright © 2011-2022 走看看