zoukankan      html  css  js  c++  java
  • POJ1325 Machine Schedule 二分图匹配求最小顶点覆盖

    以前在杭电上做过,不过代码提交到POJ就WA了,原因还是程序存在一定的bug:
    http://www.cnblogs.com/Lyush/archive/2012/03/28/2422060.html

    在处理开始状态为0状态,匹配结果不进行统计的过程中,正确的处理方式应该是忽略掉所有能够工作的0状态的所有工作,因为这样的工作肯定是先让他们在0状态工作完。然后将其他的工作进行连边,然后求一个最大顶点覆盖(所有的边都至少有一个顶点落在所求顶点集合内),也即所有的工作至少能够有一台机器进行执行。

    代码如下:

    #include <cstdlib>
    #include <cstring>
    #include <cstdio> 
    using namespace std;
    int N, M, K, visit[105], marry[105];
    int G[105][105];
    
    int path(int u)
    {
        for (int i = 0; i < M; ++i) {
            if (!G[u][i] || visit[i])
                continue;
            visit[i] = 1;
            if (marry[i] == -1 || path(marry[i])) {
                marry[i] = u;
                return 1;
            }
        }
        return 0;
    }
    /*
    3 3 2
    0 1 1
    1 0 1
    
    1
    */
    
    int main()
    {
        int a, x, y, ans;
        while (scanf("%d", &N), N) {
            scanf("%d %d", &M, &K);
            ans = 0;
            memset(G, 0, sizeof (G));
            memset(marry, 0xff, sizeof (marry));
            for (int i = 0; i < K; ++i) {
                scanf("%d %d %d", &a, &x, &y);
                if (x && y) 
                    G[x][y] = 1;
            }
            for (int i = 0; i < N; ++i) {
                memset(visit, 0, sizeof (visit));
                if (path(i)) {
                    ++ans;
                }
            }
            printf("%d\n", ans);
        }
        return 0;
    }
  • 相关阅读:
    自测项目:批量删除云盘重复文件
    表格更新成本 二 问用户年龄和口令,直到他们提供有效的输入
    知乎抓取、写入文档
    倒打印心
    HDU 1102 Constructing Roads
    C++模板:字典树
    HDU 3015 Disharmony Trees
    HDU 2227 Find the nondecreasing subsequences
    HDU 3486 Interviewe
    C++模板:ST算法
  • 原文地址:https://www.cnblogs.com/Lyush/p/3003369.html
Copyright © 2011-2022 走看看