poj1325(最小顶点覆盖)

http://poj.org/problem?id=1325

Machine Schedule
Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 9666Accepted: 4108

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

题意:A机器有n(0~n-1)个模式,B机器有m(0~m-1)个模式
先有k个任务需要做,可以用A机器的ai模式做或者用B机器的bi模式做
任务无先后,换模式序重启,开始两个机器都在模式0

问最少需要重启几次

二分图最小点覆盖

A的模式为X集 B的模式为Y集

按任务建边

求最小点覆盖

任务是求最少的重启次数,也就是最少要工作在几个模式(除了0模式)
只要边的任何一个端点被选中就行了

所以求最小点覆盖 这些点抓住了所有的边,集完成了所有任务,模式就是这个店所代表的模式

最小点覆盖=二分图最大匹配
 
本来这个题给人的情形是MachineA——Jobs——MachineB,根本想不到可以转换成MachineA——MachineB即转化为求最小覆盖点数,而最小覆盖点数==最大匹配数,即求最大匹配数即可!具体的说A——B,把边作为jobs,而任意一边都可以用A或者B的某一种模式完成,所以每一条边,都可以连着A_i,B_j(i,j表示模式i,j),则试着选取最少的A_i和B_j即选择最少的模式,那么它就是求最小覆盖点数(求覆盖所有边的最少点数)需要注意的是0模式是初始模式,不需要重启,所以能用0模式工作的不要去练即0不连线
 
表示这题建图还不理解。。一直wa的地方就是这里,其他的没什么就是最大匹配
At the beginning they are both work at mode_0.
需要注意的是由于机器开始都处于 0 模式,所以如果有任务可以有任一种机器的 0 模式加工的话可先直接加工该任务而不要重启机器,即不要连边。
 
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int maxn=1010;
int uN,vN,k;
int xM[maxn],yM[maxn];
bool chk[maxn];
int g[1100][1100];
//int n,m,k;
bool SearchPath(int u)
{
    int v;
    for(v=0;v<vN;v++)
    {
        if(g[u][v]&&!chk[v])
        {
            chk[v]=true;
            if(yM[v]==-1||SearchPath(yM[v]))
            {
                yM[v]=u;
                xM[u]=v;
                return true;
            }
        }
    }
    return false;
}
int MaxMatch()
{
    int u,ret=0;
    memset(xM,-1,sizeof(xM));
    memset(yM,-1,sizeof(yM));
    for(u=0;u<uN;u++)
    {
        if(xM[u]==-1)
        {
            memset(chk,false,sizeof(chk));
            if(SearchPath(u))
            {
                ret++;
            }
        }
    }
    return ret;
}
int main()
{
    //int n,k;
    while(scanf("%d",&uN)!=EOF)
    {
        if(uN==0)
        {
            break;
        }
        //scanf("%d%d",&m,&k);
        //memset(chk,false,sizeof(chk));
        scanf("%d%d",&vN,&k);
  //uN=n;
  //vN=m;
        int x,y;
        memset(g,0,sizeof(g));
        int i;
        for(int j=0;j<k;j++)
        {
            scanf("%d%d%d",&i,&x,&y);
            if(x&&y)//***关键
                g[x][y]=1;
                //g[i][x]=1;
                //g[i][y]=1;
        }
        printf("%d\n",(MaxMatch()));
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值