Reward

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 7   Accepted Submission(s) : 3
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
 题解:
错了n次才在大神的帮助下解决;自己犯的错误有两点:
一:没理解清题意;题上说的是a比b大;
二:自己只判断了第一次成环,没考虑中间成环的情况;
应该加个temp计数,如果temp==N;证明没有成环;
我的思路就是每次记录当前层的个数,每次弹出一个就加一个;每进一层value值就加一;
代码:

 #include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
const int MAXN=;
struct Node{
int to,next;
};
Node edg[MAXN*];
int head[MAXN];
int que[MAXN];
queue<int>dl;
int money,N,M,flot;
void topu(){
int temp;
for(int i=;i<=N;i++){
if(!que[i]){
dl.push(i);
}
}
int value=;
temp=;
while(!dl.empty()){
int t=dl.size();
while(t--){
int k=dl.front();
money+=value;
dl.pop();
temp++;
for(int j=head[k];j!=-;j=edg[j].next){
que[edg[j].to]--;
if(!que[edg[j].to])dl.push(edg[j].to);
}
}
value++;
}
if(temp==N)
printf("%d\n",money);
else puts("-1");
}
void initial(){
memset(head,-,sizeof(head));
memset(que,,sizeof(que));
while(!dl.empty())dl.pop();
money=;flot=;
}
int main(){
int a,b;
while(~scanf("%d%d",&N,&M)){
initial();
for(int i=;i<M;i++){
scanf("%d%d",&a,&b);
edg[i].to=a;
edg[i].next=head[b];
head[b]=i;
que[a]++;
}
topu();
}
return ;
}

Reward(拓扑结构+邻接表+队列)的更多相关文章

  1. Genealogical tree(拓扑结构+邻接表+优先队列)

    Genealogical tree Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) ...

  2. 确定比赛名次(map+邻接表 邻接表 拓扑结构 队列+邻接表)

    确定比赛名次 Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submis ...

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

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

  4. ACM/ICPC 之 数据结构-邻接表+DP+队列+拓扑排序(TSH OJ-旅行商TSP)

    做这道题感觉异常激动,因为在下第一次接触拓扑排序啊= =,而且看了看解释,猛然发现此题可以用DP优化,然后一次A掉所有样例,整个人激动坏了,哇咔咔咔咔咔咔咔~ 咔咔~哎呀,笑岔了- -|| 旅行商(T ...

  5. POJ 2259 - Team Queue - [队列的邻接表]

    题目链接:http://poj.org/problem?id=2259 Queues and Priority Queues are data structures which are known t ...

  6. hdu 2647 (拓扑排序 邻接表建图的模板) Reward

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=2647 老板给员工发工资,每个人的基本工资都是888,然后还有奖金,然后员工之间有矛盾,有的员工希望比某员 ...

  7. 08-图8 How Long Does It Take(25 分)邻接表和队列

    Given the relations of all the activities of a project, you are supposed to find the earliest comple ...

  8. 邻接表的广度优先遍历(java版)

    到 0 的权是 91 到 2 的权是 31 到 3 的权是 61 到 4 的权是 7 2 到 0 的权是 22 到 3 的权是 5 3 到 0 的权是 33 到 4 的权是 1 4 到 2 的权是 2 ...

  9. 06-图1 列出连通集 (25分)(C语言邻接表实现)

    题目地址:https://pta.patest.cn/pta/test/558/exam/4/question/9495 由于边数E<(n*(n-1))/2 所以我选用了邻接表实现,优先队列用循 ...

随机推荐

  1. 闲来瞎扯 -- 在vs2008下编写linux程序

    虽说vim很强大,但是个人感觉器代码提示功能不如visual assist来的强大.如何使用visual assist来实现代码的提示功能呢? 首先说明我的环境 : 宿主机是xp(O(∩_∩)O~还是 ...

  2. Unix/Linux环境C编程入门教程(25) C/C++字符测试那些事儿

    isalnum isalpha isascii iscntrl isdigit isgraph isislower isprint isspace ispunct isupper isxdigit介绍 ...

  3. Error: Linux下 mysql.sock文件丢失被删除解决方法

    在默认情况下,Mysql安装以后会在/tmp目录下生成一个mysql.sock文件,如该文件丢失则Mysql将不能够正常启动,解决方法:使用mysqld_safe 启动即可解决: #basedir:m ...

  4. 设置ListView的item多选

    一,首先定义一个类来关联Adapter和Activity能够记住点击的位置 public class MyViewHoler{ public CheckBox cb; public TextView ...

  5. 【UVA】1449-Dominating Patterns(AC自己主动机)

    AC自己主动机的模板题.须要注意的是,对于每一个字符串,须要利用map将它映射到一个结点上,这样才干按顺序输出结果. 14360841 1449 option=com_onlinejudge& ...

  6. 【机房系统知识小结点系列】之遍历窗体中的控件,判断Text是否为空?

    做机房系统时,几乎每个窗体中都会用到判断界面中的控件是否为空的情景.我们曾经是这样走来的: 第一版: 好处:对窗体界面中的Text等控件,逐一做判断,当用户输入某一项为空的时候,会议弹出框的形式,告诉 ...

  7. Bostonkey Simple calc

    Simple Calc 明显的memcpy栈溢出,是一个静态链接的程序所以没给libc.发现里面有: 参数a1应该为_libc_stack_end的地址了._stack_prot通过rop修改为0x7 ...

  8. 关于SQL 系统自带存储过程的使用 (一)

    关于SQL,一边恐惧一边前行,战战兢兢,如履薄冰. 1.那些Maggie教我的事 因为脚本老是倒不齐全,QA某次跟我要了三次脚本,于是乎求助公司DBA. 利用SQL server本身的查询,找出最近修 ...

  9. Android自定义工具类获取按钮并绑定事件(利用暴力反射和注解)

    Android中为按钮绑定事件的有几种常见方式,你可以在布局文件中为按钮设置id,然后在MainActivity中通过findViewById方法获取按钮对象实例,再通过setOnClickListe ...

  10. oracle查询表信息

    oracle查询表信息(索引,外键,列等) oracle中查询表的信息,包括表名,字段名,字段类型,主键,外键唯一性约束信息,索引信息查询SQL如下: 1.查询出所有的用户表 select * fro ...