POJ 1325 Machine Schedule(二分匹配 最小点覆盖)

题目链接:http://poj.org/problem?id=1325

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

Beijing 2002

题目大意;有两台机器A和B以及N个需要运行的任务。每台机器有M种不同的模式,而每个任务都恰好在一台机器上运行。如果它在机器A上运行,则机器A需要设置为模式xi,如果它在机器B上运行,则机器A需要设置为模式yi。每台机器上的任务可以按照任意顺序执行,但是每台机器每转换一次模式需要重启一次。请合理为每个任务安排一台机器并合理安排顺序,使得机器重启次数尽量少。
二分图的最小顶点覆盖数=最大匹配数

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int MAXN = 117;
int LN, RN;
int g[MAXN][MAXN], linker[MAXN];
bool used[MAXN];

int dfs(int L)
{
    for(int R = 0; R < RN; R++)
    {
        if(g[L][R] && !used[R])
        {
            used[R] = true;
            if(linker[R] == -1 || dfs(linker[R]))
            {
                linker[R] = L;
                return 1;
            }
        }
    }
    return 0;
}

int hungary()
{
    int res = 0;
    memset(linker,-1,sizeof(linker));
    for(int L = 0; L < LN; L++)
    {
        memset(used,0,sizeof(used));
        if(dfs(L))
            res++;
    }
    return res;
}

int main()
{
    int i, L, R;
    int res;
    int k;
    while(~scanf("%d",&LN) && LN)
    {
        scanf("%d%d",&RN,&k);
        memset(g,0,sizeof(g));
        for(int j = 0; j < k; j++)
        {
            scanf("%d%d%d",&i,&L,&R);
            if(L > 0 && R > 0)
                g[L][R] = 1;
        }
        res = hungary();
        printf("%d\n",res);
    }
    return 0;
}
时间: 2024-08-27 00:08:57

POJ 1325 Machine Schedule(二分匹配 最小点覆盖)的相关文章

hdu - 1150 Machine Schedule (二分图匹配最小点覆盖)

http://acm.hdu.edu.cn/showproblem.php?pid=1150 有两种机器,A机器有n种模式,B机器有m种模式,现在有k个任务需要执行,没切换一个任务机器就需要重启一次, 如果任务i在机器A上执行,A机器需要一个对应的模式A,如果在机器B上执行,机器A需要一个模式B. 一直就是机器A在切换模式,现在让你安排一种合理的任务执行顺序,让机器重启次数最少. 每个任务之间连一条边.二分图的最小顶点覆盖就是求最少的点可以连接所有的边,然后转化成最大匹配即可. 这题就是初始状态

POJ-1325 Machine Schedule 二分图匹配 最小点覆盖问题

POJ-1325 题意: 有两台机器A,B,分别有n,m种模式,初始都在0模式,现在有k项任务,每项任务要求A或者B调到对应的模式才能完成.问最少要给机器A,B调多少次模式可以完成任务. 思路: 相当于是在以n.m个点构成的二分图中,求二分图的最小顶点覆盖数(就是每个任务都涉及到,所需的顶点数).根据Konig定理,二分图的最小顶点覆盖数就是求最大匹配数,注意这里是Base 0的,就是初始不用调整模式就可以完成0模式的任务,所以读入的时候不用考虑与0相连的边. #include <algorit

poj 1325 Machine Schedule 二分图匹配+DFS实现

#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <stack> #include <set> #include <map> #include <string> #include <ma

POJ 1325 Machine Schedule【最小点覆盖】

E - Machine Schedule Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1325 Appoint description:  System Crawler  (2014-08-10) Description As we all know, machine scheduling is a very classical pr

poj 1325 Machine Schedule 解题报告

题目链接:http://poj.org/problem?id=1325 题目意思:有 k 个作业,机器A有 n 个模式:0 ~ n-1,机器B 有 m 个模式:0~ m-1.每一个作业能运行在 A 的 某一个模式(假设为 i (0 <= i <= n-1 ) )或 B 的某一个模式下(j (0 <= j <= m-1)).多个作业可以同时运行在 A 的某一个 模式下,当然 B 也如此.每对A 或 B 转换一次模式,就要重启一次 A 或者 B,你需要选择A 或 B 的一些模式,使得所

POJ 1325 Machine Schedule 二分图最大匹配

把每一个任务看做一个边,机器的模式看做是一个点,这个其实就是求一个最少点覆盖所有边即最小点覆盖集的问题,因为最小点覆盖集=二分图的最大匹配,所以问题转化成了求二分图最大匹配问题. 第一次写二分图匹配,感觉建模还是相当困难的. #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <strin

poj 1325 Machine Schedule 最小点覆盖

题目链接:http://poj.org/problem?id=1325 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 satis

POJ 1325 Machine Schedule (最小点覆盖 &amp;&amp; 二分图最大匹配)

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">鏈接: http://poj.org/problem?id=1325</span> Description As we all know, machine scheduling is a very classical problem in computer science a

POJ - 1325 Machine Schedule 二分图 最小点覆盖

题目大意:有两个机器,A机器有n种工作模式,B机器有m种工作模式,刚开始两个机器都是0模式,如果要切换模式的话,机器就必须的重启 有k个任务,每个任务都可以交给A机器的i模式或者B机器的j模式完成,问要重启多少次机器才能完成任务 解题思路:两个机器的点分为两个点集,点集之间的关系就是任务了,要将所有任务都完成,就要将所有边都覆盖掉,所以就是求最小点覆盖了. 这里有一个点要注意,如果所有任务中都有一个0,那么机器就不用重启了,重启次数就为0了(因为刚开始都是0) #include<cstdio>

POJ 1325 Machine Schedule (二分图最小点集覆盖 匈牙利算法)

Machine Schedule Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12621   Accepted: 5399 Description As we all know, machine scheduling is a very classical problem in computer science and has been studied for a very long history. Scheduli