HDU_2647_Reward (topological sorting)

Source: Internet
Author: User

HDU_2647_Reward (topological sorting)
RewardTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 4746 Accepted Submission (s): 1448



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 shoshould 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 shoshould 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 'demanders, print-1.
Sample Input

2 11 22 21 22 1

Sample Output
1777-1

 

Question: The boss will give a year-end bonus to n employees and then give m requirements (a, B), which means a has more bonuses than B, the boss decides that each employee can get at least 888 Yuan, and now the boss must pay at least the total amount of bonus.

If not, output-1.

Analysis: Topology Sorting Problem. If the total number is less than the I-th individual's bonus, the I-th individual's bonus is 888 + sum. Note that the data for this question is relatively large. store it in an adjacent table.

Question link: http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 2647

Code List:

(1) vector implementation

 

#include
# Include # Include # Include # Include # Include # Include # Include # Include # Include # Include using namespace std; typedef unsigned int uint; typedef long ll; typedef unsigned long ull; const int maxv = 10000 + 5; int n, m; int p, q; bool judge; int save [maxv]; int degree [maxv]; vector Graph [maxv]; void init () {memset (degree, 0, sizeof (degree); for (int I = 0; I <= n; I ++) graph [I]. clear () ;}int topSort () {int cnt = 0; int sum = 0; int q = 888; while (cnt

(2) Static adjacent table (array)

 

#include
# Include # Include # Include # Include # Include # Include # Include # Include # Include # Include using namespace std; typedef unsigned int uint; typedef long ll; typedef unsigned long ull; const int maxv = 10000 + 5; const int maxn = 20000 + 5; struct Edge {int to, next;} graph [maxn]; int n, m; int p, q; int index; bool judge; int save [maxv]; int head [maxn]; int degree [maxv]; void init () {index = 1; memset (head, 0, sizeof (head); memset (degree, 0, sizeof (degree);} void add (int u, int v) {graph [index]. to = v; graph [index]. next = head [u]; head [u] = index ++;} int topSort () {int cnt = 0; int sum = 0; int q = 888; while (cnt

 

 

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.