【二分图】poj 1325 Machine Schedule

来源:互联网 发布:微信矩阵的作用 编辑:程序博客网 时间:2024/04/29 20:44
Machine Schedule
Time Limit: 1000MS
Memory Limit: 10000KTotal Submissions: 15423
Accepted: 6616

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 100 1 11 1 22 1 33 1 44 2 15 2 26 2 37 2 48 3 39 4 30

Sample Output

3

题目大意:有两个机器A和B,A机器有n个模式,B机器有m个模式,两个机器最初在0模式

然后有k个作业,每个作业有三个参数i,a,bi代表作业编号,a和b代表第i作业要么在A机器的a模式下完成【或者】在B机器的b模式下完成问两个机器总共最少变换多少次可以完成所有作业

解题思路:很水的一个二分图的最小点覆盖建立一个二分图,左边代表A机器,有n个点,代表有n个模式右边代表B机器,有m个点,代表有m个模式现在对于每个作业,i,a,b都使左边a点指向右边b点即可这样如果我们要把所有的作业做完,是不是找出最小的点覆盖将所有的边都覆盖掉即可然后最小点覆盖等于最大匹配,用匈牙利直接过注意:要删除和0相连的边。。。///AC代码
#include <algorithm>#include <cmath>#include <cstdio>#include <cstring>#include <ctime>#include <iostream>#include <map>#include <queue>#include <set>#include <stack>#include <string>#include <vector>#define eps 1e-8#define INF 0x7fffffff#define maxn 100005#define PI acos(-1.0)using namespace std;typedef long long LL;const int N = 302;/*变种1:二分图的最小顶点覆盖:假如选了一个点就相当于覆盖了以它为端点的所有边,你需要选择最少的点来覆盖所有的边二分图的最小顶点覆盖数 = 二分图的最大匹配数变种2:DAG图(无回路有向图)的最小路径覆盖路径覆盖就是在图中找一些路经,使之覆盖了图中的所有顶点,且任何一个顶点有且只有一条路径与之关联,如果把这些路径中的每条路径从它的起始点走到它的终点,那么恰好可以经过图中的每个顶点一次且仅一次DAG图的最小路径覆盖数 = 节点数(n)- 最大匹配数(m)变种3: 二分图的最大独立集:在图中选取最多的点,使任意所选两点均不相连二分图的最大独立集数 = 节点数(n)- 最大匹配数(m)*//*=***************************************************二分图匹配(匈牙利算法的DFS实现)INIT:g[][]两边定点划分的情况CALL:res=hungary();输出最大匹配数优点:适于稠密图,DFS找增广路快,实现简洁易于理解时间复杂度:O(VE);***************************************************=*/const int MAXN = 1000;int uN, vN; //u,v数目int g[MAXN][MAXN];//编号是0~n-1的int linker[MAXN];bool used[MAXN];bool dfs(int u){    int v;    for (v = 1; v < vN; v++)        if (g[u][v] && !used[v])        {            used[v] = true;            if (linker[v] == -1 || dfs(linker[v]))            {                linker[v] = u;                return true;            }        }    return false;}int hungary(){    int res = 0;    int u;    memset(linker, -1, sizeof(linker));    for (u = 1; u < uN; u++)    {        memset(used, 0, sizeof(used));        if (dfs(u))        {            res++;        }    }    return res;}int main(){    int n, m, k;    while (~scanf("%d", &n) && n)    {        scanf("%d%d", &m, &k);        memset(g, 0, sizeof(g));        uN = n;        vN = m;        int xx, a, b;        for (int i = 0; i < k; i++)        {            scanf("%d%d%d", &xx, &a, &b);            g[a][b] = 1;        }        int ans = hungary();        cout << ans << endl;    }    return 0;}


阅读全文
0 0
原创粉丝点击