HDU 2647 reward (topological sorting layer)

Source: Internet
Author: User
Reward

Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 3815 accepted submission (s): 1162


Problem 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 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.

 

Inputone 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.

 

Outputfor every case, print the least money Dandelion's uncle needs to distribute. If it's impossible to fulfill all the works's demands, print-1.

 

Sample input2 11 22 21 22 1

 

Sample Output1777-1

 

 

That is, the boss wants to send welfare to employees. If each person's contribution is different, the boss will send welfare based on the contribution, but the boss wants the least welfare, the basic welfare of each person is 888. If a contributes more than B, we will send 889 to.

Solution: If I use an adjacent matrix, I think it must be out of memory. So I chose to use an adjacent table to solve this problem. Because we need to compare people who are bigger than others and make statistics, we will build graphs in the opposite direction, so that we can count people at every level. This is still done once, because a person may contribute more than many people, so we have to choose him more than the largest one among the many people, hey.

 

Paste the Code:

 

# Include <stdio. h> # include <stdlib. h> # include <string. h> # define maxn 10005 struct arcnode {int to; struct arcnode * Next;}; struct arcnode * list [maxn]; int counting [maxn], indegree [maxn]; int mark; void topological (int n) {int Top =-1; struct arcnode * temp; For (INT I = 1; I <= N; I ++) // Add a point with a degree of 0 to the stack {If (0 = indegree [I]) {indegree [I] = top; Top = I ;}} for (INT I = 1; I <= N; I ++) {If (Top =-1) // you can determine whether a ring is formed. {Mark = 1; break;} else {Int J = top; Top = indegree [Top]; temp = list [J]; while (null! = Temp) {int K = temp-> to; counting [k] = counting [k]> counting [J] + 1? Counting [k]: Counting [J] + 1; // obtain the largest contribution if (-- indegree [k] = 0) {indegree [k] = top; top = K;} temp = temp-> next ;}}} int main () {int n, m, U, V, num; struct arcnode * temp; while (scanf ("% d", & N, & M )! = EOF) {memset (counting, 0, sizeof (counting); memset (indegree, 0, sizeof (indegree); memset (list, 0, sizeof (list )); mark = 0; num = 0; while (M --) {scanf ("% d", & U, & V); // U indicates the starting point, V indicates the end point indegree [u] ++; // reverse graph creation, recording the inbound degree of each vertex temp = (struct arcnode *) malloc (sizeof (arcnode )); temp-> to = u; temp-> next = NULL; If (list [v] = NULL) list [v] = temp; else {temp-> next = list [v]; list [v] = temp ;}} topological (n); If (Mark) printf ("-1 \ n "); else {for (INT I = 1; I <= N; I ++) // accumulate the value of each vertex num + = counting [I]; num = num + 888 * n; printf ("% d \ n", num) ;}} return 0 ;}

 

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.