zoukankan      html  css  js  c++  java
  • Reward 杭电 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
    题目大意:老板发工资,基本工资是888,要求前面的人要比后面的人多,问最少能发多少。如果满足不了要求输出-1
    思路:拓扑排序,,先判断有没有环,有环 的话输出-1,一开始我想着前面人的钱比后面人的多,和后面的人比前面的人多结果应该相同。。但是。。wa了N次。
    这个题目有两个坑:首相就是 2个人的工资可能相等,第二个就是必须到着来。因为第一个人的工资为基础工资,,也就是说可能会多个人是基础工资。。所以工资必须降序
    AC代码:
    #include<iostream>
    #include<cstdio>
    #include<queue>
    #include<cstring>
    #include<vector>
    using namespace std;
    typedef long long ll;
    const int N=1E5+7;
    vector<int >ve[N];
    int re[N];
    int in[N];
    int main(){
        int n,m;
        while(~scanf("%d%d",&n,&m)){
            memset(re,0,sizeof(re));
            memset(in,0,sizeof(in));
            queue<int >que;
            
            for(int i=1;i<=m;i++){
                int x,y;
                scanf("%d%d",&x,&y);
                in[x]++;
                ve[y].push_back(x);
            }
            
            for(int i=1;i<=n;i++){
                if(in[i]==0){
                    que.push(i);
                }
            }
            
            int n1=0;
            ll sum=0;
            while(que.size()){
                int xx=que.front();
                que.pop();
                sum+=888+re[xx];
                n1++;
                for(int i=0;i<ve[xx].size();i++){
                    in[ve[xx][i]]--;
                    if(in[ve[xx][i]]==0){
                        re[ve[xx][i]]=re[xx]+1;
                        que.push(ve[xx][i]);
                    }
                }
            }
            if(n1!=n){
                puts("-1");
            }
            else {
                printf("%lld
    ",sum);
            }
            for(int i=1;i<=n;i++){
                ve[i].clear();
            }
        }
        return 0;
    }
     
     
  • 相关阅读:
    1000F.One Ocurrence(可持久化线段树+思维)
    P2184.贪婪大陆(思维+树状数组)
    438D.The Child and Sequence(线段树+取模性质)
    P2894 [USACO08FEB]Hotel G(线段树维护区间子串)
    620E New Year Tree(线段树维护状压)
    P6492 [COCI2010-2011#6] STEP(线段树维护最长子串)
    242E.XOR on segment(线段树维护区间异或)
    1527D.MEX Tree(树上分类讨论+容斥)
    解决for循环中写异步函数,异步函数中输出下标一样问题
    vue拦截器
  • 原文地址:https://www.cnblogs.com/Accepting/p/11314515.html
Copyright © 2011-2022 走看看