码迷,mamicode.com
首页 > 系统相关 > 详细

hdu1150 Machine Schedule

时间:2015-09-26 10:35:34      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:

Machine Schedule

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


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
 
题意:要完成k个任务,每个任务可以用某种模式下的A机器,或者某种模式下的B机器完成。花费为模式转换的次数。求挖成任务的最小模式转换次数。
题解:边作为某一个任务,边连接的点是可以完成该任务的机器模式,那么题目就转化为了最小点覆盖集合的问题。
//注意:1.开始的模式是0  2.hungary函数用之前要先调用init,而且调用的时间是加边之前 3.加入的边不考虑连接0状态的,因为一开始是0状态 4.加入的是有向边。(A->B)
代码:
#include <cstdio>
#include <cmath>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <queue>
#include <map>
#include <stack>
using namespace std;
#define test freopen("in.txt","r",stdin)
#define pt(u,p) cout<<u<<"="<<p<<endl;
#define pb(u) push_back(u)
#define ptln cout<<endl;
#define ptarr(i,a,s,f) for(int i=s;i<=f;i++) {if(i!=f) cout<<a[i]<<" ";else cout<<a[i]<<endl;}
#define REP(i,s,f) for(int i=s;i<=f;i++)
#define RREP(i,s,f) for(int i=s;i>=f;i--)
#define COVER(a,v) memset(a,v,sizeof(a))
#define IOS ios_base::sync_with_stdio()
typedef long long ll;

const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
const int maxn=400;
const int maxm=4000;
int n,m,k,kase;
struct Edge{
    int to,next;
}edge[maxm];
int head[maxn],tot;
void init()
{
    tot=0;
    memset(head,-1,sizeof(head));
}
void addedge(int u,int v)
{
    edge[tot].to=v;edge[tot].next=head[u];
    head[u]=tot++;
}
int linker[maxn];
bool used[maxn];
bool dfs(int u)
{
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].to;
        if(!used[v])
        {
            used[v]=true;
            if(linker[v]==-1||dfs(linker[v]))
            {
                linker[v]=u;
                return true;
            }
        }
    }
    return false;
}
int hungary()
{
    int res=0;
    memset(linker,-1,sizeof(linker));
    for(int u=0;u<n;u++)
    {
        memset(used,false,sizeof(used));
        int ret=dfs(u);
        if(ret) res++;
    }
    return res;
}


int main()
{
    while(1)
    {
        cin>>n;
        if(!n)break;
        cin>>m>>k;
        init();
        for(int i=0;i<k;i++)
        {
            int u,v1,v2;
            cin>>u>>v1>>v2;
            if(v1>0&&v2>0) addedge(v1,v2);
        }
        cout<<hungary()<<endl;
    }
    return 0;
}

 

hdu1150 Machine Schedule

标签:

原文地址:http://www.cnblogs.com/diang/p/4840346.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!