HDUOJ----2647Reward

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 }
View Code

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值