求解Machine Schedule

bary1980 2003-07-13 08:32:24
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
这道题目好象很典型,但是我就不会,听说是用图,但是我想不明白,请大侠门指点迷津
...全文
974 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
mmmcd 2003-07-18
  • 打赏
  • 举报
回复
清华赛题,浙大1364

#include<iostream.h>
//#include<fstream.h>
#include<string.h>

#define maxn 105

int nx,ny,jobnum;
int g[maxn][maxn];
int ans;
int sx[maxn],sy[maxn],cx[maxn],cy[maxn];

int path(int u)
{
sx[u]=1;
int v,i;
for(v=1;v<=ny;v++)
if( (g[u][v]>0) && (!sy[v]) )
{
sy[v]=1;
if( (!cy[v]) || (path(cy[v])) )
{
cx[u]=v;
cy[v]=u;
return 1;
}
}
return 0;
}

int solve()
{
ans=0;
int i,j;
memset(cx,0,sizeof(cx));
memset(cy,0,sizeof(cy));
for(i=1;i<=nx;i++)
if(!cx[i])
{
memset(sx,0,sizeof(sx));
memset(sy,0,sizeof(sy));
ans+=path(i);
}

return 0;
}

int main()
{
// ifstream cin("1364.in");
// ofstream cout("1364.out");
int i,j,k,l;

while(cin>>nx,nx>0)
{
cin>>ny>>jobnum;
if(cin.fail())
return 0;
memset(g,0,sizeof(g));
for(k=0;k<jobnum;k++)
{
cin>>l>>i>>j;
g[i][j]=1;
}
solve();
cout<<ans<<endl;
}

return 0;
}
jwd_1_cool 2003-07-16
  • 打赏
  • 举报
回复
包括所有边的点的最少个数! 浅显易懂了吧!至于程序,大都采用匈牙利算法。自己查下就知道了
Riemann 2003-07-15
  • 打赏
  • 举报
回复
如果图G的顶点集可划分为两个集合X,Y,使得图G的每条边的两个端点都位于不同的集合中,那么图G就称为二部图,或二分图。
对于此问题,以机器的工作模式为顶点,分为两个集合A,B,分别表示两个机器的工作模式。如果某个任务能够在A的x模式和B的y模式下完成,则从x到y连接一条边,这样就得到一个二部图。原问题就是求这个二部图的最小顶点覆盖数目。根据Koning定理:二部图的最小顶点覆盖数目等于其最大匹配数目。因此本题实际上就是求二部图的最大匹配。
ZhangYv 2003-07-14
  • 打赏
  • 举报
回复
呵呵,看离散数学图论章节:)
bary1980 2003-07-14
  • 打赏
  • 举报
回复
哥们,什么是二部图,我当初就没有搞明白
Riemann 2003-07-13
  • 打赏
  • 举报
回复
你以前好像就问过这个问题,starfish给过解答,用二部图,你不会就忘了吧!

33,008

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧