• 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; 
    }
    
    
  • 相关阅读:
    java实现验证码功能
    C# 自动注册OCX方法
    wamp出现You don’t have permission to access/on this server提示
    C# 图像旋转代码
    C# 实现图像快速 水平 垂直 翻转
    C#创建Graphics对象的方法
    winform控件大小改变是防止背景重绘导致的闪烁(转载)
    C#中DataTable中Rows.Add 和 ImportRow 对比
    MongoDb C# 驱动操作示例
    解决c#所有单线程单元(STA)线程都应使用泵式等待基元(如 CoWaitForMultipleHandles),并在运行时间很长的操作过程中定期发送消息。 转载
  • 原文地址:https://www.cnblogs.com/hhlya/p/13441284.html
Copyright © 2020-2023  润新知