首页 > 系统服务 > 详细

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

时间:2014-10-11 23:07:08      阅读:597      评论:0      收藏:0      [点我收藏+]

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;
}


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

原文:http://blog.csdn.net/chang_mu/article/details/40001303

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!