1150 Machine Schedule

题目详情:

Machine Schedule

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12936    Accepted Submission(s): 6437


 

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
 

Source
 

Recommend
Ignatius.L
 

题目大意:

两个机器,有多种模式,每个机器都可以重启切换模式,机器默认模式是模式0,现有多种任务,每个任务对应一种模式,问两个机器最少需要重启多少次才可以完成这些任务。

解题思路:

匈牙利算法,此题可以转化成二分图的最大匹配。

AC代码:

#include <iostream>
#include <vector>
#include <cstring>
#define MAX 101
using namespace std;
vector<int> unvisit(MAX, 1); //存放是否访问过
vector<int> bo_f(MAX, -1);   //女生的男朋友
bool dfs(vector<vector<int>> &machine, int b_w, int n, int m){
    for (int i = 0; i < m; ++i){
        if (machine[b_w][i] && unvisit[i]){ //传过来的男生有女朋友且该女生没有访问过
            unvisit[i] = 0;
            if (bo_f[i] == -1 || dfs(machine, bo_f[i], n, m)){ //该女生没有男朋友且dfs后为真
                bo_f[i] = b_w;
                return true;
            }
        }
    }
    return false;
}
int xyl(vector<vector<int>> &machine, int n, int m){
    int s = 0;
    for (int i = 0; i < n; ++i){
        fill(unvisit.begin(), unvisit.end(), 1);
        if (dfs(machine, i, n, m))  ++s;
    }
    return s;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, m, k;
    int i, x, y;
    while (cin >> n && n){
        cin >> m >> k;
        vector<vector<int>> machine(MAX, vector<int>(MAX, 0)); //邻接矩阵
        fill(unvisit.begin(), unvisit.end(), 1);
        fill(bo_f.begin(), bo_f.end(), -1);
        while (k--){
            cin >> i >> x >> y;
            if (x != 0 && y != 0)
                machine[x][y] = 1;
        }
        cout << xyl(machine, n, m) << endl;
    }
    return 0;
}

推荐几款学习编程的网站

免费在线开发平台(LTPP在线开发平台 | LTPP宇宙文档

        探索编程世界的新天地,为学生和开发者精心打造的编程平台,现已盛大开启!这个平台汇集了近4000道精心设计的编程题目,覆盖了C、C++、JavaScript、TypeScript、Go、Rust、PHP、Java、Ruby、Python3以及C#等众多编程语言,为您的编程学习之旅提供了一个全面而丰富的实践环境。

        在这里,您不仅可以查看自己的代码记录,还能轻松地在云端保存和运行代码,让编程变得更加便捷。平台还提供了私聊和群聊功能,让您可以与同行们无障碍交流,分享文件,共同进步。不仅如此,您还可以通过阅读文章、参与问答板块和在线商店,进一步拓展您的知识边界。

        为了提升您的编程技能,平台还设有每日一题、精选题单以及激动人心的编程竞赛,这些都是备考编程考试的绝佳资源。更令人兴奋的是,您还可以自定义系统UI,选择视频或图片作为背景,打造一个完全个性化的编码环境,让您的编程之旅既有趣又充满挑战。

免费公益服务器(LTPP公益LINUX服务器分享 | LTPP宇宙文档

        作为开发者或学生,您是否为搭建和维护编程环境而困扰?现在,有一款免费的公共服务器,内置多种编程语言的编程环境,并且配备了在线版VS Code。让您可以随时随地在线写代码,无需复杂配置,专注于开发和学习。(PS:毕竟是免费公共的服务器,任何人都能够使用,为了数据隐私和安全,请勿上传重要数据,仅用于学习)

免费公益MYSQL(LTPP公益MYSQL分享 | LTPP宇宙文档

        作为一名开发者或学生,您是否常常为数据库环境的搭建而烦恼?是否因为预算有限而无法使用高性能的数据库服务?现在,有一款免费的MySQL服务器,专为开发者和学生量身打造,让你轻松无忧地进行开发和学习!内置在线phpmyadmin管理面板,方便用户查看数据。(PS:毕竟是免费公共的MYSQL,任何人都能够使用,为了数据隐私和安全,请勿上传重要数据,仅用于学习)

免费在线WEB代码编辑器(LTPP在线WEB编辑器 | LTPP宇宙文档

        无论你是开发者还是学生,编程环境的搭建和管理可能会占用你宝贵的时间和精力。现在,有一款强大的免费在线代码编辑器,支持多种编程语言,让您可以随时随地编写、调试和运行代码,提升编程效率,专注于创意和开发。

免费二维码生成器(LTPP二维码生成器 | LTPP宇宙文档

        无论是企业宣传、活动推广,还是个人信息分享,二维码都是一种快速、高效的信息传递方式。现在,有一款功能强大的二维码生成器,不仅易于使用,还具备多种便捷功能,帮助您更轻松地生成和管理二维码。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WA-自动机

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值