Machine Schedule(UVA 1194)

本文探讨了计算机科学中的经典问题——机器调度,特别是在两台具有多种工作模式的机器上的任务分配问题。通过将问题转换为二分图完美匹配问题,使用匈牙利算法来找到最小化机器重启次数的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Machine Schedule

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 besatisfied 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 mode0,mode1, . . ., moden−1, likewise machine B has m kinds of working modes, mode0, mode1, . . . , modem−1.At the beginning they are both work at mode0.

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 mode3 or in machine B at mode4,
job 1 can either be processed in machine A at mode2 or in machine B at mode4, 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 modex, or in machine B at modey.

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


二分图完美匹配(匈牙利算法)裸题,但是建模却非常难想;

首先分析一下这道题,给你 k 件事,每件事有两种解决办法,题目变相要求你的就是用最少的方法解决这些事(因为换一次方法就要重启一次机器);

但是这怎么会跟二分图匹配挂上钩呢?这时我们要知道一个概念:二分图最小点覆盖

它的含义就是给定一张二分图,求出一个最小的点集 S ,使得图中任意一条边都已至少一个端点属于 S ,这个被称为二分图的最小点覆盖;并且最小点覆盖包含的点数=二分图最大匹配包含的边数

题目的每件事的两种方法不就可以当做一条边,这道题就可以转化为求最大匹配了;

代码:

#include<bits/stdc++.h>
#define LL long long
#define pa pair<int,int>
#define lson k<<1
#define rson k<<1|1
#define inf 0x3f3f3f3f
//ios::sync_with_stdio(false);
using namespace std;
const int N=200100;
const int M=200100;
const LL mod=998244353;
struct Node{
	int to,nex;
}edge[10100];
int cnt;
int head[1000];
void add(int p,int q){
	edge[cnt].to=q;
	edge[cnt].nex=head[p];
	head[p]=cnt++;
}
bool v[1010];
int fa[1010];
bool find(int p){
	for(int i=head[p];~i;i=edge[i].nex){
		int q=edge[i].to;
		if(!v[q]){
			v[q]=true;
			if(fa[q]==0||find(fa[q])){//跟并查集有点相似 
				fa[q]=p;
				return true;
			}
		}
	}
	return false;
}
int main(){
	ios::sync_with_stdio(false);
	int n,m,k;
	while(cin>>n){
		if(n==0) break;
		cin>>m>>k;
		cnt=0;
		memset(head,-1,sizeof(head)); 
		for(int i=1;i<=k;i++){
			int x,y,j;
			cin>>j>>x>>y;
			add(x,y);
		}
		int s=0;
		memset(fa,0,sizeof(fa));
		for(int i=1;i<=n;i++){
			memset(v,false,sizeof(v));
			if(find(i)) s++;
		}
		cout<<s<<endl;		
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值