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. C++14介绍

    C++14标准是 ISO/IEC 14882:2014 Information technology -- Programming languages -- C++ 的简称[1]  .在标准正式通过之 ...

  2. C语言的本质(21)——预处理之三:其它预处理特性及总结

    C标准规定了几个特殊的宏,在不同的地方使用可以自动展开成不同的值,预编译程序对于在源程序中出现的这些串将用合适的值进行替换.这些宏有下面这些: __FILE__ 展开为当前源文件的文件名,是一个字符串 ...

  3. cf466A Cheap Travel

    A. Cheap Travel time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  4. C#操作XML的完整例子——XmlDocument篇(转载,仅做学习之用)

    原文地址:http://www.cnblogs.com/serenatao/archive/2012/09/05/2672621.html 这是一个用c#控制台程序下,  用XmlDocument 进 ...

  5. windows2003 64位注册码 序列号 激活码

    Windows 2003 R2 64bit Enterprise VOL Edition 企业版 MR78C-GF2CY-KC864-DTG74-VMT73 VPT7T-77D38-KWVW2-2G3 ...

  6. 关于Makefile.am中与Build相关的变量设置 AM_CPPFLAGS

    http://tonybai.com/2010/10/26/about-variables-related-to-building-in-makefile-am/ 关于Makefile.am中与Bui ...

  7. 网易云课堂_C++开发入门到精通_章节2:引用和函数的高级用法

    课时6函数重载 函数重载 在C语言头文件中的extern "C" //常见的C语言头文件格式 #ifndef _FUNC_ #define _FUNC_ #ifdef __cplu ...

  8. 【泛化物品】【HDU1712】【ACboy needs your help】

    ACboy needs your help Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  9. Silverlight学习(二)

    好久没来写博客了,这期间经历了春节,也因为忙于一个项目,所以博客被疏忽了.最近一段时间一直在用silverlight做项目,从来一开始的不熟悉渐渐的开始上手.今天记录一下自己学习prism的一些sam ...

  10. Linux下安装McAfee防病毒软件(企业版本)

    最近公司接一个项目虚拟化解决方案,不过所有硬件设备不是我们采购的,我们只是负责软体安装.我看了一下那个硬件设备那叫高,不过目前还到那边去安装,那边硬件还没安装完成,然后Boss给我拿来两台新服务器,让 ...