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;
    }
     
     
  • 相关阅读:
    一个用css写出来的下拉菜单
    oracle创建新的用户 创建序列 并生成自动自增
    Ubuntu 16.04下安装网络流量分析工具 Wireshark
    Ubuntu16.04安装PostgreSQL并使用pgadmin3管理数据库_图文详解
    http协议无状态中的 "状态" 到底指的是什么?!
    Struts2使用流程
    hibernate创建一对多映射关系
    hibernate中创建一对一映射关系
    利用Hibernate进行数据库的增删改查
    Hibernate的简单流程
  • 原文地址:https://www.cnblogs.com/Accepting/p/11314515.html
Copyright © 2011-2022 走看看