Attention Sequence
Reward
Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 4289 accepted submission (s): 1311
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 Input
2 11 22 21 22 1
Sample output
1777-1
# Include <stdio. h> # include <stdlib. h> # include <malloc. h> # include <limits. h> # include <ctype. h> # include <string. h> # include <string> # include <math. h> # include <algorithm> # include <iostream> # include <queue> # include <stack> # include <deque> # include <vector> # include <set> # include <map> using namespace STD; # define maxn 10011int money [maxn]; int head [maxn]; int into [maxn]; struct node {int to; int next;} edge [200 10]; int main () {int n, m; int I, j; while (~ Scanf ("% d", & N, & M) {for (I = 1; I <= N; I ++) {money [I] = 888;} memset (into, 0, sizeof (into); memset (Head,-1, sizeof (head); int K = 0; while (M --) {scanf ("% d", & J, & I); edge [K]. to = J; edge [K]. next = head [I]; head [I] = K ++; into [J] ++;} queue <int> T; for (I = 1; I <= N; I ++) {If (into [I] = 0) {T. push (I) ;}} int num = 0; int ans = 0; while (! T. empty () {int u = T. front (); ans + = money [u]; T. pop (); num ++; For (k = head [u]; k! =-1; k = edge [K]. next) {If (-- into [edge [K]. to] = 0) {// -- Note T. push (edge [K]. to); money [edge [K]. to] = money [u] + 1;} // T. push (edge [K]. to); // money [edge [K]. to] = money [u] + 1 ;}} if (num! = N) {ans =-1;} printf ("% d \ n", ANS);} return 0 ;}
Topological sorting HDU 2647