POJ 1325 Machine Schedule(最小覆盖数)

题意:题意杀。题目大意就是有两台机器A,B,分别由m和n种模式,初始时都在模式0,现在有k个工作,每一个工作都可以将A设置成模式i或将B设置成模式j,但每一次更换模式时机器不得不要重启,求完成所有工作的最小重启次数输入数据的第一行有三个数据,分别代表工作数,A/B的模式数,当输入0时结束程序,接下来多行,每行的开始代表工作的序号,和完成该工作需将A/B设置的模式数,输出一个整数,代表机器最小重启次数.

思路:转自网上。

           首先由于A和B机器初始都是模式0,所以我们对于输入任务可以在A 0 模式或B 0 模式下完成的,我们直接不考虑这些任务. 因为这些任务不会对最终机器最小重启次数有任何影响.

           下面我们只考虑那些只能在A 模式 1 到n-1 和B模式 1到m-1 下完成的任务.

           把A的1到n-1个模式看成是左边的n-1个点,把B模式的1到m-1个任务看成右边的m-1个点. 对于每个任务(x,i,j) ,连一条左边第i个点与右边第j个点的无向边. 那么该图的每条边就表示一个任务了.我们想要使得机器的重启次数最少,就是要在左边和右边选出总数最少的节点(每个节点代表机器重启了一次),让这些节点覆盖所有的边(即任务). 那么就是求该无向图的最小覆盖 = 无向图的最大匹配数.

注意:就算不同编号的任务所构成的边重复,依然能正确计算.因为最大匹配计算的是匹配的点对数目.


#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int maxn=100+5;

struct Max_Match
{
    int n,m;
    vector<int> g[maxn];
    bool vis[maxn];
    int left[maxn];

    void init(int n,int m)
    {
        this->n=n;
        this->m=m;
        for(int i=1;i<=n;i++) g[i].clear();
        memset(left,-1,sizeof(left));
    }

    bool match(int u)
    {
        for(int i=0;i<g[u].size();i++)
        {
            int v=g[u][i];
            if(!vis[v])
            {
                vis[v]=true;
                if(left[v]==-1 || match(left[v]))
                {
                    left[v]=u;
                    return true;
                }
            }
        }
        return false;
    }

    int solve()
    {
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            memset(vis,0,sizeof(vis));
            if(match(i)) ans++;
        }
        return ans;
    }
}MM;

int main()
{
    int n,m,k;
    while(scanf("%d",&n)==1&&n)
    {
        scanf("%d%d",&m,&k);
        MM.init(n-1,m-1);
        while(k--)
        {
            int i,u,v;
            scanf("%d%d%d",&i,&u,&v);
            if(u==0 || v==0) continue;
            MM.g[u].push_back(v);
        }
        printf("%d\n",MM.solve());
    }
    return 0;
}


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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值