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