HDU 2647 Reverse topology ordering

Source: Internet
Author: User

So that each employee has a level of their own level[i], the higher the level of staff, the higher the salary, in order to make the money as little as possible, so that each level only add a unit of money

Input a B to indicate a level higher than B, then we reverse add edge, so that b->a so in[a]++, and then the topological sort, each time the end of the edge of the levels value is greater than the starting point, then use Max to take a larger value

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <queue>5 6 using namespacestd;7 8 Const intN =10005;9 intN, M, level[n];Ten intFirst[n],inch[N], K; One  A structpath{ -     inty, Next; -}path[n<<2]; the  - voidAdd_edge (intXinty) - { -     inch[y]++; +Path[k].y=y, Path[k].next =First[x]; -FIRST[X] = k++; + } A  at BOOLDAG () - { -Memset (Level,0,sizeof(level)); -     intCNT =0;//count the number of points that can form the most non-circular graphs -queue<int>Q; -      for(inti =1; i<= N; i++) in         if(inch[I] = =0){ - Q.push (i); toLevel[i] =0; +         } -      while(true){ the         if(Q.empty ()) Break; *         intU =Q.front (); $ Q.pop ();Panax Notoginsengcnt++; -          for(inti = First[u]; i!=-1; I=Path[i].next) { the             intv=path[i].y; +             inch[v]--; ALEVEL[V] = max (Level[v], level[u]+1); the             if(inch[v] = =0) + Q.push (v); -         } $     } $     if(cnt<n)return false; -     return true; - } the  - intMain ()Wuyi { the    //freopen ("a.in", "RB", stdin); -     intb; WuMemset (Level,0x3f,sizeof(level)); -      while(~SCANF ("%d%d",&n,&m)) { Aboutmemset (First,-1,sizeof(first)); $Memsetinch,0,sizeof(inch)); -k=0; -  -          for(intI=0; I<m; i++){ Ascanf"%d%d",&a,&b); + Add_edge (b, a); the         } -  $         if(DAG ()) { the             intAns =0; the              for(inti =1; I<=n; i++) theans+=888+Level[i]; theprintf"%d\n", ans); -         } in         ElsePuts"-1"); the     } the     return 0; About}

HDU 2647 Reverse topology ordering

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.