ZOJ 1364 Machine Schedule(二分图最大匹配)

原创 2015年07月10日 15:30:36

题意 机器调度问题 有两个机器A,B A有n种工作模式0...n-1 B有m种工作模式0...m-1 然后又k个任务要做 每个任务可以用A机器的模式i或b机器的模式j来完成 机器开始都处于模式0 每次换模式时都要重启 问完成所有任务机器至少重启多少次


最基础的二分图最大匹配问题 对于每个任务把i和j之间连一条边就可以构成一个二分图 那么每个任务都可以对应一条边 那么现在就是要找最少的点 使这些点能覆盖所有的边 即点覆盖数 又因为二分图的点覆盖数 = 匹配数 那么就是裸的求二分图最大匹配问题了 两边的点数都不超过100直接DFS增广就行了

#include <bits/stdc++.h>
using namespace std;
const int N = 105;
int g[N][N], a[N], b[N], vis[N];
int n, m, k, ans;

int dfs(int i)   //DFS增广
{
    for(int j = 1; j < m; ++j)
    {
        if(g[i][j] && !vis[j])
        {
            vis[j] = 1;
            if( b[j] == -1 || dfs(b[j]))
            {
                //机器a的模式i与机器b的模式j匹配
                a[i] = j;
                b[j] = i;
                return 1;
            }
        }
    }
    return 0;
}

int main()
{
    int u, v, t;
    while(scanf("%d", &n), n)
    {
        memset(g, 0, sizeof(g));
        scanf("%d%d", &m, &k);
        for(int i = 0; i < k; ++i)
        {
            scanf("%d%d%d", &t, &u, &v);
            g[u][v] = 1;
        }

        ans = 0;
        memset(a, -1, sizeof(a));
        memset(b, -1, sizeof(b));
        //状态0不需要重启  所以可以忽略0
        for(int i = 1; i < n; ++i)
        {
            if(a[i] == -1)  //i没被匹配 以i为起点找一条增广路
            {
                memset(vis, 0, sizeof(vis));
                ans += dfs(i);  //
            }
        }

        printf("%d\n", ans);
    }
    return 0;
}
//Last modified :   2015-07-10 14:50

Machine Schedule

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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




版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

ZOJ 1364 Machine Schedule (二分图最大匹配)

解题思路:二分图最大匹配模板题  每一个任务在AB两种机器模式上连边,最小点覆盖数 = 最大匹配数 AC代码: /* @Author: wchhlbt @Date: 2017/...
  • wchhlbt
  • wchhlbt
  • 2017年07月31日 23:15
  • 53

POJ 1325 && ZOJ 1364--Machine Schedule【二分图 && 最小点覆盖数】

由题意可知,本意要求的是二分图的最小点覆盖集问题,即最小的顶点集合,“覆盖”所有的边,可以转化成二分图的最大匹配问题。 二分图的最小点覆盖数 == 最大匹配数。...
  • hpuhjh
  • hpuhjh
  • 2015年08月24日 10:02
  • 593

Machine Schedule HDoj (二分图最小顶点覆盖--最大匹配数)

Machine Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others...

POJ 1325 Machine Schedule (最小点覆盖 && 二分图最大匹配)

鏈接: http://poj.org/problem?id=1325 Description As we all know, machine scheduling is a very class...

求解二分图的最大匹配的匈牙利算法---POJ 1325 Machine Schedule

【基本概念】 二分图:简单来说,如果图中点可以被分为两组,并且使得所有边都跨越组的边界,则这就是一个二分图。准确地说:把一个图的顶点划分为两个不相交集 U 和V ,使得每一条边都分别连接U、V中的顶点...

poj 1325 Machine Schedule 二分图最小点覆盖=最大匹配

Description As we all know, machine scheduling is a very classical problem in computer science and ...

(杭电1150)(poj1325) Machine Schedule (二分图最大匹配)

http://acm.hdu.edu.cn/showproblem.php?pid=1150 http://poj.org/problem?id=1325

二分图最小覆盖点数(最大匹配):Machine Schedule

Machine ScheduleTime Limit:1000MS    Memory Limit:10000KB    64bit IO Format:%I64d & %I64u Submit...
  • lethic
  • lethic
  • 2012年08月09日 01:01
  • 621

POJ 1325-Machine Schedule(二分图匹配-匈牙利算法)

Machine Schedule Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14308   Accepted...
  • MIKASA3
  • MIKASA3
  • 2016年08月17日 19:02
  • 456

poj1325 Machine Schedule (最小点覆盖 二分图匹配)

题意: 有一些两个机器A和B,分别有n种模式和m种模式, 现在有k个任务,每个任务可以在A机器的x模式下完成,也可以在B机器的y模式下完成。 A和B一开始都是模式0,每变换一次模式要重启一次,问...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:ZOJ 1364 Machine Schedule(二分图最大匹配)
举报原因:
原因补充:

(最多只允许输入30个字)