POJ 1325 Machine Schedule (the minimum point set in the bipartite graph covers the Hungary algorithm)

Source: Internet
Author: User

POJ 1325 Machine Schedule (the minimum point set in the bipartite graph covers the Hungary algorithm)

 

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

 

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

Obviusly, 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 invocations. 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 shoshould be one integer per line, which means the minimal times of restarting machine.

Sample Input

5 5 100 1 11 1 22 1 33 1 44 2 15 2 26 2 37 2 48 3 39 4 30

Sample Output

3

Source

Beijing 2002

Question link: http://poj.org/problem? Id = 1325

Two machines, each of which can work in different modes of different machines. A x y indicates that job A can be completed in x mode of machine 1 or y mode of Machine 2, to complete all the jobs, you must constantly switch the machine mode and try to find a suitable allocation relationship to minimize the number of switches. Note that both machine 1 and machine 2 are working in the 0 mode and the minimum number of times is obtained.

Question analysis: the problem of least point set coverage in a bipartite graph. Machines 1 and 2 are used as two sides of the Bipartite Graph to create edges in 1 and 2 machine modes corresponding to each job, then we need to find the minimum point coverage of the Bipartite Graph, that is, the smallest point set, so that it contains all the edges. According to the minimum point set coverage = maximum matching of the bipartite graph, therefore, you can directly use the Hungary algorithm to solve the maximum matching problem. Note that when the 0 mode is used, do not connect edges.

#include 
 
  #include 
  
   int const MAX = 105;bool g[MAX][MAX];int cx[MAX], cy[MAX];bool vis[MAX];int n, m, k;int DFS(int x){    for(int y = 0; y < m; y++)    {        if(!vis[y] && g[x][y])        {            vis[y] = true;            if(cy[y] == -1 || DFS(cy[y]))            {                cx[x] = y;                cy[y] = x;                return 1;            }        }    }    return 0;}int MaxMatch(){    int res = 0;    memset(cx, -1, sizeof(cx));    memset(cy, -1, sizeof(cy));    for(int i = 0; i < n; i++)    {        if(cx[i] == -1)        {            memset(vis, false, sizeof(vis));            res += DFS(i);        }    }    return res;}int main(){    while(scanf("%d", &n) != EOF && n)    {        memset(g, false, sizeof(g));        scanf("%d %d", &m, &k);        for(int i = 0; i < k; i++)        {            int tmp, x, y;            scanf("%d %d %d", &tmp, &x, &y);            if(x * y)                g[x][y] = true;        }        int ans = MaxMatch();        printf("%d\n", ans);    }}
  
 




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.