zoukankan      html  css  js  c++  java
  • HDU Machine Schedule (二分图最大匹配)

    题目

    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

    思路

    匈牙利裸题。

    代码实现

    #include<cstdio>
    #include<algorithm>
    #include<vector>
    #include<queue>
    #include<map>
    #include<iostream>
    #include<cstring>
    #include<cmath>
    using namespace std;
    #define rep(i,f_start,f_end) for (int i=f_start;i<=f_end;++i)
    #define per(i,n,a) for (int i=n;i>=a;i--)
    #define MT(x,i) memset(x,i,sizeof(x) )
    #define rev(i,start,end) for (int i=0;i<end;i++)
    #define inf 0x3f3f3f3f
    #define mp(x,y) make_pair(x,y)
    #define lowbit(x) (x&-x)
    #define MOD 1000000007
    #define exp 1e-8
    #define N 1000005 
    #define fi first 
    #define se second
    #define pb push_back
    typedef long long ll;
    typedef pair<int ,int> PII;
    typedef pair<int ,PII> PIII;
    ll gcd (ll a,ll b) {return b?gcd (b,a%b):a; }
    inline int read() {
        char ch=getchar(); int x=0, f=1;
        while(ch<'0'||ch>'9') {
            if(ch=='-') f = -1;
            ch=getchar();
        } 
        while('0'<=ch&&ch<='9') {
            x=x*10+ch-'0';
            ch=getchar();
        }   return x*f;
    }
    
    const int maxn=1100;
    int n,m,k;
    vector <int > G[maxn];
    int from[maxn];
    int vis[maxn];
    
    bool find (int x) {
        rev (i,0,G[x].size ()) {
            if (!vis[G[x][i]]) {
               vis[G[x][i]]=1;
               if (from[G[x][i]]==-1||find (from[G[x][i]])) {
                   from[G[x][i]]=x;
                   return true;
               }
            }
        }
        return false;
    }
    
    int hungry () {
        int ans=0;
        MT (from,-1);
        rep (i,1,n-1) {
            MT (vis,0);
            if (find (i)) ans++;
        }
        return ans;
    }
    
    int main () {
       while (cin>>n&&n) {
           
           rep (i,1,n-1) G[i].clear ();
    
           cin>>m>>k;
           rev (i,0,k) {
               int a,b,c;
               cin>>a>>b>>c;
               if (b&&c) G[b].pb (c);
           }
           int ans=hungry ();
           cout<<ans<<endl;
       }
        return 0; 
    }
    
    
  • 相关阅读:
    Objective C 绘制透明窗口的方法
    在挂载的 NTFS 盘上运行 gdb 会遇到权限问题,导致无法初始化
    使用 Eclipse 打造 操作系统实习 JOS 开发环境
    C# URL 中文编码与解码
    突破教育网的防线,将搜狗浏览器的教育网加速功能全面推向各种浏览器
    linux 截屏工具
    yum install 时 提示某个已安装的库(x86_64)比某个要安装的库(i686) 新导致安装失败
    全方位打造 Eclipse 自定义开发环境
    自动售饮料机的verilog实现
    数字跑表的verilog实现
  • 原文地址:https://www.cnblogs.com/hhlya/p/13441284.html
Copyright © 2011-2022 走看看