HDU 2647 Reward(toposort)

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 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

建反向图编号

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<vector>
 4 using namespace std;
 5
 6 const int maxn=10005;
 7
 8 struct Node
 9 {
10     int c;
11     int id;
12 }node[maxn];
13
14 vector<int>G[maxn];
15 bool indegree[maxn];
16 int n,m;
17
18 void init()
19 {
20     for(int i=1;i<=n;i++)
21         G[i].clear();
22     memset(node,0,sizeof(node));
23     memset(indegree,0,sizeof(indegree));
24 }
25
26 bool dfs(int u,int id)
27 {
28     node[u].c=-1;
29     for(int i=0;i<G[u].size();i++)
30         if(node[G[u][i]].c<0)return false;
31     else if(!dfs(G[u][i],id+1))return false;
32         node[u].c=1;
33         node[u].id=max(node[u].id,id);
34     return true;
35 }
36
37 bool toposort()
38 {
39     for(int i=1;i<=n;i++)
40         if(!node[i].c&&!indegree[i])
41         {
42             if(!dfs(i,0))
43             return false;
44         }
45     for(int i=1;i<=n;i++)
46         if(!node[i].c)
47         return false;
48     return true;
49 }
50
51 int main()
52 {
53     int a,b;
54     while(scanf("%d%d",&n,&m)!=EOF)
55     {
56         init();
57         for(int i=0;i<m;i++)
58         {
59              scanf("%d%d",&a,&b);
60              G[b].push_back(a);
61              indegree[a]=true;
62         }
63         bool ans=toposort();
64         if(ans)
65         {
66             int temp=0;
67             for(int i=1;i<=n;i++)
68                 temp+=node[i].id;
69             printf("%d\n",888*n+temp);
70         }
71         else
72             printf("-1\n");
73     }
74     return 0;
75 }
时间: 2024-10-09 05:15:04

HDU 2647 Reward(toposort)的相关文章

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

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

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/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

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

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【拓扑排序】

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

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