Machine Schedule
Time Limit: 1000MS   Memory Limit: 10000K
     

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

题意:有N个任务和两台机器A、B,每台机器分别有m与k个工作模式,每个任务可以分别由A机器的其中一个模式完成也可以由B机器的一种一个模式完成。遗憾的是,每台机器每切换一次模式都需要重启一次。两台机器初始模式都是0,求完成全部的任务最少需要重启机器多少次。

说实话此题困扰我很久,去讨论区看大神们的思路虽然很明了,但就是有一点不明白,为什么要在两台机器的两个模式之间建图,联想到POJ-3041,恍然大悟。那个题的题解我也写了博客,有兴趣可以看看那个题再看这个题,相信会有很大的收获的。

思路:类似于3041的将点看成边,这里将每个任务看成一条边,每条边都连接着两个模式,将图构建好了后,我们仔细想想,此题要求最少重启的次数完成所有任务,换句话说就是求最少的点覆盖所有的边,俨然转化成了一个二分匹配裸模板题了。

另外:此题需要特别注意的是,建图的时候需要将模式为0的任务忽略,因为两台机器初始模式为0,不用重启。

//const int INF=0x3f3f3f3f;
const int N=500+10;
int n,m,k,g[N][N],linked[N],v[N];
int dfs(int u)
{
for(int i=1;i<=m;i++)
if(!v[i]&&g[u][i])
{
v[i]=1;
if(linked[i]==-1||dfs(linked[i]))
{
linked[i]=u;
return 1;
}
}
return 0;
}
void hungary()
{
int ans=0;
memset(linked,-1,sizeof(linked));
for(int i=1;i<=n;i++)
{
memset(v,0,sizeof(v));
if(dfs(i)) ans++;
}
printf("%d\n",ans);
}
int main()
{
while(~scanf("%d",&n),n)
{
scanf("%d%d",&m,&k);
int u,v;
memset(g,0,sizeof(g));
for(int i=0;i<k;i++)
{
scanf("%d%d%d",&i,&u,&v);
if(u&&v) g[u][v]=1;
}
hungary();
}
return 0;
}

此题让我再次领略算法魅力:花样解决问题,巧妙有趣。

POJ-1325 Machine Schedule,和3041有着异曲同工之妙,好题!的更多相关文章

  1. POJ 1325 Machine Schedule——S.B.S.

    Machine Schedule Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13731   Accepted: 5873 ...

  2. poj 1325 Machine Schedule 二分匹配,可以用最大流来做

    题目大意:机器调度问题,同一个任务可以在A,B两台不同的机器上以不同的模式完成.机器的初始模式是mode_0,但从任何模式改变成另一个模式需要重启机器.求完成所有工作所需最少重启次数. ======= ...

  3. poj 1325 Machine Schedule 题解

    Machine Schedule Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14479   Accepted: 6172 ...

  4. HDU - 1150 POJ - 1325 Machine Schedule 匈牙利算法(最小点覆盖)

    Machine Schedule As we all know, machine scheduling is a very classical problem in computer science ...

  5. poj 1325 Machine Schedule 最小点覆盖

    题目链接:http://poj.org/problem?id=1325 As we all know, machine scheduling is a very classical problem i ...

  6. poj 1325 Machine Schedule

    Time Limit: 1000 MS Memory Limit: 10000 KB 64-bit integer IO format: %I64d , %I64u   Java class name ...

  7. POJ 1325 Machine Schedule(最小点覆盖)

    http://poj.org/problem?id=1325 题意: 两种机器A和B.机器A具有n种工作模式,称为mode_0,mode_1,...,mode_n-1,同样机器B有m种工作模式mode ...

  8. poj 1325 Machine Schedule 解题报告

    题目链接:http://poj.org/problem?id=1325 题目意思:有 k 个作业,机器A有 n 个模式:0 ~ n-1,机器B 有 m 个模式:0~ m-1.每一个作业能运行在 A 的 ...

  9. POJ 1325 Machine Schedule(zoj 1364) 最小覆盖数

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=364 http://poj.org/problem?id=1325 题目大意: ...

随机推荐

  1. _bzoj2005 [Noi2010]能量采集

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2005 令F(i)表示i | gcd(x, y)的对数,f(i)表示gcd(x, y) = i ...

  2. 离散化+线段树/二分查找/尺取法 HDOJ 4325 Flowers

    题目传送门 题意:给出一些花开花落的时间,问某个时间花开的有几朵 分析:这题有好几种做法,正解应该是离散化坐标后用线段树成端更新和单点询问.还有排序后二分查找询问点之前总花开数和总花凋谢数,作差是当前 ...

  3. java的构造方法 this 重载

    this1.隐含的局部变量在方法中指向调用该方法的对象()使用:当成员变量与局部变量同名的时候,通过this说明哪一个是成员变量.(this指向的是成员变量) 2.作为当前类的构造方法名存在作用:在构 ...

  4. 对数组随机赋值,并输出(Arrays.toString(arr))

    import java.util.Arrays; public class Demo { public static void main(String[] args) { int[] arr = ne ...

  5. 关于cocoapods安装与使用的总结

    昨天晚上研究了很入的cocoapods,在各大论坛也看过了很多方法,这里把之前的方法做一个总结. 这里我把自己遇到的一些问题,大概的说了一下.也让广告初学者少走弯路. 先是来自code4app的文章: ...

  6. java格式化sql

    在日志分析中,经常会对记录的sql进行分析,所以将一整行sql格式化,进行多行缩就显得很有必要,许多数据库客户端都提供sql的格式化功能,但复杂的多层嵌套sql往往格式化的l还不够友好,所以就自己造了 ...

  7. Node.js——Buffer

    介绍 JavaScript没有读取和操作二进制数据流的机制,但是 node.js 引入了Buffer 类型,可以操作TCP流或者文件流 使用Buffer可以用来对临时数据(二进制数据)进行存储,当我们 ...

  8. libevent学习之网络通信

    服务器端要实现网络通信,肯定会用到socket等函数,这几个函数应该没什么问题.libevent默认情况下是单线程的,可以配置成多线程,每个线程有一个event_base,对应一个struct eve ...

  9. Oracle中的执行计划

    使用autotrace sqlplus系统参数:SQL> set autotrace trace onSQL> select * from dual;DUM---XExecution Pl ...

  10. CAD使用GetxDataDouble读数据(com接口)

    主要用到函数说明: MxDrawEntity::GetxDataDouble2 读取一个Double扩展数据,详细说明如下: 参数 说明 [in] LONG lItem 该值所在位置 [out, re ...