HDU 2647 Reward

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647

Reward

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


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
 


~~~基础拓扑排序

题目大意:老板要发奖金,每个员工至少得到888元,现在了解到有些员工必须比其他员工的得到的奖金要多,问老板至少要准备多少奖金。

题目思路:拓扑排序层次查找入度为0的员工,t[]数组记录了每个层次入度为0的员工,当最后找不到了时判断已经找到的人数是否正好n个,如果是则输出结果,反之输出-1。

这里用到了vector动态数组作为记录每个员工的邻接表,节省内存。

注意的情况:对于输入的 a b,是表示a的奖金要比b的奖金高,所以a的入度+1,并将a加入b的邻接表中  。


#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
using namespace std;
#define N 10010
int in[N],t[N];
vector<int>v[N];
void topo(int n){
    int tot=0,ans=888*n,p=0,ok=1;
    while(1){
        int k=0;
        for(int i=1;i<=n;i++){
            if(in[i]==0){
                in[i]--;
                t[k++]=i;
            }
        }
        if(k==0){
            if(tot!=n) ok=0;
            break;
        }
        tot+=k;
        ans+=k*p;
        p++;
        for(int i=0;i<k;i++){
            for(int j=0;j<v[t[i]].size();j++){
                int x=v[t[i]][j];
                in[x]--;
            }
        }
    }
    if(ok) printf("%d\n",ans);
    else printf("-1\n");
}

int main(){
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF){
        memset(in,0,sizeof(in));
        for(int i=0;i<N;i++)
             v[i].clear();
        int a,b,ok;
        for(int i=0;i<m;i++){

            ok=1;
            scanf("%d%d",&a,&b);
            for(int j=0;j<v[b].size();j++)
                if(v[b][j]==a){
                    ok=0;
                    break;
                }
            if(ok){
                in[a]++;
                v[b].push_back(a);
            }
        }
        topo(n);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值