Hangzhou Electric 2647 Reward (topological sort reverse row)

Source: Internet
Author: User

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 Input
2 11 22 21 22 1
Sample Output
1777-1

Effect:
The farm mainly pay, but there are certain rules, some people have to pay more than some people, the minimum wage for everyone is 888. Enter two integer n,m, which represents the number of worker numbers 1~n and M rules, the next m line has two integers a, a, a is higher than B, enter the minimum amount of money, If there is no output-1.

Because there is no way to determine the wages of the highest wage is how much, so the reverse sort, so that the minimum wage in front, enter a A, B, the record of a constraint A, the predecessor of 0 of the salary of 888, followed by the cumulative.

1#include <cstdio>2#include <queue>3#include <string.h>4 using namespacestd;5 intn,m,i,j,num[20010],head[20010],mon[20010],sum,ans;6 structStu7 {8     intTo,next;9}st[20010];Ten voidInit () One { Amemset (NUM,0,sizeof(num)); -memset (head,-1,sizeof(head)); -memset (Mon,0,sizeof(Mon)); thesum=0; -ans=0; - } - voidAddintAintb) + { -st[i].to=b; +st[i].next=Head[a]; Ahead[a]=i; atnum[b]++; - } - voidtopo () - { -     inti,j; -queue<int>que; in      while(!que.empty ()) -     { to Que.pop (); +     } -      for(i =1; I <= N; i++) the     { *         if(Num[i] = =0) $         {Panax Notoginsengmon[i]=888; - Que.push (i); the         } +     } A      while(!que.empty ()) the     { +ans++; -m=Que.front (); $ Que.pop (); $sum+=Mon[m]; -          for(i = head[m]; I! =-1; i =st[i].next) -         { the             if(--num[st[i].to] = =0) -             {Wuyi Que.push (st[i].to); themon[st[i].to]=mon[m]+1; -             } Wu         } -     } About     if(ans = =N) $printf"%d\n", sum); -     Else -printf"-1\n"); - } A intMain () + { the     intb; -      while(SCANF ("%d%d", &n,&m)! =EOF) $     { the init (); the          for(i =0; I < m; i++) the         { thescanf"%d%d",&a,&b); - Add (b,a); in         } the topo (); the     } About      the}

Hangzhou Electric 2647 Reward (topological sort reverse row)

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.