Reward

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

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

``` 1 /*@coder 龚细军*/
2 #include<cstdio>
3 #include<cstring>
4 #include<cstdlib>
5 #include<queue>
6 #include<vector>  //动态的二维数组
7 #include<iostream>
8 using namespace std;
9 const int maxn=10002;
10 int n,m;
11 int cnt[maxn],outd[maxn];
12 vector< vector<int> >map(maxn);
13 void tp_sort(int tol)
14 {
15     int i;
16     queue<int>st;
17     for(i=1;i<=n;i++)
18     {
19         if(!outd[i])
20         {
21             outd[i]--;
22             st.push(i);
23             break;
24         }
25     }
26     //环如何消除
27     while(!st.empty())
28     {
29         int temp=st.front();
30         vector<int>::iterator it;
31         for(it=map[temp].begin();it!=map[temp].end();it++)
32         {
33             outd[*it]--;
34             if(cnt[*it]<=cnt[temp])
35                 cnt[*it]=cnt[temp]+1;
36         }
37         st.pop();
38         for(i=1;i<=n;i++)
39         {
40          if(!outd[i])
41          {
42             outd[i]--;
43             st.push(i);
44             break;
45          }
46         }
47     }
48     for(i=1;i<=n;i++)
49     {
50         if(outd[i]!=-1)
51         {
52             puts("-1");
53             return ;
54         }
55     }
56         int ans=0;
57         for(i=1;i<=n;i++)
58         {
59             ans+=cnt[i];
60         }
61         if(ans)
62         printf("%d\n",n*888+ans);
63         else
64             puts("-1");
65
66 }
67
68 int main()
69 {
70     int a,b,i;
71     while(scanf("%d%d",&n,&m)!=EOF)
72     {
73         for(i=1;i<=n;i++) map[i].clear();
74         memset(outd,0,sizeof(outd));
75         memset(cnt,0,sizeof(cnt));
76         i=1;
77         while(m--)
78         {
79             scanf("%d%d",&a,&b);
80             map[b].push_back(a);
81             outd[a]++;    /*out++*/
82         }
83
84         tp_sort(i);
85     }
86     return 0;
87 }```

0 条评论

相关文章

Grade Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (J...

33290

2014---多校训练一（A Couple doubi）

Couple doubi Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768...

34640

poj----Ubiquitous Religions

Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submi...

37760

HDU2837 Calculation(扩展欧拉定理)

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe...

9620

HDUOJ 1099——Lottery

Lottery Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J...

37070

What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Lim...

401140

HDUOJ------Daydream字符查找-并求其始末位置

2013-07-17  10:50:38 Daydream Time Limit: 2000/1000 MS (Java/Others)    Memory L...

29360

HDU 1004 Let the Balloon Rise【STL<map>】

Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 655...

30840

40450

HDUOJ----1003 Max Sum

Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J...

25560