Reward
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 7 Accepted Submission(s) : 3
Problem Description
Dandelion‘s uncle is a boss of a factory. As the spring festival is coming , he wants to distribute rewards to his workers. Now he has a trouble about how to distribute the rewards.
The workers will compare their rewards ,and some one may have 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 will be at least 888 , because it‘s a lucky number.
Input
One line with two 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 two integers a and b ,stands for a‘s reward should be more than b‘s.
Output
For 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 Input
2 1 1 2 2 2 1 2 2 1
Sample Output
1777 -1
题解:
错了n次才在大神的帮助下解决;自己犯的错误有两点:
一:没理解清题意;题上说的是a比b大;
二:自己只判断了第一次成环,没考虑中间成环的情况;
应该加个temp计数,如果temp==N;证明没有成环;
我的思路就是每次记录当前层的个数,每次弹出一个就加一个;每进一层value值就加一;
代码:
1 #include<stdio.h> 2 #include<string.h> 3 #include<queue> 4 using namespace std; 5 const int MAXN=10010; 6 struct Node{ 7 int to,next; 8 }; 9 Node edg[MAXN*2]; 10 int head[MAXN]; 11 int que[MAXN]; 12 queue<int>dl; 13 int money,N,M,flot; 14 void topu(){ 15 int temp; 16 for(int i=1;i<=N;i++){ 17 if(!que[i]){ 18 dl.push(i); 19 } 20 } 21 int value=888; 22 temp=0; 23 while(!dl.empty()){ 24 int t=dl.size(); 25 while(t--){ 26 int k=dl.front(); 27 money+=value; 28 dl.pop(); 29 temp++; 30 for(int j=head[k];j!=-1;j=edg[j].next){ 31 que[edg[j].to]--; 32 if(!que[edg[j].to])dl.push(edg[j].to); 33 } 34 } 35 value++; 36 } 37 if(temp==N) 38 printf("%d\n",money); 39 else puts("-1"); 40 } 41 void initial(){ 42 memset(head,-1,sizeof(head)); 43 memset(que,0,sizeof(que)); 44 while(!dl.empty())dl.pop(); 45 money=0;flot=1; 46 } 47 int main(){ 48 int a,b; 49 while(~scanf("%d%d",&N,&M)){ 50 initial(); 51 for(int i=0;i<M;i++){ 52 scanf("%d%d",&a,&b); 53 edg[i].to=a; 54 edg[i].next=head[b]; 55 head[b]=i; 56 que[a]++; 57 } 58 topu(); 59 } 60 return 0; 61 }