Reward(拓扑排序)

http://acm.hdu.edu.cn/showproblem.php?pid=2647

Reward

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

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

题解: 这个题可以用dfs做,也可以用拓扑排序,但要注意两点,因为是要求前者比后者的值要大,所以要反向加边,而且在排序的时候,没加入一个点,就要将其后面的价钱值更新,取这个点的价钱值+1和后面点本身的价钱值得最大值。

代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std;
 5 #define N 10005
 6 #define M 20005
 7 int in[N];
 8 int que[N];
 9 int my[N];
10 struct Edge{
11     int to ;
12     int next;
13 }edge[M];
14 int head[N];
15 int Enct;
16 void init()
17 {
18     Enct = 0;
19     memset(head,-1,sizeof(head));
20     memset(my,0,sizeof(my));
21     memset(in,0,sizeof(in));
22 }
23 void add(int from, int to)
24 {
25     edge[Enct].to = to;
26     edge[Enct].next = head[from];
27     head[from] = Enct++;
28 }
29 int c;
30 int t;
31 int n;
32 int tm ;
33 bool tp()
34 {
35     c = 0;
36     t = 1;
37     for(int i = 0 ;i < n ;i++)
38     {
39         if(in[i]==0)
40         {
41             que[c++] = i;
42         }
43     }
44     for(int i = 0 ;i < c ; i++)
45     {
46         int id = que[i];
47         for(int j = head[id] ; j !=-1 ; j = edge[j].next)
48         {
49             int t = my[id];
50             Edge e = edge[j];
51             in[e.to]--;
52             my[e.to] = max(t+1,my[e.to]);//后继大于前驱,每次都要更新
53             if(in[e.to]==0)
54             {
55                 que[c++] = e.to;
56             }
57         }
58     }
59     if(c<n) return false;
60     else return true;
61 }
62 int main()
63 {
64     int m ;
65     while(~scanf("%d%d",&n,&m))
66     {
67         init();
68         for(int i = 0 ;i < m ;i++)
69         {
70             int a , b;
71             scanf("%d%d",&a,&b);
72             add(b,a);//因为是前者比后者多,所以键反向边
73             in[b]++;
74         }
75         if(tp()==false) printf("-1\n");
76         else
77         {
78             int sum = 0 ;
79             for(int i = 1;i <= n ;i++)
80             {
81                 sum+=my[i];
82             }
83             printf("%d\n",888*n+sum);
84         }
85     }
86     return 0;
87 } 
时间: 2024-10-20 14:57:57

Reward(拓扑排序)的相关文章

hdu 2647 Reward (拓扑排序分层)

Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3815    Accepted Submission(s): 1162 Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wa

HDU 2647 Reward(拓扑排序)

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

hdu 2647 Reward 拓扑排序。

Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4599    Accepted Submission(s): 1400 Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he w

ACM: hdu 2647 Reward -拓扑排序

hdu 2647 Reward Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u 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

hdu2647 Reward 拓扑排序

此题的关键在于分层次,最低一层的人的奖金是888,第二层是888+1 …… 分层可以这样实现.建立反向图.在拓扑排序的时候,第一批入度为0的点就处于第一层,第二批处于第二层 …… 由于是逐个遍历入度为0的点,所以怎么实现上面所说的第一批,第二批就需要动点脑. 可以试试下面的测试数据: 4 31 32 34 3 4 31 22 34 3 4 21 23 4 6 31 23 45 6 测试结果依次是: 3555 3556 3554  5331 #include<iostream> #include

HDU 2647 Reward 拓扑排序

Reward Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u 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

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

题目链接:https://vjudge.net/contest/218427#problem/C 题目大意: 老板要给很多员工发奖金, 但是部分员工有个虚伪心态, 认为自己的奖金必须比某些人高才心理平衡: 但是老板很人道, 想满足所有人的要求, 并且很吝啬,想画的钱最少 输入若干个关系 a b a c c b 意味着a 的工资必须比b的工资高 同时a 的工资比c高: c的工资比b高 当出现环的时候输出-1 #include<iostream> #include<cstring> #

HDU 2647 Reward(图论-拓扑排序)

Reward 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 ,a

HDU 2467 Reward(逆拓扑排序)

拓扑排序的变形,逆序建图就好了 Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3951    Accepted Submission(s): 1203 Problem Description Dandelion's uncle is a boss of a factory. As the spring festival