1. 程式人生 > >POJ 1325 Machine Schedule (最小點覆蓋 && 二分圖最大匹配)

POJ 1325 Machine Schedule (最小點覆蓋 && 二分圖最大匹配)

<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 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

題意:
有兩臺機器A和B,分別有n種和m種不同的模式,有k個工作,每個工作都可以在那兩個機器的某種特定的模式下處理。
如job0既可以在A機器的3號模式下處理,也可以在B機器的4號模式下處理。

機器的工作模式改變只能通過人工來重啟。通過改變工作的順序,和分配每個工作給合適的機器可以減少重啟機器的次數達到最小。
任務就是計算那個最小的次數。初始時兩臺機器都執行在0號模式下。

思路:
把每個任務化為一條線,假設任務i在A機器上處理的模式為A[x]點,在B機器上為B[y]點,連線A[x]和B[y],
用A機器和B機器中最少的點覆蓋所有的邊(用最少的模式完成所有的任務)。
這是最小點覆蓋問題,根據König定理(一個二分圖中的最大匹配數等於這個圖中的最小點覆蓋數)就是求的二分圖的最大匹配,
然後再用匈牙利演算法直接就算出最大匹配數了,要注意的是初始是在0號模式下,
所以如果A或B機器其中至少有個在0號模式下時就不用重啟機器了,所以建圖的時候沒有把0建進去。

程式碼:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#define MAXN 100
#define RST(N)memset(N, 0, sizeof(N))
using namespace std;

int m, n, k, Mc, Md, Me;
bool map[MAXN][MAXN], vis[MAXN];
int link[MAXN], res;

bool find(int x)
{
    for(int i=1; i<n; i++) if(map[x][i]&&!vis[i]) {
            vis[i] = true;
            if(link[i] == 0 || find(link[i])) {
                link [i] = x ;
                return true ;
            }
    }
    return false;
}

void Init()
{
    RST(map), RST(link);
    for(int i=0; i<k; i++) {
        scanf("%d %d %d", &Mc, &Md, &Me);
        if(Md == 0 || Me == 0) continue;
        map[Md][Me] = true;
    }
    res = 0;
}

int main()
{
    while (~scanf("%d %d %d", &m, &n, &k) && m) {
        Init();
        for(int i=1; i<m; i++) {
            RST(vis);
            if(find(i)) res++;
        }
        printf("%d\n", res);
    }
    return 0;
}


相關推薦

POJ 1325 Machine Schedule 覆蓋 && 二分匹配

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"&g

POJ 1325 Machine Schedule匹配數=覆蓋

題意:給你2個機器A(A的模式有n種,標號從0到n-1)和B(B的模式有m種,標號從0到m-1),然後給你k個任務,(i,x,y)表示做完第i個任務可以用A機器的x模式和B機器的y模式 思路:很裸的二分圖最小點覆蓋,這道題需要注意點,A,B機器剛開始的模式都是0,所以在模

poj 1325 Machine Schedule頂點覆蓋+匹配

http://poj.org/problem?id=1325 題意:有AB兩臺機器和k個任務,機器A有n種模式,機器B有m種模式,初始均工作在模式0,每個任務都可以由機器A的一種模式或機器B的一種模式

POJ 2060 路徑覆蓋 二分

這題是一道簡單的最小路徑覆蓋題目,最小路徑覆蓋的數目等於點的數目減去最大匹配數。#include <stdio.h> #include <string.h> #include <iostream> #include <algor

POJ - 1325 Machine Schedule 二分 覆蓋

code mach 切換 才幹 ces 任務 ack div con 題目大意:有兩個機器,A機器有n種工作模式,B機器有m種工作模式,剛開始兩個機器都是0模式。假設要切換模式的話,機器就必須的重新啟動 有k個任務,每一個任務都能夠交給A機器的i模式或

Poj 1325 Machine Schedule二分匹配-------覆蓋

Machine Schedule Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14439 Accepted: 6156 Description As we all know, mach

POJ 1325 Machine Schedule (二分覆蓋 匈牙利演算法)

Machine Schedule Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12621 Accepted: 5399

HDU 1150 Machine Schedule覆蓋問題

/* 題意: A機器n種工作模式,B機器m種工作模式,共有k個任務。(i,x,y)代表:任務i可由A機器x模式或者B機器y模式完成。任務順序可以隨便改動,如果A或者B機器需要更換模式,則需要重啟機器。求完成工作,需要最少啟動機器次數。 解題思路: 畫出二分圖,易知該問題為

1150 Machine Schedule 覆蓋二分匹配-匈牙利演算法鄰接表寫法

日! 最近 CF 做的多了,再做多組輸入的題的時候 忘記陣列清空,,然後wa了 好久。。。 我就說嗎。。。這麼裸的匈牙利怎麼會出錯呢? ——————————————————————分割 這個題是最小點覆蓋問題,畫出圖來以後可以知道是找最少的點覆蓋所有邊,每個點覆蓋它相連的邊

hdu 1150 Machine Schedule二分覆蓋

        題意:有兩臺機器,可以分別以n和m種不同的模式執行。有k個任務,每個任務可以在分別在第一臺機器和第二臺機器的兩種模式下完成。每次改變機器的模式需要一定的花費,現在問最少要改變幾次機器

HDU 1150 Machine Schedule覆蓋

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 dif

hdu1150—Machine Schedule覆蓋

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

Machine Schedule覆蓋=匹配

Machine Schedule Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12012 Accepted: 5118 Description As we all know, m

POJ 3041 Asteroids 二分覆蓋

0ms ext ted with width any print scrip avi Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 24789

POJ - 2226】Muddy Fields匈牙利演算法 或 網路流dinic,二分匹配覆蓋,矩陣中優秀的建方式

題幹: Rain has pummeled the cows' field, a rectangular grid of R rows and C columns (1 <= R <= 50, 1 <= C <= 50). While good for the gra

HDU1150 Machine Schedule二分 覆蓋

Machine Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 10961&

POJ - 1463-Strategic game覆蓋

POJ - 1463-Strategic game(最小點覆蓋) Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast e

HDU 1150 Machine Schedule 覆蓋

想一想最小點覆蓋時的那種性質,很容易就會產生要把能生產一種產品的機器連線的想法。我今天看這個題,又像以前一樣,看著看著就自動把題意給忘了然後自己進行了腦補,誤以為一個產品能用一臺機器的一種以上模式生產,結果不能建圖,以後一定引以為戒! 另一個神奇之處就是mod_0這個事。顯

HDU 1150 Machine Schedule 覆蓋數==匹配

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

求解二分匹配的匈牙利演算法---POJ 1325 Machine Schedule

【基本概念】 二分圖:簡單來說,如果圖中點可以被分為兩組,並且使得所有邊都跨越組的邊界,則這就是一個二分圖。準確地說:把一個圖的頂點劃分為兩個不相交集 U 和V ,使得每一條邊都分別連線U、V中的