码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-08-20 01:26:25      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:

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 } 

 

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

标签:

原文地址:http://www.cnblogs.com/fengshun/p/4743928.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!