zoj1364 poj1325
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
求最小的顶点集合 覆盖住所有边 转换成求二部图的最大匹配问题 二部图的点覆盖书a = 匹配数b
#include<cstring> #include<iostream> #include<string> #include<cstdio> using namespace std; const int maxn = 505; int match[maxn];int used[maxn];int map[maxn][maxn];int n,m; int dfs(int s) { int i,temp; for(i=1;i<=m;i++) { if(map[s][i] && !used[i]) { used[i] = 1; // temp = match[i]; if(match[i]==-1 || dfs(match[i])) { match[i] = s; return 1; } } } return 0; } int solve() { int i,ans= 0; memset(match,-1,sizeof(match)); for(i=1;i<=n;i++) { memset(used,0,sizeof(used)); if(dfs(i)) ans++; //if(ans==n) break; } return ans; } int main() { int x,y,i,f,k,h; while(scanf("%d",&n)!=EOF && n) { scanf("%d %d",&m,&h); memset(map,0,sizeof(map)); for(k=0;k<h;k++) { scanf("%d %d %d",&f,&x,&y); map[x][y] = 1; } int t = solve(); printf("%d\n",t); } return 0; }
#include <cstdio> #include <cstring> #define maxn 105 int nx; //机器A的工作模式个数 int ny; //机器B的工作模式个数 int jobnum; //作业个数 int g[maxn][maxn]; //所构造的二部图 int ans; //最大匹配数 int sx[maxn], sy[maxn]; //path函数所表示的DFS算法中用来标明顶点访问状态的数组 int cx[maxn], cy[maxn];//求得的匹配情况,X集合中的顶点i匹配给Y集合中的顶点cx[i] //从X集合中的顶点u出发用深度优先的策略寻找增广路 //(这种增广路只能使当前的匹配数增加1) int path( int u ) { sx[u] = 1; int v; //考虑所有Yi顶点(机器A和B最初工作在模式0, //所以完成可以工作在模式0的作业时部需要重启机器) for( v=1; v<=ny; v++ ) { if( (g[u][v]>0) && (!sy[v]) ) //v跟u邻接并且v未访问过 { sy[v] = 1; //如果v没有匹配 //或者如果v已经匹配了,但从y[v]出发可以找到一条增广路 if( !cy[v] || path(cy[v]) ) { //在回退过程修改增广路上的匹配,从而可以使匹配数增加1 cx[u] = v; cy[v] = u; //把v匹配给u, 把u匹配给v return 1; } } } return 0; } int solve( )//求二部图的最大匹配算法 { ans = 0; int i; memset( cx, 0, sizeof(cx) ); memset( cy, 0, sizeof(cy) ); //机器A和B最初工作在模式0,所以完成可以工作在模式0的作业时部需要重启机器 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( ) { int i, x, y; //三元组 int k; //循环变量 while( scanf( "%d", &nx ) ) { if( nx==0 ) break; scanf( "%d%d", &ny, &jobnum ); memset( g, 0, sizeof(g) ); for( k=0; k<jobnum; k++ ) { scanf( "%d%d%d", &i, &x, &y ); g[x][y] = 1;//构造二部图 } solve( ); printf( "%d\n", ans ); } return 0; }