POJ1325 Machine Schedule 【二分图最小顶点覆盖】

Machine Schedule
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 11958   Accepted: 5094

Description

As we all know, machine scheduling is a very classical problem in computer science and has been studied for a very long history. Scheduling problems differ widely in the nature of the constraints that must be satisfied and the type of schedule desired. Here we consider a 2-machine scheduling problem. 

There are two machines A and B. Machine A has n kinds of working modes, which is called mode_0, mode_1, ..., mode_n-1, likewise machine B has m kinds of working modes, mode_0, mode_1, ... , mode_m-1. At the beginning they are both work at mode_0. 

For k jobs given, each of them can be processed in either one of the two machines in particular mode. For example, job 0 can either be processed in machine A at mode_3 or in machine B at mode_4, job 1 can either be processed in machine A at mode_2 or in machine B at mode_4, and so on. Thus, for job i, the constraint can be represent as a triple (i, x, y), which means it can be processed either in machine A at mode_x, or in machine B at mode_y. 

Obviously, to accomplish all the jobs, we need to change the machine's working mode from time to time, but unfortunately, the machine's working mode can only be changed by restarting it manually. By changing the sequence of the jobs and assigning each job to a suitable machine, please write a program to minimize the times of restarting machines. 

Input

The input file for this program consists of several configurations. The first line of one configuration contains three positive integers: n, m (n, m < 100) and k (k < 1000). The following k lines give the constrains of the k jobs, each line is a triple: i, x, y. 

The input will be terminated by a line containing a single zero. 

Output

The output should be one integer per line, which means the minimal times of restarting machine.

Sample Input

5 5 10
0 1 1
1 1 2
2 1 3
3 1 4
4 2 1
5 2 2
6 2 3
7 2 4
8 3 3
9 4 3
0

Sample Output

3

Source

题意:二分图最小覆盖:找到一个点集,使得每条边上至少有一个点在该集合中。最小匹配==最大覆盖。

题解:匈牙利,因为问的是机器重新启动次数,所以对于左右连接点有0号模式的任务不要读入图中。


#include <stdio.h>
#include <string.h>

const int inf = 0x3f3f3f3f;
const int maxn = 102;
int n, m, k;
bool map[maxn][maxn], visy[maxn];
int cx[maxn], cy[maxn];

void getMap() {
    memset(map, 0, sizeof(map));
    int u, v;
    while(k--) {
        scanf("%*d%d%d", &u, &v);
        if(u * v) map[u][v] = true;
    }
}

int findPath(int x) {
    int i, j;
    for(i = 0; i < m; ++i) {
        if(map[x][i] && !visy[i]) {
            visy[i] = 1;
            if(cy[i] == -1 || findPath(cy[i])) {
                cx[x] = i; cy[i] = x; return 1;
            }
        }
    }
    return 0;
}

int MaxMatch() {
    memset(cy, -1, sizeof(cy));
    memset(cx, -1, sizeof(cx));
    int i, j, ans = 0;
    for(i = 0; i < n; ++i) {
        if(cx[i] == -1) {
            memset(visy, 0, sizeof(visy));
            ans += findPath(i);
        }
    }
    return ans;
}

void solve() {
    printf("%d\n", MaxMatch());
}

int main() {
    // freopen("stdin.txt", "r", stdin);
    while(scanf("%d%d%d", &n, &m, &k) == 3) {
        getMap();
        solve();
    }
    return 0;
}


你可能感兴趣的:(schedule)