HDU - 1150 题解E

https://vjudge.net/contest/362594#problem/E

E - 同下

HDU - 1150

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.

InputThe 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. OutputThe 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

题目描述:

意思就是给出(i,x,y),任务i可以由机器1的模式x完成,或者由机器2的模式y完成。每台机器要换模式的时候,都要重新启动。问要完成所有的任务,至少要重启多少次。

分析:

其实意思是要完成所有任务,至少要用多少个模式。我们把所有模式当成点,对每个(i,x,y),x-y建边,相当于每个任务都对应一条边,要完成所有任务,就相当于要用点,把所有的任务对应的边覆盖,所以就是最小点覆盖问题。

|最小点覆盖|=|最大匹配数|

代码:

 

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<deque>
#include<cmath>
#include<map>
#define sd(x) scanf("%d",&x)
#define ms(x) memset(x,0,sizeof x)
#define fr(i,n) for(int i=1;i<=n;i++)
using namespace std;
typedef long long ll;
const int maxn=506;
const int INF=0x4f4f4f4f;
int mat[maxn]; 
bool vis[maxn];
vector<int>to[maxn];
bool find(int x)
{
    vis[x]=1;
    for(int i=0;i<to[x].size();i++)
    {
        int y=to[x][i];
        if(!mat[y])
        {
            mat[y]=x;
            return 1;
        }
        else 
        {
            int nxt=mat[y];
            if(!vis[nxt])
            {
                if(find(nxt)) 
                {
                    mat[y]=x;
                    return 1;
                }
            }
        }
    }
    return 0;
}
int main()
{
    int n,m,k,x,y;
    while(1)
    {
        cin>>n;
        if(n==0) break;
        cin>>m>>k;
        ll ans=0;
        int p;
        ms(mat);
        fr(i,n) to[i].clear();//其中的fr就是for(1~n),ms就是memset,(个人代码习惯)
        fr(i,k) sd(p),sd(x),sd(y),to[x].push_back(y);//存边,建边  
        fr(i,n) 
        {
            ms(vis);
            if(find(i)) ans++;
        }
        cout<<ans<<endl;
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/studyshare777/p/12913690.html