"Topology Sort" "Critical path"

Source: Internet
Author: User
Tags in degrees

P1113 Chores https://www.luogu.org/problemnew/show/P1113Title Description

John's farm has a lot of chores to do before milking cows, and every chore takes a certain amount of time to complete. For example: they want to bring cows together, drive them into the barn, wash their breasts and some other work for cows. It is necessary to finish all the chores as early as possible, because there is more time to squeeze more milk. Of course, some chores have to be done in the case of other chores. For example: Only the cows will be driven into the barn to start cleaning the breasts, and not to milk the cows before they wash their breasts. We refer to these as preparations for the completion of this work. At least one chore does not require preparation, and this work, which can be done as early as possible, is marked as chore 1. John has a list of n chores to complete, and the list is in a certain order, and the chore K (k>1) preparation is only possible in chore 1. The k-1.

Write a program from 1 to n read the job description of each chore. Calculates the shortest time that all chores are completed. Of course, chores that are not related to each other can work at the same time, and you can assume that John's farm has enough workers to do any number of tasks at the same time.

Input/output format

Input format:

Line 1th: An integer n, the number of chores that must be completed (3<=n<=10,000);

2nd ~ n+1: A total of n rows, each line has a number of 1 spaces separated by an integer, respectively, said:

* Work Sequence Number (1..N, in the input file is ordered);

* The time required to complete the work Len (1<=len<=100);

* Some must-do preparations, the total number of not more than 100, by a number of 0 end. Some chores do not need to be prepared for a job that only describes a single 0, no extra spaces appear in the entire input file.

Output format:

An integer that represents the minimum time required to complete all chores.

Input/Output sample

Input Sample # #:
71 5 02 2 1 03 3 2 04 6 1 05 1 2 4 06 8 2 4 07 4 3 5 6 0
Sample # # of output:
23
Topic Analysis: Is to seek the critical path, and by the topic is easy to know not to appear ring and other non-solution and only the requirements of the value, do not require the path through the point, so you can directly through a topological sort to solve the "if the critical path requires a reverse topology ordering"
1#include <iostream>2#include <cstdio>3#include <vector>4#include <cstring>5#include <queue>6 using namespacestd;7vector<int>vs[10005];//Record the point of work after the work is completed8queue<int>PQ;9 intqwq[10005];//the time required to complete a jobTen intdist[10005];//earliest time to record work One int  out[10005];//out of A int inch[10005];//in degrees - intans=0; - voidTopu () the { -      while(!pq.empty ()) -     { -         intt=Pq.front ();p q.pop (); +          for(inti =0; i< vs[t].size (); i++) -         { +             inch[vs[t][i]]--; ADist[vs[t][i]]=max (Dist[vs[t][i]],qwq[vs[t][i]]+dist[t]);//earliest time of update point at             if(!inch[Vs[t][i]]) -             { -                 if( out[Vs[t][i]])//Core, out of the 0 o'clock place the point in the queue - Pq.push (Vs[t][i]); -                 Else -Ans=max (Ans,dist[vs[t][i]);//Core, if the output is 0, the final result is updated in             } -         } to     } + } - intMain () the { *     intN; $scanf"%d",&n);Panax Notoginsengmemset (Dist,0,sizeof(Dist)); -      for(inti =1; I <= N; i++) the     { +         intb; Ascanf"%d",&a); thescanf"%d",&Qwq[a]); +         intx; -scanf"%d",&x); $          while(x) $         { -             inch[a]++; -              out[x]++; the Vs[x].push_back (a); -scanf"%d",&x);Wuyi         } the     } -      for(inti =1; I <= N; i++) Wu     { -         if(!inch[i]) { Aboutdist[i]=Qwq[i]; $ Pq.push (i); -         } -     } - Topu (); Acout << ans <<Endl; +     return 0; the}

"Topology Sort" "Critical path"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.