码迷,mamicode.com
首页 > 系统相关 > 详细

Poj_1325 Machine Schedule -最大匹配性质题目

时间:2016-07-31 12:49:39      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

题目:用最少的起点,带上所有的边

分析:对于最大匹配,其中一半的点可以连上所有的边。可以反面去证明,若存在一条边最大匹配中的匹配点都不与它相连,那么加入这条边,并不会破坏匹配的性质并且使最大匹配大一,与假设矛盾,所以证明成立。

/************************************************
Author        :DarkTong
Created Time  :2016/7/31 11:28:07
File Name     :Poj_1325.cpp
*************************************************/

//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 300 + 10;
int w[maxn][maxn], n, m;
int Left[maxn];
bool used[maxn];
bool match(int i)
{
    for(int j=1;j<=n;++j) if(w[j][i]&&!used[j])
    {
        used[j] = true;
        if(!Left[j]||match(Left[j]))
        {
            Left[j] = i;
            return true;
        }
    }
    return false;
}
//返回最大匹配数
int hungary()
{
    int res=0;
    memset(Left, 0, sizeof(Left));
    for(int i=1;i<=m;++i)
    {
        memset(used, 0, sizeof(used));
        if(match(i)) res++;
    }
    return res;
}

int main()
{
    int T, cas=1;
    int k;
    while(scanf("%d", &n)==1&&n)
    {
        scanf("%d%d", &m, &k);
        memset(w, 0, sizeof(w));

        int a, b, c;
        for(int i=0;i<k;++i)
        {
            scanf("%d%d%d", &a, &b, &c);
            w[b][c]=1;
        }
        printf("%d\n", hungary());
    }
    
    return 0;
}

Poj_1325 Machine Schedule -最大匹配性质题目

标签:

原文地址:http://www.cnblogs.com/DarkTong/p/5722692.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!