Reward

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

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
 
题目重现:

问题描述
蒲公英的叔叔是工厂的老板。 随着春节来临,他想向他的工人分发奖励。 现在他有分享奖励的麻烦。
工作人员会比较他们的回报,有的可能会有分配奖励的要求,就像一个奖励应该超过b's.蒲公英的无礼想要完成所有的要求,当然,他想使用最少的钱。每个工作 奖励至少是888,因为这是一个幸运数字。

思路:
拓扑排序,求出每一层上的节点,对于每一层比上一层的钱数多一,至于无法确定的情况即是出现环的情况、、直接输出-1。
怎样判断是哪一层?!我们进行删边的时候当前节点的入读为0,被删掉后的入度为零的节点一定比当前节点的层数多1.我们用这一层关系来判断、也就是这样ceng[edge[i].to]=ceng[x]+1、、、
代码:
#include<queue>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N  20100
#define money 888
using namespace std;
queue<int>q;
long long anss;
int n,m,x,y,s,in[N],tot,sum,head[N],ans[N];
int read()
{
    ,f=; char ch=getchar();
    ; ch=getchar();}
    +ch-'; ch=getchar();}
    return x*f;
}
struct Edge
{
    int from,to,next;
}edge[N];
int add(int x,int y)
{
    tot++;
    edge[tot].to=y;
    edge[tot].next=head[x];
    head[x]=tot;
}
void begin()
{
    s=,sum=,tot=;anss=;
    memset(,sizeof(in));
    memset(ans,,sizeof(ans));
    memset(head,,sizeof(head));
    memset(edge,,sizeof(edge));
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        begin();
        ;i<=m;i++)
        {
            x=read(),y=read();
            add(y,x),in[x]++;
        }
        ;i<=n;i++)
         ) q.push(i),ans[i]=0;
        while(!q.empty())
        {
            x=q.front(),q.pop(),sum++;
            for(int i=head[x];i;i=edge[i].next)
            {
                int t=edge[i].to;
                in[t]--;
                ) q.push(t),ans[t]=ans[x]+1;
            }
        }
        if(sum!=n) printf("-1\n");
        else
        {
            ;i<=n;i++)
             anss+=money+ans[i];
            printf("%lld\n",anss);
        }
    }
    ;
}

HDU——2647 Reward的更多相关文章

  1. ACM: hdu 2647 Reward -拓扑排序

    hdu 2647 Reward Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Des ...

  2. HDU.2647 Reward(拓扑排序 TopSort)

    HDU.2647 Reward(拓扑排序 TopSort) 题意分析 裸的拓扑排序 详解请移步 算法学习 拓扑排序(TopSort) 这道题有一点变化是要求计算最后的金钱数.最少金钱值是888,最少的 ...

  3. 题解报告:hdu 2647 Reward(拓扑排序)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 Problem Description Dandelion's uncle is a boss ...

  4. hdu 2647 Reward

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2647 Reward Description Dandelion's uncle is a boss o ...

  5. HDU 2647 Reward (拓扑排序)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题意是给你n点m条有向边,叶子点(出度为0)上的值为888,父亲点为888+1,依次计算... ...

  6. HDU 2647 Reward(拓扑排序+判断环+分层)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题目大意:要给n个人发工资,告诉你m个关系,给出m行每行a b,表示b的工资小于a的工资,最低工 ...

  7. HDU 2647 Reward(图论-拓扑排序)

    Reward Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is comin ...

  8. hdu 2647 Reward(拓扑排序,反着来)

    Reward Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submis ...

  9. HDU 2647 Reward 【拓扑排序反向建图+队列】

    题目 Reward Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to d ...

  10. HDU 2647 Reward(拓扑排序,vector实现邻接表)

    Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

随机推荐

  1. 掌握Spark机器学习库-09.3-kmeans算法实现分类

     数据集 iris.data 数据集概览 代码 package org.apache.spark.examples.hust.hml.examplesforml import org.apache.s ...

  2. spark编译错误解决 Error:(52, 75) not found: value TCLIService

    对于2.20版本可能会出现以下问题: spark\sql\hive-thriftserver\src\main\java\org\apache\hive\service\cli\thrift\Thri ...

  3. Node.js——基本服务开启

    标注模式 var http = require('http'); var server = http.createServer(); server.on('request', function (re ...

  4. 关于NSCELL

    作为一个初学者,我一直很弄不明白NSCell的子类,比如,NSButtonCell,NSImageCell及其对应的控件之间的关系.今天,在做一个TableView实现的时候,我终于开始有点悟了——好 ...

  5. Okhttp3发送xml、json、文件的请求方法

    1.引入依赖 <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okht ...

  6. 使用Let's Encrypt 生成免费的ssl证书的详细过程

    参考连接:https://github.com/diafygi/acme-tiny 中文:https://hacpai.com/article/1487899289204 目前我了解可以生成免费证书的 ...

  7. Python 判断是否存在Excel表

    Python 判断是否存在Excel表,无则生成,有则删除重建 import os import xlwt from openpyxl import workbook def sheet_method ...

  8. 「 Luogu P3137 」X 「 USACO16FEB 」 圆形谷仓

    # 题目大意 管理大大给修下 $\text{Markdown}$ 吧,严重影响做题体验啊. 这道题的意思很简单就是给你一个长度是 $n$ 的环,这个环上不均匀的分布着 $n$ 头奶牛.一头奶牛移动要花 ...

  9. 2018湖南省第14届大学生计算机程序设计竞赛 D: 卖萌表情

    Description 已知以下 4 种都是卖萌表情(空白的部分可以是任意字符.竖线是便于展示的分隔符,没有实际意义): ^ ^ | ^ | < | > v | v v | > | ...

  10. 算法竞赛入门经典5.2 STL初步

    1. 排序和检索,学会使用sort排序,以及low_bound函数 Raju and Meena love to play with Marbles. They have got a lot of m ...