Problem 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
解题思路:
将S与机器A的所有模式连一条容量为1的边
将机器A的模式与对应的任务连一条容量为1的边
将每个任务与机器B的对应模式连一条容量为1的边
将机器B的所有模式与E连一条容量为1的边
代码:
#include
using namespace std;
inline int read(){
int x=0,f=0;char ch=getchar();
while(ch>'9'||ch<'0')f|=ch=='-',ch=getchar();
while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
return f?-x:x;
}
namespace NetFlow{
const int MAXN=1e3,MAXM=1e5,INF=0x3f3f3f3f;//点数/边数
struct Edge{
int to,next,cap,flow;
}edge[MAXM];
int tol,head[MAXN],gap[MAXN],dep[MAXN],cur[MAXN],Q[MAXN],S[MAXN];
void init(){tol=0;memset(head,-1,sizeof(head));}
void link(int u,int v,int w){
edge[tol]={v,head[u],w,0},head[u]=tol++;
edge[tol]={u,head[v],0,0},head[v]=tol++;
}
void BFS(int start,int end){
memset(dep,-1,sizeof(dep));
memset(gap,0,sizeof(gap));
gap[0]=1,dep[end]=0;
int front=0,rear=0;
Q[rear++]=end;
while(front!=rear){
int u=Q[front++];
for(int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].to;
if(dep[v]!=-1)continue;
Q[rear++]=v,dep[v]=dep[u]+1;
++gap[dep[v]];
}
}
}
int ISAP(int start,int end,int N){//源点/汇点/点数
BFS(start,end);
memcpy(cur,head,sizeof(head));
int top=0,u=start,maxflow=0;
while(dep[start]<N){
if(u==end){
int Min=INF,inser;
for(int i=0;i<top;++i)if(Min>edge[S[i]].cap-edge[S[i]].flow)
Min=edge[S[i]].cap-edge[S[i]].flow,inser=i;
for(int i=0;i<top;++i)edge[S[i]].flow+=Min,edge[S[i]^1].flow-=Min;
maxflow+=Min,top=inser,u=edge[S[top]^1].to;
continue;
}
bool flag=false;int v;
for(int i=cur[u];i!=-1;i=edge[i].next){
v=edge[i].to;
if(edge[i].cap-edge[i].flow&&dep[v]+1==dep[u]){
flag=true,cur[u]=i;break;
}
}if(flag){
S[top++]=cur[u];
u=v;continue;
}
int Min=N;
for(int i=head[u];i!=-1;i=edge[i].next)
if(edge[i].cap-edge[i].flow&&dep[edge[i].to]<Min)
Min=dep[edge[i].to],cur[u]=i;
--gap[dep[u]];
if(!gap[dep[u]])return maxflow;
dep[u]=Min+1;++gap[dep[u]];
if(u!=start)u=edge[S[--top]^1].to;
}
return maxflow;
}
}
using namespace NetFlow;
int n,m,k;
int main(){
while(~scanf("%d",&n),n){
scanf("%d%d",&m,&k);
init();
for(int i=1+k;i<=n+k;++i)link(0,i,1);
for(int i=n+k+1;i<=n+m+k;++i)link(i,n+m+k+1,1);
for(int i=0;i<k;++i){
int x=read(),u=read(),v=read();
link(u+k,x+1,1);
link(x+1,v+k+n,1);
}
printf("%d\n",ISAP(0,n+m+k+1,n+m+k+2));
}
return 0;
}
匈牙利算法:
#include
#define maxn 203
using namespace std;
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
return x*f;
}
int n,m,k,line[maxn][maxn],used[maxn],nxt[maxn];
int Find(int x){
for(int i=n+1;i<=n+m;++i)
if(line[x][i]&&!used[i]){
used[i]=1;
if(nxt[i]==0||Find(nxt[i])){
nxt[i]=x;
return 1;
}
}
return 0;
}
int match(){
int sum=0;
for(int i=1;i<=n;++i){
memset(used,0,sizeof(used));
if(Find(i))++sum;
}
return sum;
}
int main(){
while(~scanf("%d",&n),n){
scanf("%d%d",&m,&k);
memset(line,0,sizeof(line));
memset(nxt,0,sizeof(nxt));
for(int i=0;i<k;++i){
int x=read(),u=read(),v=read();
if(u&&v)line[u][v+n]=1;
}
printf("%d\n",match());
}
return 0;
}