杭电2647--Reward(反向拓扑)

解决一个工厂老板如何在满足员工提出的相对金额要求下,使用最少的资金进行奖金分配的问题。采用反向拓扑排序的方法,并确保每位员工的奖金不低于888。

Reward

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5863    Accepted Submission(s): 1797


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 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 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 should 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' demands ,print -1.
 

 

Sample Input
2 1
1 2
2 2
1 2
2 1
 

 

Sample Output
1777
-1
 

 

Author
dandelion
 

 

Source
 

 

Recommend
yifenfei   |   We have carefully selected several similar problems for you:   1811  1142  2729  1548  3339 
WA了好多次, 题目意思是:如果一个人在一个人之后领奖金, 他会比前一个人领的多。所以说这个人之后可能会同时存在两个人领的钱数一样多。 反向拓扑。数据较多,只能用邻接表。
 1 #include <queue>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <iostream>
 5 using namespace std;
 6 int indegree[10010], mon[10010]; 
 7 int n, m; 
 8 struct Edge
 9 {
10     int from, to, next;
11 } edge[20020];
12 int head[10010], cnt; 
13 void Add(int a, int b)
14 {
15     Edge E = {a, b, head[a]}; 
16     edge[cnt] = E;
17     head[a] = cnt++;
18 } 
19 int sum;
20 void Tsort()
21 {
22     sum = 0;
23     queue<int> q;
24     for(int i = 1; i <= n; i++){
25         if(!indegree[i])
26             q.push(i);
27     mon[i] = 888;
28     }
29     while(!q.empty())
30     {
31         int u = q.front();
32         q.pop(); indegree[u]--;  sum++;
33         for(int i = head[u]; i != -1; i = edge[i].next)
34         {
35             if(!--indegree[edge[i].to])
36                 mon[edge[i].to] = mon[u] + 1, q.push(edge[i].to); 
37         }
38     }
39     int total = 0;
40     if(sum == n)
41     {
42         for(int i = 1; i <= n; i++)
43             total += mon[i];
44         printf("%d\n", total);
45     }
46     else
47         printf("-1\n"); 
48 }
49 int main()
50 {
51     while(~scanf("%d %d", &n, &m))
52     {
53         cnt = 0;
54         memset(head, -1, sizeof(head));
55         memset(indegree, 0, sizeof(indegree));
56         for(int i = 1; i <= m; i++)
57         {
58             int a, b;
59             scanf("%d %d", &a, &b);
60             indegree[a]++;
61             Add(b, a);    
62         } 
63         Tsort();
64     } 
65     return 0;    
66 } 

 

转载于:https://www.cnblogs.com/soTired/p/4743928.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值