Luogu P1113 Chores "topological sort" by cellur925

Source: Internet
Author: User

Topic Portal

This problem we know is a topological sort, however, in how to transform the problem took a lot of work, one hours after the last still helpless to see the QWQ.

Obviously we can get a DAG for each task, from his leading task to the one where he even has a side. The topology is sorted on this DAG, and the cost of updating to the task J is Max (F[j],f[i]+val[j]). When we find a point with a degree of 0, we can use it to update the final answer.

Code

1#include <cstdio>2#include <algorithm>3#include <queue>4 5 using namespacestd;6 7 intn,ans,tot,cnt;8 intval[10090],du[10090],head[10090],f[10090],cdu[10090];9 structnode{Ten     intnext,to; One}edge[200900]; A  - voidAddintXinty) - { theedge[++tot].next=Head[x]; -head[x]=tot; -edge[tot].to=y; - } +  - voidtopo () + { Aqueue<int>Q; at      for(intI=1; i<=n;i++) -         if(du[i]==0) Q.push (i), f[i]=Val[i]; -      while(!q.empty ()) -     { -         intx=Q.front (); Q.pop (); -          for(intI=head[x];i;i=edge[i].next) in         { -             inty=edge[i].to; toF[y]=max (f[y],f[x]+val[y]); +             if(--du[y]==0) -             { the                 if(!cdu[y]) ans=Max (ans,f[y]); *                 ElseQ.push (y); $             }Panax Notoginseng         } -     } the } +  A intMain () the { +scanf"%d",&n); -      for(intI=1; i<=n;i++) $     { $         intopt=0, y=0; -scanf"%d%d",&opt,&val[i]); -          while(SCANF ("%d", &y) &&y!=0) Add (opt,y), du[y]++,cdu[opt]++; the     } - topo ();Wuyiprintf"%d", ans); the     return 0; -}
View Code

Details: The size of the open array is very fastidious, the size of the adjacency table is not control, began to re a point.

Luogu P1113 Chores "topological sort" by cellur925

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.