zoukankan      html  css  js  c++  java
  • hdu2647:Reward(拓扑排序+贪心)

    http://acm.hdu.edu.cn/showproblem.php?pid=2647

    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

    题意分析:

    老板要给员工发奖金,但是工人之间会比较他们的奖金,有n个人,和m个要求,每个要求代表a的奖金要大于b的,求满足要求的最少钱数,每位员工的奖金最少是888.如果不能满足要求输出-1.

    解题思路:

    先反向建图,拓扑排序,当有要求更高奖金的时候,就把奖金数目加1,直到发完所有人的奖金,如果期间发现有环,则输出-1.

    #include <stdio.h>
    #include <string.h>
    #include <vector>
    #define N 10020
    using namespace std;
    vector<int>e[N];
    int indegree[N], book[N], bookx[N];
    int main()
    {
        int n, m, u, v, i, j, l, sum, num, ans, temp;
        while(scanf("%d%d", &n, &m)!=EOF)
        {
            memset(indegree, 0, sizeof(indegree));
            memset(book, 0, sizeof(book));
            memset(bookx, 0, sizeof(bookx));
            for(i=1; i<=n; i++)
                e[i].clear();
            for(i=0; i<m; i++)
            {
                scanf("%d%d", &u, &v);
                e[v].push_back(u);
                indegree[u]++;
            }
            sum=888;num=0;ans=0;
            while(num<n)
            {
                temp=0;
                for(i=1; i<=n; i++)
                    if(indegree[i]==0 && book[i]==0 && bookx[i]==0)
                    {
                        bookx[i]=1;
                        ans+=sum;
                    }
    
                sum++;
                for(i=1; i<=n; i++)
                {
                    if(indegree[i]==0 && book[i]==0 && bookx[i]==1)
                    {
                        book[i]=1;
                        num++;
                        temp=1;
                        l=e[i].size();
                        for(j=0; j<l; j++)
                        {
                            v=e[i][j];
                            indegree[v]--;
                        }
                    }
                }
                if(temp==0)
                    break;
            }
            if(temp==0)
                printf("-1
    ");
            else
                printf("%d
    ", ans);
        }
        return 0;
    }
    
  • 相关阅读:
    @codeforces
    @总结
    @总结
    @codeforces
    @topcoder
    @codeforces
    @codeforces
    @codeforces
    @codeforces
    @codeforces
  • 原文地址:https://www.cnblogs.com/zyq1758043090/p/11852618.html
Copyright © 2011-2022 走看看