您的位置:首页 > 其它

POJ-1325 Machine Schedule,和3041有着异曲同工之妙,好题!

2016-08-27 17:25 288 查看
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

,linked
,v
;
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;
}


   此题让我再次领略算法魅力:花样解决问题,巧妙有趣。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: