Topic Links:
http://acm.hdu.edu.cn/showproblem.php?pid=5638
Test instructions
Give you a DAG diagram, delete the K-bar, so that you can get a dictionary order as small as possible the topological sort
Exercises
The algorithm of topological sorting is changed slightly, if a vertex is less than k, it is added to the priority queue.
K reduced after the queue there will be some points do not meet the <=k, directly kicked out on the good.
Code:
#include <iostream>#include<cstring>#include<cstdio>#include<vector>#include<queue>#include<functional>using namespaceStd;typedefLong LongLL;Const intMAXN =101010;Const intMAXM = Maxn <<1;Const intMoD = 1e9 +7;intN, M, K;vector<int>G[MAXN];intIND[MAXN], USED[MAXN],INQ[MAXN];voidinit () { for(inti =1; I <= N; i++) g[i].clear (); memset (Ind,0,sizeof(Ind)); memset (INQ,0,sizeof(INQ)); memset (Used,0,sizeof(used));}intMain () {intTC; scanf ("%d", &TC); while(tc--) {scanf ("%d%d%d", &n, &m, &k); Init (); for(inti =0; I < m; i++) { intu, v; scanf ("%d%d", &u, &v); G[u].push_back (v); IND[V]++; } priority_queue<int,vector<int>,greater<int> >PQ; for(inti =1; I <= N; i++) { if(Ind[i] <= k) Pq.push (i), inq[i] =1; } Vector<int>ans; while(!Pq.empty ()) { while(Ind[pq.top ()]>k) {inq[pq.top ()]=0; Pq.pop (); } intx =pq.top (); Pq.pop (); INQ[X]=0; K-=Ind[x]; Ans.push_back (x); USED[X]=1; for(inti =0; I < g[x].size (); i++) { intv =G[x][i]; IND[V]--; if(Ind[v] <= k&&!inq[v]&&!Used[v]) {Pq.push (v); INQ[V]=1; }}} LL cnt=0; for(inti =0; I < ans.size (); i++) {CNT+ = (LL) (i +1)*Ans[i]; CNT%=MoD; } printf ("%lld\n", CNT); } return 0;}
HDU 5638 topology sort + Priority queue