luogu1113 clutter (topological sorting)

Source: Internet
Author: User

Title 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 of the preparations that must be completed, not more than 100, are ended by a number of 0. 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 and Output sample input sample #: Copy
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
Output Example # #: Replication
23

Steelyard weight Scales Find what are the questions Ah Qaq this question how to feel with the tree-shaped DP can also water over the appearance _ (: Зゝ∠) _
1#include"bits/stdc++.h"2 using namespacestd;3typedefLong LongLL;4 Const intmax=10004;5 intN,m,ans,inch[MAX], out[Max],w[max],f[max];6 inttot,head[max<<4],adj[max<<4],next[max<<4];7InlineintRead () {8     intan=0, x=1;CharC=GetChar ();9      while(c<'0'|| C>'9') {if(c=='-') x=-1; c=GetChar ();}Ten      while(c>='0'&& c<='9') {an= (an<<3) + (an<<1) +c-'0'; c=GetChar ();} One     returnan*x; A } - voidAddedge (intUintv) { -tot++,inch[v]++;adj[tot]=v,next[tot]=head[u],head[u]=tot; the } - voidTopsort () { -     inti,j; -Queue <int>Q; +      for(i=1; i<=n;i++)if(!inch[i]) f[i]=W[i],q.push (i); -      while(!Q.empty ()) { +         intu=Q.front (); Q.pop (); A          for(i=head[u];i;i=Next[i]) { atF[adj[i]]=max (f[adj[i]],f[u]+W[adj[i]]); -             inch[adj[i]]--; -             if(!inch[Adj[i]]) Q.push (Adj[i]); -         } -     } - } in intMain () { -Freopen ("thing.in","R", stdin); Freopen ("Thing.out","W", stdout); to     inti,j,zt,ans=0; +n=read (); -      for(i=1; i<=n;i++){ theRead (); w[i]=read (); *          while(zt=read ()) Addedge (zt,i); $     }Panax Notoginseng Topsort (); -      for(i=1; i<=n;i++) ans=Max (ans,f[i]); theprintf"%d", ans); +     return 0; A}

luogu1113 clutter (topological sorting)

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.