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

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 } 
时间: 2024-10-10 01:26:37

杭电2647--Reward(反向拓扑)的相关文章

杭电 2647 Reward (拓扑排序反着排)

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

HDU 2647 Reward【拓扑排序】

题意:工厂发工资,最低工资是888元,然后比他高一层得人的工资是889,依次类推 因为是从工资低的人推到工资高的人,所以反向建图 然后就是自己写的时候犯的错误,以为工资是后一个人比前一个人高1元,然后就直接判断是否能形成拓扑序列之后,用n*888+(n-1)*n/2来算了 这样不对,是后一层的工资比前一层得工资多1元,用一个数组记录下来钱就可以了 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4

杭电ACM2094——产生冠军~~拓扑排序

题目的意思,如题.很容易明白. 解决的方法就是拓扑排序,就可以很容易的解决了. 每输入一对选手,判断两个选手是否出现过,没有出现过,新建一个头结点,加入到邻接表中,更新结点的入度. 最后判断是否存在一个结点的入度为0,有,则Yes,否则No. 我用的是STL中的list容器来创建的邻接表. 下面的是 AC的代码: #include <iostream> #include <list> #include <cstring> using namespace std; cla

杭电4857--逃生(拓扑排序)

逃生 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2257    Accepted Submission(s): 637 Problem Description 糟糕的事情发生啦,现在大家都忙着逃命.但是逃命的通道很窄,大家只能排成一行. 现在有n个人,从1标号到n.同时有一些奇怪的约束条件,每个都形如:a必须在b之前.同时,社会是不

hdu 2647 Reward(拓扑排序,反着来)

Reward Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 51   Accepted Submission(s) : 21 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Dandelion's uncle is a boss of

hdu 2647 Reward(拓扑排序)

1.反向建图,有利于计算 2.代码: #include<cstdio> #include<cstring> #include<stdlib.h> #define Max(a,b) ((a)>(b)?(a):(b)) using namespace std; typedef struct ArcNode { int adjvex; struct ArcNode * nextarc; } ArcNode; typedef struct VNode { int vert

杭电4324--Triangle LOVE(拓扑排序)

Triangle LOVE Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 3479    Accepted Submission(s): 1350 Problem Description Recently, scientists find that there is love between any of two people. For

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(图论-拓扑排序)

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