首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HDUOJ----2647Reward

HDUOJ----2647Reward

作者头像
Gxjun
发布2018-03-22 11:08:28
6140
发布2018-03-22 11:08:28
举报
文章被收录于专栏:mlml

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 }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2013-12-26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Reward
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档