zoukankan      html  css  js  c++  java
  • poj-1325-Machine Schedule

    poj-1325-Machine Schedule

    Machine Schedule
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 15834   Accepted: 6776

    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

    17957182   1325 Accepted 4392K 79MS G++ 882B 2017-12-16 21:03:57

    两台机器,分别有N种和M种状态,对于每一项任务来说都可以由两台机器来完成。所以,现在只需要找出最小的状态数量就行了。

    只需要找Machine 1 的最少转态数量就行了。因为所有的任务都可以由 Machine 1 来完成。(使用贪心,尽可能所有任务由machine1完成,完成不了的交给 machine 2) 

    可以看出是: 二分图匹配。使用匈牙利算法可以解决。

    (开始提交出现 wrong answer, 因为问的是需要改变的最小状态次数,最初状态是 mode_0 状态,所以对于 任务可以在 mode_0 的话,可以跳过不考虑。)

    // poj-1325 
    #include <cstdio> 
    #include <cstring> 
    const int MAXN = 1000 + 10; 
    
    int n, m, k, mp[MAXN][MAXN], vis[MAXN], match[MAXN], len[MAXN]; 
    
    bool dfs(int x){
    	for(int i=0; i<len[x]; ++i){
    		int y = mp[x][i]; 
    		if(vis[y] == 0){
    			vis[y] = 1; 
    			if(match[y] < 0 || dfs(match[y])){
    				match[y] = x; 
    				return true; 
    			} 
    		} 
    	}
    	return false; 
    }
    
    int main(){
    	freopen("in.txt", "r", stdin); 
    
    	int id, x, y, ans; 
    	while(scanf("%d", &n) != EOF){
    		if(n == 0){ break; } 
    		scanf("%d %d", &m, &k); 
    		memset(len, 0, sizeof(len)); 
    		memset(mp, 0, sizeof(mp)); 
    
    		for(int i=0; i<k; ++i){
    			scanf("%d %d %d", &id, &x, &y); 
    			if( x == 0 || y == 0){
    				continue; 
    			}
    			mp[ x ][ len[x] ] = y; 
    			++len[x]; 
    		}
    
    		memset(match, -1, sizeof(match)); 
    		ans = 0; 
    		for(int i=1; i<=n; ++i){
    			memset(vis, 0, sizeof(vis)); 
    			if(dfs(i)){
    				++ans; 
    			}
    		} 
    		printf("%d
    ", ans ); 
    	}
    	return 0; 
    }
    

      

  • 相关阅读:
    a和b互换的2种方式
    spring cloud 方法调用 feign
    spring boot redis 五种类型使用实例
    springboot引入properties文件 yml文件
    Spark-Streaming结合Redis
    Spark-Streaming结合Mysql案例
    Springboot与scala编写第一个web程序
    Springboot工程Mybatis二级缓存配置
    小奇画画
    saf
  • 原文地址:https://www.cnblogs.com/zhang-yd/p/8047677.html
Copyright © 2011-2022 走看看