HDU 1150 Machine Schedule(二分匹配最小点覆盖)

题意:有两台机器A和B,A有n种工作模式(0~n-1),B有m种工作模式(0~m-1),两台机器的初始状态都是在工作模式0处。现在有k(0~k-1)个工作,(i,x,y)表示编号为i的工作可以通过机器A的工作模式x完成,也可以通过机器B的工作模式y完成。机器必须重启后才能更换一种工作模式,问最少的重启次数。

分析:

1、重启次数最少,即工作模式种类最少,即用最少的工作模式完成所有工作。

2、将A的n种工作模式看做n个点,将B的m种工作模式看做m个点,即用最少的点(工作模式)覆盖所有的边(一条边代表一种工作)。

3、最小点覆盖数 = 最大匹配数。

最小顶点覆盖:用最少的点,让每条边都至少和其中一个点关联;---实质上是能覆盖所有的边的最小点集。

4、注意,因为两台机器的初始状态都是在工作模式0处,因此若一条边的其中一个端点为0,则可以将其看做是在两台机器的初始工作状态时完成的,即不参与重启次数的统计。

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
const double eps = 1e-8;
inline int dcmp(double a, double b){
    if(fabs(a - b) < eps) return 0;
    return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 1000 + 10;
const int MAXT = 1000000 + 10;
using namespace std;
int n, m, k;
int vis[MAXN];
bool used[MAXN];
int pic[110][110];
void init(){
    memset(pic, 0, sizeof pic);
    memset(vis, 0, sizeof vis);
}
bool dfs(int x){
    for(int i = 1; i < m; ++i){
        if(pic[x][i] && !used[i]){
            used[i] = true;
            if(!vis[i] || dfs(vis[i])){
                vis[i] = x;
                return true;
            }
        }
    }
    return false;
}
int hungary(){
    int cnt = 0;
    for(int i = 1; i < n; ++i){
        memset(used, false, sizeof used);
        if(dfs(i)) ++cnt;
    }
    return cnt;
}
int main(){
    while(scanf("%d", &n) == 1){
        if(!n) return 0;
        init();
        scanf("%d%d", &m, &k);
        for(int i = 0; i < k; ++i){
            int a, b, c;
            scanf("%d%d%d", &a, &b, &c);
            if(b > 0 && c > 0) pic[b][c] = 1;
        }
        printf("%d\n", hungary());
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/6502932.html