Rewardtime limit:2000/1000ms (java/other) Memory limit:32768/32768k (Java/other) total submission (s): 7 Accepted S Ubmission (s): 3Problem descriptiondandelion ' s uncle is a boss of a factory. As the Spring Festival is coming, he wants to distribute rewards to his workers. Now he had a trouble about what to distribute the rewards.
The workers would compare their rewards, and some one may has demands of the distributing of rewards, just like a ' s reward Should more than B ' S.dandelion's unclue wants to fulfill all the demands, of course, he wants to use the least money. Every work ' s reward'll be at least 888, because it's a lucky number.
Inputone line with a integers n and m, stands for the number of works and the number of demands. (n<=10000,m<=20000) then M-lines, each line contains the integers a and B, stands for a ' s reward should is more than B ' s.
Outputfor every case, print the least money dandelion ' s uncle needs to distribute. If it ' s impossible to fulfill all the works ' demands, print-1.
Sample Input2 1 1 2 2 2 1 2 2 1
Sample output1777-1 solution: Wrong N times to solve with the help of the great God; there are two mistakes: one: I did not understand the Qing test instructions; The problem is that a is bigger than B; the second: I only judge the first time looping, not consider the situation of the middle ring; should be added a temp count if temp==n ; proof that there is no looping; my idea is to record the number of the current layer each time, each pop-up one by one; add one per layer value value; Code:
1#include <stdio.h>2#include <string.h>3#include <queue>4 using namespacestd;5 Const intmaxn=10010;6 structnode{7 intTo,next;8 };9Node edg[maxn*2];Ten intHEAD[MAXN]; One intQUE[MAXN]; Aqueue<int>DL; - intMoney,n,m,flot; - voidTopu () { the inttemp; - for(intI=1; i<=n;i++){ - if(!Que[i]) { - Dl.push (i); + } - } + intValue=888; Atemp=0; at while(!Dl.empty ()) { - intt=dl.size (); - while(t--){ - intk=Dl.front (); -money+=value; - Dl.pop (); intemp++; - for(intj=head[k];j!=-1; j=Edg[j].next) { toque[edg[j].to]--; + if(!que[edg[j].to]) Dl.push (edg[j].to); - } the } *value++; $ }Panax Notoginseng if(temp==N) -printf"%d\n", money); the ElsePuts"-1"); + } A voidInitial () { thememset (head,-1,sizeof(head)); +memset (que,0,sizeof(que)); - while(!dl.empty ()) Dl.pop (); $money=0; flot=1; $ } - intMain () { - intb; the while(~SCANF ("%d%d",&n,&M)) { - initial ();Wuyi for(intI=0; i<m;i++){ thescanf"%d%d",&a,&b); -edg[i].to=A; Wuedg[i].next=Head[b]; -head[b]=i; Aboutque[a]++; $ } - Topu (); - } - return 0; A}
Reward (topological structure + adjacency table + queue)