Reward

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

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
 

wa了很多次,一直没找出错在哪,想了很久,发现了错误。

{6 6 2 6 3 6 4 2 4 3 4 1 4 5}这组数据的结果就不对。

如图:

最开始的程序算出的结果为5331,但正确答案为5332。找到了问题。需要每次对当前入度为0的点的后继更新值,而不是当后继度数为零了才更新。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<stack>
using namespace std; int n,m;
vector<int> v[]; int degree[];
int mon[]; void topusort()
{
stack<int>s;
for(int i=; i<=n; i++)
if(degree[i]==)
{
mon[i]=;
s.push(i);
}
long long tot=,cnt=;
while(!s.empty())
{
int h=s.top();
degree[h]--;
s.pop();
cnt++;
tot+=mon[h];
for(int i=; i<v[h].size(); i++)
{
degree[v[h][i]]--;
mon[v[h][i]]=max(mon[v[h][i]],mon[h]+); //做出的修改
if(degree[v[h][i]]==)
{
//mon[v[h][i]]=mon[h]+1; //之前的问题就出在这
s.push(v[h][i]);
}
}
}
if(cnt==n)
printf("%I64d\n",tot+*n);
else
printf("-1\n");
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(degree,,sizeof(degree));
memset(mon,,sizeof(mon));
for(int i=; i<=n; i++)
v[i].clear();
for(int i=; i<m; i++)
{
int a,b;
scanf("%d%d",&a,&b);
v[b].push_back(a);
degree[a]++;
}
//cout<<degree[1]<<endl;
topusort();
}
return ;
} /*
7 8
2 1
3 1
4 1
5 2
5 3
6 4
7 5
7 6
*/

HDU_Reward_拓扑排序的更多相关文章

  1. 算法与数据结构(七) AOV网的拓扑排序

    今天博客的内容依然与图有关,今天博客的主题是关于拓扑排序的.拓扑排序是基于AOV网的,关于AOV网的概念,我想引用下方这句话来介绍: AOV网:在现代化管理中,人们常用有向图来描述和分析一项工程的计划 ...

  2. 有向无环图的应用—AOV网 和 拓扑排序

    有向无环图:无环的有向图,简称 DAG (Directed Acycline Graph) 图. 一个有向图的生成树是一个有向树,一个非连通有向图的若干强连通分量生成若干有向树,这些有向数形成生成森林 ...

  3. 【BZOJ-2938】病毒 Trie图 + 拓扑排序

    2938: [Poi2000]病毒 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 609  Solved: 318[Submit][Status][Di ...

  4. BZOJ1565 [NOI2009]植物大战僵尸(拓扑排序 + 最大权闭合子图)

    题目 Source http://www.lydsy.com/JudgeOnline/problem.php?id=1565 Description Input Output 仅包含一个整数,表示可以 ...

  5. 图——拓扑排序(uva10305)

    John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...

  6. Java排序算法——拓扑排序

    package graph; import java.util.LinkedList; import java.util.Queue; import thinkinjava.net.mindview. ...

  7. poj 3687(拓扑排序)

    http://poj.org/problem?id=3687 题意:有一些球他们都有各自的重量,而且每个球的重量都不相同,现在,要给这些球贴标签.如果这些球没有限定条件说是哪个比哪个轻的话,那么默认的 ...

  8. 拓扑排序 - 并查集 - Rank of Tetris

    Description 自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球. 为了更好的符合那些爱好者的喜好,Lele又想了一个新点子:他将制作一个全球 ...

  9. *HDU1285 拓扑排序

    确定比赛名次 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

随机推荐

  1. Windows Server下把BAT批处理注册成服务在后台运行且注销后能正常运行

    批处理有如下特点: 1.登录到当前窗口运行时,如果关闭控制台会连同启动的程序一起关闭. 2.如果是以start /b的形式启动,那么同样也是在控制台关闭后者注销当前窗口也会一起关闭. 3.如果以vbs ...

  2. Cisco IOU Web Interface : Web IOU

    https://github.com/dainok http://sns.clnchina.com.cn/space.php?uid=404779&do=blog&id=4298 ht ...

  3. 1.4-动态路由协议OSPF④

    多区域的OSPF: 划分多区域的主要目的: 1.减少每个区域中的路由条目,进而减少每个路由器的内存中的路由,及其内存消耗,提高转发效率. 2.因为每一个OSPF区域对应在一个OSPF LSDB,配合在 ...

  4. TCP/IP具体解释学习笔记--TCP的坚持和保活定时器

    TCP的坚持定时器 1.基本概念 TCP的接收方指名希望从发送方接收的数据字节(窗体大小)来进行流量控制,假设窗体大小为0.那么放送方就会阻止发送数据,直到接收方发来一个已跟新窗体大小的ACK为止,那 ...

  5. 基于FPGA的简易数字时钟

    基于FPGA的可显示数字时钟,设计思路为自底向上,包含三个子模块:时钟模块,进制转换模块.led显示模块.所用到的FPGA晶振频率为50Mhz,首先利用它得到1hz的时钟然后然后得到时钟模块.把时钟模 ...

  6. Python中range和xrange的异同之处

    range     函数说明:range([start,] stop[, step]).依据start与stop指定的范围以及step设定的步长,生成一个序列. range演示样例:  >> ...

  7. LeetCode 824. Goat Latin (山羊拉丁文)

    题目标签:String 首先把vowel letters 保存入 HashSet. 然后把S 拆分成 各个 word,遍历每一个 word: 当 word 第一个 字母不是 vowel 的时候,把第一 ...

  8. Mysql常用索引及优化

    索引是帮助我们快速获取数据的数据结构.索引是在存储引擎中实现的,因此不同存储引擎的索引也不同.这里只介绍InnoDB存储索引所支持的BTree索引: 一.索引类型 为了方便举例子,先创建表person ...

  9. Android横竖屏切换不重新调用onCreate()

    再次感叹Android的碎片化!!!! 设置AndroidManifest.xml中Activity的android:configChanges=”keyboardHidden|orientation ...

  10. VS2010 使用TeeChart绘图控件 - 之一 - 控件和类的导入

    vs2010的用法和vc6有很大的不同,特别是在一些函数调用那里,当然.控件导入也是很不一样的 安装好控件后就可以在工程里加入teechart控件了 加入方法有如下几种: 1.添加Teechart控件 ...