www.icesr.com
IT运维工程师的摇篮

HDU-1150-Machine Schedule【最小点覆盖】【二分图匹配】


Machine Schedule


Problem 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

题目链接:HDU-1150

题目大意:有两台机器加工零件,A有n种工作模式(a0,a1,a2,a3…an),B有m种工作模式(b0,b1,b2…bm)。现在需要加工k个零件,每个零件输入(i,x,y)表示第i这个零件在A机器上需要用x模式,在B机器上需要y模式完成。零件加工顺序任意,每次改变机器模式需要花费1点值。问最小花费为多少,即最少改变几次模式?
注意,两台机器初始状态都在0状态

题目思路:
1.每个任务必须完成,即必须选择一台机器完成。
2.将A,B两台机器分为两组端点,每个任务只能用A机器完成或者B机器完成,选了一台就不能选另外一台。
3.连线,如图所示。
这里写图片描述
4.每条线代表一个任务,我们要完成所有任务就是要用最小的点覆盖所有的边,所以问题就是求最小点覆盖
5.我们又知道,最小点覆盖=二分图最大匹配,所以问题转化为二分图最大匹配。

以下是代码:

<code><span class="hljs-preprocessor">#include &lt;iostream&gt;</span>
<span class="hljs-preprocessor">#include &lt;iomanip&gt;</span>
<span class="hljs-preprocessor">#include &lt;fstream&gt;</span>
<span class="hljs-preprocessor">#include &lt;sstream&gt;</span>
<span class="hljs-preprocessor">#include &lt;cmath&gt;</span>
<span class="hljs-preprocessor">#include &lt;cstdio&gt;</span>
<span class="hljs-preprocessor">#include &lt;cstring&gt;</span>
<span class="hljs-preprocessor">#include &lt;cctype&gt;</span>
<span class="hljs-preprocessor">#include &lt;algorithm&gt;</span>
<span class="hljs-preprocessor">#include &lt;functional&gt;</span>
<span class="hljs-preprocessor">#include &lt;numeric&gt;</span>
<span class="hljs-preprocessor">#include &lt;string&gt;</span>
<span class="hljs-preprocessor">#include &lt;set&gt;</span>
<span class="hljs-preprocessor">#include &lt;map&gt;</span>
<span class="hljs-preprocessor">#include &lt;stack&gt;</span>
<span class="hljs-preprocessor">#include &lt;vector&gt;</span>
<span class="hljs-preprocessor">#include &lt;queue&gt;</span>
<span class="hljs-preprocessor">#include &lt;deque&gt;</span>
<span class="hljs-preprocessor">#include &lt;list&gt;</span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;

<span class="hljs-comment">/*数组下标从1开始*/</span>
<span class="hljs-keyword">const</span> <span class="hljs-keyword">int</span> N = <span class="hljs-number">300</span>;

<span class="hljs-keyword">char</span> <span class="hljs-built_in">map</span>[N][N];
<span class="hljs-keyword">int</span> col[N][N],row[N][N];
<span class="hljs-keyword">int</span> link[N],head[N];
<span class="hljs-keyword">bool</span> vis[N];
<span class="hljs-keyword">int</span> cnt,n,m;
<span class="hljs-keyword">int</span> R,C;      <span class="hljs-comment">//两种物品的数量</span>

<span class="hljs-keyword">struct</span> Edge
{
    <span class="hljs-keyword">int</span> to;
    <span class="hljs-keyword">int</span> next;
};

Edge edge[N*N];

<span class="hljs-keyword">void</span> Init() <span class="hljs-comment">//初始化</span>
{
    cnt = <span class="hljs-number">0</span>;
    <span class="hljs-built_in">memset</span>(head,-<span class="hljs-number">1</span>,<span class="hljs-keyword">sizeof</span>(head));
    <span class="hljs-built_in">memset</span>(col,<span class="hljs-number">0</span>,<span class="hljs-keyword">sizeof</span>(col));
    <span class="hljs-built_in">memset</span>(row,<span class="hljs-number">0</span>,<span class="hljs-keyword">sizeof</span>(row));
}

<span class="hljs-keyword">void</span> add(<span class="hljs-keyword">int</span> u,<span class="hljs-keyword">int</span> v)   <span class="hljs-comment">//u和v相连</span>
{
    edge[cnt].to = v;
    edge[cnt].next = head[u];
    head[u] = cnt++;
}

<span class="hljs-keyword">bool</span> dfs(<span class="hljs-keyword">int</span> u)
{
    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i=head[u]; ~i; i=edge[i].next)
    {
        <span class="hljs-keyword">int</span> v = edge[i].to;
        <span class="hljs-keyword">if</span>(!vis[v])
        {
            vis[v] = <span class="hljs-number">1</span>;
            <span class="hljs-keyword">if</span>(link[v] == -<span class="hljs-number">1</span> || dfs(link[v]))
            {
                link[v] = u;
                <span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>;
            }
        }
    }
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">false</span>;
}

<span class="hljs-keyword">int</span> match()  <span class="hljs-comment">//直接调用</span>
{
    <span class="hljs-keyword">int</span> ans = <span class="hljs-number">0</span>;
    <span class="hljs-built_in">memset</span>(link,-<span class="hljs-number">1</span>,<span class="hljs-keyword">sizeof</span>(link));
    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i=<span class="hljs-number">1</span>; i&lt;=R; i++)
    {
        <span class="hljs-built_in">memset</span>(vis,<span class="hljs-number">0</span>,<span class="hljs-keyword">sizeof</span>(vis));
        <span class="hljs-keyword">if</span>(dfs(i)) ans++;
    }
    <span class="hljs-keyword">return</span> ans;
}

<span class="hljs-keyword">int</span> main()
{
    <span class="hljs-keyword">int</span> k;
    <span class="hljs-keyword">while</span>(<span class="hljs-built_in">cin</span> &gt;&gt; R &amp;&amp; R)
    {
        <span class="hljs-built_in">cin</span> &gt;&gt; C &gt;&gt; k;
        Init();  <span class="hljs-comment">//初始化</span>
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; k; i++)
        {
            <span class="hljs-keyword">int</span> u,v,no;
            <span class="hljs-built_in">cin</span> &gt;&gt; no &gt;&gt; u &gt;&gt; v;
            <span class="hljs-keyword">if</span> (u == <span class="hljs-number">0</span> &amp;&amp; v == <span class="hljs-number">0</span>) <span class="hljs-keyword">continue</span>;
            add(u,v);
        }
        <span class="hljs-keyword">int</span> ans = match();
        <span class="hljs-built_in">cout</span> &lt;&lt; ans &lt;&lt; endl;
    }
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}</code>

未经允许不得转载:冰点网络 » HDU-1150-Machine Schedule【最小点覆盖】【二分图匹配】

分享到:更多 ()

评论 抢沙发

评论前必须登录!