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. javascript的层次

    1.功能api 2.代码organization 3.performance 4.work flow

  2. linux 命令总结(转载)

    linux 命令总结(转载) 1. 永久更改ip ifconfig eth0 新ip 然后编辑/etc/sysconfig/network-scripts/ifcfg-eth0,修改ip 2.从Lin ...

  3. C#打印条码BarTender SDK打印之路和离开之路(web平凡之路)

    从来没想过自己会写一篇博客,鉴于这次从未知的探索到一个个难点的攻破再到顺利打印,很想记录这些点滴,让后人少走弯路. 下面走进正题. 需求:取数据库里的相应的字段数据,并生成条形码,可以批量.单条打印. ...

  4. Java File类读取文件属性

     package myjavademo;import java.io.*; publicclass MyJavaDemo {     public static void main(String[]  ...

  5. ORACLE Recyclebin管理及flashback recyclebin中的对象

    Flashback用于恢复用户误删除的对象(包括表,索引等), 不支持sys用户. system表空间下的对象,也不能从回收站里拿到.故使用SYS 或者SYSTEM用户登陆时, show recycl ...

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

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

  7. EF项目中应用出现问题???

    最近用EF做了个项目发现很多不便利的地方. 具体如下. 1,我是通过edmx 建模,然后通过模型生成数据库. 虽然数据库已经创建成功但是问题来了,我在加字段,和标的时候再次生成时domeo.edmx. ...

  8. (原) Jquery 判断移动设备是IOS / Android系统

    var u = navigator.userAgent; var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > - ...

  9. (原)工具篇-利用fis压缩项目

    fis3 1.添加 fis-conf.js 到项目根目录中 fis-conf.js 内容如下 : //配置MD5版本控制 fis.match('*.{js,css,png,jpg}', { useHa ...

  10. StringBuffer工具类整理(一)

    package com.gzcivil.utils; /** * 同StringBuffer * * @author Dragon * @time 2013-3-1 */ public class S ...