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

    Encoding Time Limit: 2000/1000ms (Java/Others) Problem Description: 给定一个只包含'A' - 'Z'的字符串,我们可以使用以下方法对 ...

  2. java数组实现买彩票(重复则重新遍历查询思想)

    package com.wh.shuzu; import java.util.Arrays; /** * 买彩票 * @author 丁璐同学 * 重复则重新遍历查询思想 */ public clas ...

  3. jmeter(六)关联

    话说LoadRunner有的一些功能,比如:参数化.检查点.集合点.关联,Jmeter也都有这些功能,只是功能可能稍弱一些,今天就关联来讲解一下. JMeter的关联方法有两种:后置处理器-正则表达式 ...

  4. Uncaught TypeError: Cannot set property 'f7View' of undefined 错误原因

    // 添加视图var mainView = myApp.addView('.view-main', { // 因为我们要用动态的导航栏,我们需要使它的这一观点: dynamicNavbar: true ...

  5. AngularJS入门 & 分页 & CRUD示例

    一.AngularJS 简介 ​ AngularJS  诞生于2009年,由Misko Hevery 等人创建,后为Google所收购.是一款优秀的前端JS框架,已经被用于Google的多款产品当中. ...

  6. java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to

    在做android解析服务器传来的json时遇到的错误. 服务器传来的数据格式 [{"," id":"7ef6815938394fce88a5873312b66 ...

  7. redis 在windows 集群

    前言:为什么自己要花时间写一篇redis集群文章,网上众多的文章大都是思路正确,但是细节不足,这里写一篇文章记录自己部署时候遇到的问题,当下次再部署的时候避免跳入重复的坑. 上篇文章(http://w ...

  8. Objective -C Categories

    Objective -C Categories  The dynamic runtime dispatch mechanism employed by Objective-C lets you add ...

  9. ButterKnife 在父类 点击事件没反应的解决方案

    在用继承的方式实现butterKnife的封装的时候遇到问题, butterKnife就在baseActivity中绑定的,但是父类中公共控件点击事件无效.找了半天原因,原来是子类和父类定义的点击方法 ...

  10. xamarin 学习笔记02- IOS Simulator for windows 安装

    微软发布了在window下的ios模拟器 下载 ios模拟器 并安装在windows系统上. Xamarin for Visual Studio 和 网络上的 Mac 中的 Xamarin.iOS 开 ...