hdu 2647 Reward (topsort)

Posted getcharzp

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu 2647 Reward (topsort)相关的知识,希望对你有一定的参考价值。

Reward
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 13132    Accepted Submission(s): 4199

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

 

CC++:

 1 #include <map>
 2 #include <queue>
 3 #include <cmath>
 4 #include <vector>
 5 #include <string>
 6 #include <cstdio>
 7 #include <cstring>
 8 #include <climits>
 9 #include <iostream>
10 #include <algorithm>
11 #define INF 0xffffff
12 using namespace std;
13 
14 const int my_max = 20010;
15 int n, m, a, b, my_cnt, my_indeg[my_max];
16 vector <int> my_G[my_max];
17 
18 int topsort()
19 {
20     my_cnt = 0;
21     int my_ans = 0, t = 887;
22     queue <int> Q;
23     while (t ++)
24     {
25         int flag = -1, my_temp = 0;
26         for (int i = 1; i <= n; ++ i)
27             if (my_indeg[i] == 0)
28             {
29                 my_indeg[i] = -1;
30                 flag = 1;
31                 Q.push(i);
32                 my_cnt ++;
33                 my_temp ++;
34             }
35 
36         if (flag == -1) break;
37         my_ans += my_temp * t;
38 
39         while (my_temp --)
40         {
41             int my_now = Q.front();
42             for (int i = 0; i < my_G[my_now].size(); ++ i)
43                 my_indeg[my_G[my_now][i]] --;
44             my_G[my_now].clear();
45             Q.pop();
46         }
47     }
48     if (my_cnt == n)
49         return my_ans;
50 
51     for (int i = 1; i <= n; ++ i)
52         my_G[i].clear();
53     return -1;
54 }
55 
56 int main()
57 {
58     while (~scanf("%d%d", &n, &m))
59     {
60         memset(my_indeg, 0, sizeof(my_indeg));
61         while (m --)
62         {
63             scanf("%d%d", &a, &b);
64             ++ my_indeg[a];
65             my_G[b].push_back(a);
66         }
67 
68         printf("%d
", topsort());
69     }
70     return 0;
71 }

 




















以上是关于hdu 2647 Reward (topsort)的主要内容,如果未能解决你的问题,请参考以下文章

题解报告:hdu 2647 Reward(拓扑排序)

Reward HDU - 2647

hdu 2647 Reward - 拓扑排序

HDU 2647 Reward

HDU2647Reward(拓扑排序)

hdu 2647 Reward(拓扑排序+反图)