HDU 1150 最小点覆盖数

Machine Schedule

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5614    Accepted Submission(s): 2804

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

题目意思:

有两个机器A和B,有K个任务需要在机器上操作,A机器有a个模式,B机器有b个模式,任务在机器上操作时需要对应特定的模式才能操作,比如任务2在A机器上操作时需要3模式而在B机器上操作时需要2模式,可能多个任务在某个机器上对应同一模式,而如果两个任务不同模式时,做完前面任务再做这个任务时需要更换机器模式,每更换一次时间为1,

求任务全部完成用的最小时间。

思路:

时间只有模式更换时间,那么若想用的总时间最小,需要更换模式的次数最少,假设以A机器的a个模式为行,B机器的b个模式为列建a*b的图,在图上有k个点即对应该行该列的任务,有种操作是覆盖一行或一列,那么问题就转换成用最少的操作使得覆盖图上所有的点,很明显这个模型就是最小覆盖点数。在二分图里,最小覆盖点数=最大二分图匹配数。

代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <vector>
 5 #include <iostream>
 6 using namespace std;
 7 #define N 110
 8
 9 vector<int>ve[N];
10 int from[N];
11 int visited[N];
12 int n, m, k;
13
14
15 int mark(int u){
16     int i, v;
17     for(i=0;i<ve[u].size();i++){
18         v=ve[u][i];
19         if(!visited[v]){
20             visited[v]=1;
21             if(from[v]==-1||mark(from[v])){
22                 from[v]=u;
23                 return 1;
24             }
25         }
26     }
27     return 0;
28 }
29 main()
30 {
31     int i, j;
32     while(scanf("%d",&n)==1&&n){
33         scanf("%d %d",&m,&k);
34         memset(from,-1,sizeof(from));
35         for(i=0;i<=n;i++) ve[i].clear();
36         int x, y, z;
37         while(k--){
38             scanf("%d %d %d",&z,&x,&y);
39             ve[x].push_back(y);
40         }
41         int num=0;
42         for(i=1;i<=n;i++){
43             memset(visited,0,sizeof(visited));
44             if(mark(i))
45             num++;
46         }
47         printf("%d\n",num);
48      }
49 }

HDU 1150 最小点覆盖数

时间: 2024-10-10 10:42:17

HDU 1150 最小点覆盖数的相关文章

hdu 1150 最小点覆盖

题目大意;有两台机器A和B以及N个需要运行的任务.每台机器有M种不同的模式,而每个任务都恰好在一台机器上运行.如果它在机器A上运行,则机器A需要设置为模式xi,如果它在机器B上运行,则机器A需要设置为模式yi.每台机器上的任务可以按照任意顺序执行,但是每台机器每转换一次模式需要重启一次.请合理为每个任务安排一台机器并合理安排顺序,使得机器重启次数尽量少. 注意状态为0的即可 1 #include<cstdio> 2 #include<algorithm> 3 #include<

hdu 1150 Machine Schedule(二分图-最小顶点覆盖)

Machine Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5424    Accepted Submission(s): 2691 Problem Description As we all know, machine scheduling is a very classical problem in compu

hdu 1150 Machine Schedule hdu 1151 Air Raid 匈牙利模版

//两道大水……哦不 两道结论题 结论:二部图的最小覆盖数=二部图的最大匹配数 有向图的最小覆盖数=节点数-二部图的最大匹配数 1 //hdu 1150 2 #include<cstdio> 3 #include<iostream> 4 #include<cmath> 5 #include<algorithm> 6 #include<cstring> 7 #include<cstdlib> 8 #include<queue>

hdu 3657 最小割的活用 / 奇偶方格取数类经典题 /最小割

题意:方格取数,如果取了相邻的数,那么要付出一定代价.(代价为2*(X&Y))(开始用费用流,敲升级版3820,跪...) 建图:  对于相邻问题,经典方法:奇偶建立二分图.对于相邻两点连边2*(X&Y),源->X连边,Y->汇连边,权值w为点权. ans=总点权-最小割:如果割边是源->X,表示x不要选(是割边,必然价值在路径上最小),若割边是Y-汇点,同理:若割边是X->Y,则表示选Y点且选X点, 割为w( 2*(X&Y) ). 自己的确还没有理解其本质

HDU 1150:Machine Schedule(二分匹配,匈牙利算法)

Machine Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5371    Accepted Submission(s): 2658 Problem Description As we all know, machine scheduling is a very classical problem in compu

HDU 4160 最小路径覆盖

Dolls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1040    Accepted Submission(s): 496 Problem Description Do you remember the box of Matryoshka dolls last week? Adam just got another box of

POJ 1325 &amp;amp;&amp;amp; ZOJ 1364--Machine Schedule【二分图 &amp;amp;&amp;amp; 最小点覆盖数】

Machine Schedule Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13071   Accepted: 5575 Description As we all know, machine scheduling is a very classical problem in computer science and has been studied for a very long history. Scheduli

HDU 1350 最小路径覆盖

Taxi Cab Scheme Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 707    Accepted Submission(s): 336 Problem Description Running a taxi station is not all that simple. Apart from the obvious dem

String Problem hdu 3374 最小表示法加KMP的next数组

String Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1492    Accepted Submission(s): 662 Problem Description Give you a string with length N, you can generate N strings by left shifts.