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. www

    dddd int vec_rotate(char *vec,int rotdist, int length) { int i,j,k,times; char t; times = gcd(rotdis ...

  2. 数值运算内建函数(core python programming 2nd edition 5.6.2)

    数值运算内建函数 函数  功能 abs(num) 返回 num 的绝对值 coerce(num1, num2) 将num1和num2转换为同一类型,然后以一个元组的形式返回. divmod(num1, ...

  3. Word2Vec之Deep Learning in NLP (一)词向量和语言模型

    转自licstar,真心觉得不错,可惜自己有些东西没有看懂 这篇博客是我看了半年的论文后,自己对 Deep Learning 在 NLP 领域中应用的理解和总结,在此分享.其中必然有局限性,欢迎各种交 ...

  4. OpenSSl 加密解密 示例(终于有编程实践了)

    OPenSSl的加密主要有三个重要的函数.看懂下面的代码就基本上知道该如何使用openssL来加密了. 不过注意,要先将libssl.so.1.0和libcrypto.so.1.0文件复制到执行的文件 ...

  5. PowerShell_零基础自学课程_2_Powershell与Cmd以及Unix/Linux Shell

    上篇文章我说道,windows为了改变用户对其console界面的诟病,于是就从windows   vista开始,计划要改变这种局面,于是就有 了Powershell的出现. 1.兼容shell命令 ...

  6. smartassembly 使用指南

    原文 http://www.cnblogs.com/hsapphire/archive/2010/09/21/1832758.html smartassembly 提供了一种用于优化和混淆你的 .ne ...

  7. JS代码的window.location属性详解

    转载:http://www.5icool.org/a/201105/a564.html 如果你稍微懂一些JS代码,一般都会知道 window.location.href 这个属性.并且用该属性获取页面 ...

  8. Android_Studio 及SDK下载

    Android Studio includes all the tools you need to build apps for Android. DOWNLOAD ANDROID STUDIO 2. ...

  9. Encode and Decode Strings 解答

    Question Design an algorithm to encode a list of strings to a string. The encoded string is then sen ...

  10. HashMap Java Doc

    原文 public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneab ...