zoukankan      html  css  js  c++  java
  • POJ 1949 Chores 拓扑排序

    Chores
    Time Limit: 3000MS   Memory Limit: 30000K
    Total Submissions: 4608   Accepted: 2146

    Description

    Farmer John's family pitches in with the chores during milking, doing all the chores as quickly as possible. At FJ's house, some chores cannot be started until others have been completed, e.g., it is impossible to wash the cows until they are in the stalls. 
    Farmer John has a list of N (3 <= N <= 10,000) chores that must be completed. Each chore requires an integer time (1 <= length of time <= 100) to complete and there may be other chores that must be completed before this chore is started. We will call these prerequisite chores. At least one chore has no prerequisite: the very first one, number 1. Farmer John's list of chores is nicely ordered, and chore K (K > 1) can have only chores 1,.K-1 as prerequisites. Write a program that reads a list of chores from 1 to N with associated times and all perquisite chores. Now calculate the shortest time it will take to complete all N chores. Of course, chores that do not depend on each other can be performed simultaneously.

    Input

    * Line 1: One integer, N 
    * Lines 2..N+1: N lines, each with several space-separated integers. Line 2 contains chore 1; line 3 contains chore 2, and so on. Each line contains the length of time to complete the chore, the number of the prerequisites, Pi, (0 <= Pi <= 100), and the Pi prerequisites (range 1..N, of course). 

    Output

    A single line with an integer which is the least amount of time required to perform all the chores. 

    Sample Input

    7
    5 0
    1 1 1
    3 1 2
    6 1 1
    1 2 2 4
    8 2 2 4
    4 3 3 5 6
    

    Sample Output

    23
    

    Hint

    [Here is one task schedule:
             Chore 1 starts at time 0, ends at time 5.
             Chore 2 starts at time 5, ends at time 6.
             Chore 3 starts at time 6, ends at time 9.
             Chore 4 starts at time 5, ends at time 11.
             Chore 5 starts at time 11, ends at time 12.
             Chore 6 starts at time 11, ends at time 19.
             Chore 7 starts at time 19, ends at time 23.
     ]
    代码:
    #include<cstdio>
    #include<cstring>
    #include<vector>
    #include<queue>
    using namespace std ;
    #define MAXN 10010
    vector<int>qe[MAXN];
    int mun[MAXN] , len[MAXN] , add[MAXN];
    int main()
    {
    	int i , j , n , now ;
    	int time , k ;
    
    	while( scanf( "%d" , &n ) != EOF )
    	{ 
    		queue<int>q ;
    		memset( mun , 0  ,sizeof(mun) ) ;
    		memset( add , 0 , sizeof(add) ) ;
    		for( i = 1; i <= n ; i++)
    		{ 
    			qe[i].clear() ;
    			scanf( "%d%d" , &time  , &k ) ;
    			len[i] = time ;
    			while(k--)
    			{
    				scanf( "%d" , &j ) ;
    				mun[j]++ ;     
    				qe[i].push_back( j ) ; 
    			}
    		}
    		for( i = 1; i <= n ;i++)
    			if( mun[i] == 0 )
    			{
    				q.push( i ) ;
    			}
    			while( !q.empty())
    			{  
    				now = q.front( ) ;
    			    q.pop() ;
    				for( i = 0 ; i < qe[now].size() ;i++)
    				{    
    					add[qe[now][i]] = max( add[qe[now][i]] , len[now] ) ; // 先计算前面都完成中所需最长时间
    					if( --mun[qe[now][i]] == 0 )
    					{
    						q.push( qe[now][i] ) ;
    						len[qe[now][i]] += add[qe[now][i]] ;// 然后再对 len 最终要的时间更新
    					}
    				}
    			}
    			int ans = 0 ;
    			for( i = 1 ; i <= n ;i++)
    			{
                   if( ans < len[i] ) 
    				   ans = len[i] ;
    			}
    			printf( "%d\n" , ans ) ;
    	}
    }
    

      

  • 相关阅读:
    netbeans 快捷键
    Netbeans Platform的Lookup 边学边记
    Swing中的并发使用SwingWorker线程模式
    转:ExtJS:tabpanel 多个tab同时渲染问题
    NetBeans Platform Login Tutorial
    检测项目
    C#实现通过程序自动抓取远程Web网页信息
    WebClient类
    string字符串的方法
    WebClient的研究笔记认识WebClient
  • 原文地址:https://www.cnblogs.com/20120125llcai/p/3052450.html
Copyright © 2011-2022 走看看