Reward
Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 3927 accepted submission (s): 1199
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
Question: There are n employees. Some employees require their bonuses to be higher than others. If they do not, they must be able to meet the requirements of everyone;
Question: we can think of everyone's needs as a number of layers. If there is no need, we will go to the first layer. The bonus will be paid at each layer, in this way, the final bonus amount will be minimized. The reverse topology is required. In addition, this problem encountered a strange phenomenon. At first, I opened a small map array, but when I submitted it, the result was TLE instead of RE, once the array is doubled, the AC becomes available. It seems that the OJ question recognition system is not very accurate.
#include <stdio.h>#include <string.h>#define maxn 10002int ans, queue[maxn];struct Node{ int to, next, val;} map[maxn << 1];struct node{ int first, money, indegree;} head[maxn];bool topoSort(int n){ int i, front = 0, back = 0, u; for(i = 1; i <= n; ++i) if(!head[i].indegree) queue[back++] = i; while(front != back){ u = queue[front++]; ans += head[u].money; for(i = head[u].first; i != 0; i = map[i].next) if(!--head[map[i].to].indegree){ head[map[i].to].money = head[u].money + 1; queue[back++] = map[i].to; } } return back == n;}int main(){ int n, m, a, b, i; while(scanf("%d%d", &n, &m) != EOF){ memset(head, 0, sizeof(head)); for(i = 1; i <= m; ++i){ scanf("%d%d", &a, &b); map[i].to = a; map[i].next = head[b].first; ++head[a].indegree; head[b].first = i; } ans = 888 * n; if(!topoSort(n)) printf("-1\n"); else printf("%d\n", ans); } return 0;}