Reward

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

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
-1
-1777
 
 
 
题意:
       让人恶心的不能在恶心的题。。只想说出题作者你太很了。
该题有几个陷阱需要注意:
一:不能在用简单的拓扑了,因为此时显然超内存。只能用到邻接表。
二:注意多劳多得,可能前面的一个人后面跟着好几个,即,后面那几个人的工资是一样的(本人表示被坑了一个上午)
    最后题意简单,就是叫你判读是否有环。
   详细过程就自己看代码吧,不懂得欢迎提问。共同进步。
 
 
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <queue>
using namespace std; const int N = 10005;
struct edge
{
int w;
edge *next;
}*e[N]; int cnt;
int deg[N], money[N];
int n, m; void init() //初始化
{
cnt = 0;
for(int i = 0; i <= n; i++)
{
e[i] = NULL;
money[i] = 888;
deg[i] = 0;
}
} void add(int x, int y) //建边
{
edge *p = (edge *)malloc(sizeof(edge));
p->w = y;
p->next = e[x];
e[x] = p;
} int main()
{
int i, x, y, k, u, num, sum;
while(scanf("%d %d", &n, &m) != EOF)
{
init();
sum = 0;
num = n;
for(i = 1; i <= m; i++)
{
scanf("%d %d", &x, &y);
add(y, x); //建边
deg[x]++; //度数++
}
queue<int> Q;
for(i = 1; i <= n; i++)
{
if(deg[i] == 0) Q.push(i);
}
while(!Q.empty())
{
k = Q.front();
Q.pop();
num--;
for(edge *p = e[k]; p; p = p->next) //链表代替矩阵
{
u = p->w;
if(--deg[u] == 0) //和k连接的点度数--,若为0,入队列
{
money[u] = money[k] + 1; //钱比连接点k的钱多1
Q.push(u);
}
}
}
if(num > 1) printf("-1\n"); //入队列次数少于n,证明有环
else
{
for(i = 1; i <= n; i++)
{
sum += money[i];
}
printf("%d\n", sum);
}
} return 0;
}

 
 
 

Reward HDU的更多相关文章

  1. 逆拓扑排序 Reward HDU - 2647

    Reward HDU - 2647 题意:每个人的起始金额是888,有些人觉得自己做的比另一个人好所以应该多得一些钱,问最少需要花多少钱,如果不能满足所有员工的要求,输出 -1 样例1: 2 1 1 ...

  2. (回文串 )Best Reward -- hdu -- 3613

    http://acm.hdu.edu.cn/showproblem.php?pid=3613 Best Reward Time Limit: 2000/1000 MS (Java/Others)    ...

  3. Reward HDU - 2647

    传送门     Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to dis ...

  4. Best Reward HDU 3613(回文子串Manacher)

    题目大意:有一个串(全部由小写字母组成),现在要把它分成两部分,如果分开后的部分是回文串就计算出来它的价值总和,如果不是回文的那么价值就是0,最多能得到的最大价值.   分析:首先的明白这个最大价值有 ...

  5. Best Reward HDU - 3613(马拉车+枚举+前缀和)

    题意: 给你一串字符串,每个字符都有一个权值,要求把这个字符串在某点分开,使之成为两个单独的字符串 如果这两个子串某一个是回文串,则权值为那一个串所有的字符权值和 若不是回文串,则权值为0 解析: 先 ...

  6. 2019 HZNU Winter Training Day 14 Comprehensive Training

    A - Choosing Capital for Treeland CodeForces - 219D 题意:有一颗单向边的树,要选取一个结点作为首都.要求是这个结点到其它结点,总共需要翻转的路径数量 ...

  7. POJ 3376 Finding Palindromes EX-KMP+字典树

    题意: 给你n个串串,每个串串可以选择和n个字符串拼接(可以自己和自己拼接),问有多少个拼接后的字符串是回文. 所有的串串长度不超过2e6: 题解: 这题由于是在POJ上,所以string也用不了,会 ...

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

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

  9. 扩展KMP --- HDU 3613 Best Reward

    Best Reward Problem's Link:   http://acm.hdu.edu.cn/showproblem.php?pid=3613 Mean: 给你一个字符串,每个字符都有一个权 ...

随机推荐

  1. GacLib使用方法(一)

    GacLib使用方法 这是vczh大神的GacLib库新手入门,为自己做点笔记,详细的信息可以参考网页.下面简单说说怎么在自己的程序中使用GacLib库,本文只是前述网址中新手教程的一点体验,使用的环 ...

  2. tttt

    while(scanf("%d",&n)!=EOF) { res=-1; level(tmp,n,res,1); printf("%d/n",res); ...

  3. mysql 日期时间运算函数(转)

      DAYOFWEEK(date) 返回日期date是星期几(1=星期天,2=星期一,……7=星期六,ODBC标准)mysql> select DAYOFWEEK('1998-02-03'); ...

  4. MySQL中的两个时间函数,用来做两个时间之间的对比

    TIMESTAMPDIFF,(如果当期时间和之前时间的分钟数相比较.大于1天,即等于1:小于1天,则等于0) select TIMESTAMPDIFF(DAY,'2016-11-16 10:13:42 ...

  5. WordPress插件制作教程(二): 编写一个简单的插件

    上一篇说到了如何创建一个插件,我想大家看了之后一定会有所收获,这一篇简单给大家写一个插件样例,让大家有一个基本的印象.这个插件的样例就是当你激活这个插件后会在你的每篇文章中插入一段自己定义好的内容,比 ...

  6. $(function(){})与(function($){....})(jQuery)的区别

    $(function(){}); 全写为 $(docunemt).ready(function(){ }); 意义为在DOM加载完毕后执行ready()方法 (function($){....})(j ...

  7. php爬虫的两种思路

    写php爬虫可能最大的问题就是php脚本执行时间的问题了,对于这个问题,我找到了两种解决方法. 第一种通过代码set_time_limit(0)或者ini_set("max_executio ...

  8. LeetCode_Wildcard Matching

    Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...

  9. android批量文件上传(android批量图片上传)

    项目中多处用到文件批量上传功能,今天正好解决了此问题,在此写出来,以便日后借鉴. 首先,以下架构下的批量文件上传可能会失败或者不会成功:   1.android客户端+springMVC服务端:服务端 ...

  10. 记忆2--记忆的"记"和"忆"

    有时候也会想,我们是如何记住东西的?是如何想起来的?在写这篇文章的时候,想起初中的时候(应当是初二),语文老师检查唐诗背诵,在下面觉得已经能背起来的时候,去向老师背诵的时候,忘记了开头,干急想不起来, ...