Machine Schedule(poj 1325)

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


不得不说,二分图匹配的题总是隐藏的很好,我一上来看是贪心题

给你两台机器A,B,分别有 n ,m 种模式,做 k 个任务

某个任务只能由 A 的 a[i] 模式 或 B 的 b[i] 模式做,我们建一个 a[i] -> b[i] 的二分图

如图,每条边相当于一个任务

我们先把 a:1 匹配上 b:1,当 a:2 匹配 b:1  时意味着什么?

说明该做任务 a 必须要切换下模式,或是 b 切换(开启)

到了 a: 3 ,同理,要再切换一次

到了 a: 4 ,可以理解为上一步开启了 b: 3 ,所以就直接由它做了

以前我以为匈牙利只是最大边匹配,现在明白了它等同于最小点覆盖(切换次数)>:<

code

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int MX=1001;
int n,m,k,match[MX];
bool vis[MX],f[MX][MX];

bool hungry(int u)
{
    for(int i=1;i<=m;++i) {
        if(f[u][i]) {
            if(vis[i]) continue;
            vis[i]=1;
            if(!match[i] || hungry(match[i])) {
                match[i]=u;
                return 1;
            }
        }
    }
    return 0;
}

int main()
{
    while(scanf("%d",&n)!=EOF && n) {
        memset(f,0,sizeof(f));
        memset(match,0,sizeof(match));
        scanf("%d%d",&m,&k);
        while(k--) {
            int i,u,v;
            scanf("%d%d%d",&i,&u,&v);
            f[u][v]=1;
        }
        int ans=0;
        for(int i=1;i<=n;++i) {
            memset(vis,0,sizeof(vis));
            ans+=hungry(i);
        }
        printf("%d\n",ans);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/qseer/p/9765696.html

时间: 2024-08-02 18:44:56

Machine Schedule(poj 1325)的相关文章

hdu-----(1150)Machine Schedule(最小覆盖点)

Machine Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5817    Accepted Submission(s): 2932 Problem Description As we all know, machine scheduling is a very classical problem in comput

杭电 HDU ACM 1150 Machine Schedule(二分匹配)

Machine Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7056    Accepted Submission(s): 3537 Problem Description As we all know, machine scheduling is a very classical problem in compu

【HDU 1150】Machine Schedule(二分图匹配)

机器的不同模式为点,对于每个job,建两条边 A机器需要的模式<->B机器需要的模式. 问题转化为最小点覆盖,然后用二分图的最小点覆盖==最大匹配,用匈牙利算法解. #include <cstdio> #include <cstring> const int N=105<<1; const int M=1001<<1; struct edge{ int to,next; }e[M]; int head[N],tot; void add(int u

HDU 1150:Machine Schedule(二分匹配,匈牙利算法)

Machine Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5371    Accepted Submission(s): 2658 Problem Description As we all know, machine scheduling is a very classical problem in compu

HDU 1325 Is It A Tree? (POJ 1308)

并查集问题... 这题以前做过-- 以前做过-- 做过-- 过-- 不过重做时候被吭得异常之爽-- 在判断 vis[i]的时候.我记得标准C++是非0 即为真. 而我用C++ 提交的时候 if(vis[i]) 去直接给我WA了. 用G++ 就AC了...然后改成if(vis[i]==1) 交C++ 就AC了. 特瞄的我每次初始化都把 vis[i] 都赋值为 0 了..都能出这种错? 求路过大神明示我的错误. 题意是判断是否是一棵树. 不能存在森林,用并查集合并,每个点的入度不能超过1. 比如 1

HDU 1535 Invitation Cards (POJ 1511)

两次SPFA.求 来 和 回 的最短路之和. 用Dijkstra+邻接矩阵确实好写+方便交换,但是这个有1000000个点,矩阵开不了. d1[]为 1~N 的最短路. 将所有边的 邻点 交换. d2[] 为 1~N 的最短路. 所有相加为 所要答案. 忧伤的是用SPFA  "HDU 1535"  AC了,但是POJ 一样的题 "POJ 1511" 就WA了. 然后强迫症犯了,不停的去测试. 题意中找到一句关键话 :Prices are positive integ

每日一dp(1)——Largest Rectangle in a Histogram(poj 2559)使用单调队列优化

Largest Rectangle in a Histogram 题目大意: 有数个宽为1,长不定的连续方格,求构成的矩形中最大面积 /************************************************************************/ /* 思路1. 当前为n的面积如何与n-1相联系,dp[i][j]=max(dp[i-1][k]) , 0<k<=j 描述:i为方块个数,j为高度 但是此题目的数据对于高度太变态,h,1000000000 ,n,1

Power string(poj 2406)

题目大意,给出一个字符串s,求最大的k,使得s能表示成a^k的形式,如 abab 可以表示成(ab)^2: 方法:首先 先求kmp算法求出next数组:如果 len mod (len-next[len])==0 ,答案就是 len /(len-next[len]),否则答案是1:证明如下: 如果s能表示成 a^k的形式且k>1,k尽可能大,即s可以表示成aaaaaa(k个a):那么next[len]就等于k-1个a的长度:aaaaaaa  aaaaaaa那么 (len-next[len])a的长

(多重背包+记录路径)Charlie&#39;s Change (poj 1787)

http://poj.org/problem?id=1787 描述 Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he often buys coffee at coffee vending machines at motorests. Charlie hates change. That is basically the setup of your next task. Your