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. Android Studio VS Eclipse (还在用Eclipse?你OUT了!)

    转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990).谢谢支持! 公司派来一个去美国參加完AnDevCon大会的来给我们分享大会上学习的内容. AnDevC ...

  2. HDU 1421 搬寝室 (线性dp 贪心预处理)

    搬寝室 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  3. Html5 移动游戏开发

    有非常多游戏採用H5技术开发.比方三国来了.巴哈姆特之怒.切绳子等. 我们公司也有多款游戏用H5开发.H5开发成本低.效率高,方便做自己主动更新,可移植性好. 受益于H5技术,我们公司的非常多产品都非 ...

  4. 5.3.3 deque对象

    class collections.deque([iterable[, maxlen]]) 返回一个新双向队列,当有输入迭代器时.会从左至右地加入到队列里.假设没有输入參数,就创建一个空队列. deq ...

  5. android 之WebView

    (一)使用中遇到的问题: 1.解决webview缓存: WebSettings.LOAD_NO_CACHE   或者直接清除缓存 webView.getSettings().setCatchMode( ...

  6. Silverlight 2学习笔记二:三个基本布局控件(Canvas、StackPanel、Grid )

    这篇文章主要是翻译了ScottGu博客的文章:Silverlight Tutorial Part 2: Using Layout Management.虽然是翻译,但通过笔记记录,我发现对这三个布局控 ...

  7. struts2里result类型Stream的参数配置

    转自:https://blog.csdn.net/q714699280/article/details/51756126 contentType 内容类型,和互联网MIME标准中的规定类型一致,例如t ...

  8. 46. Ext中namespace的作用(转)

    转自:https://www.cnblogs.com/givemeanorange/p/5569954.html Ext中在每一个页面中添加一个namespace呢,就像下面的代码: // creat ...

  9. Coursera Algorithms Programming Assignment 4: 8 Puzzle (100分)

    题目原文:http://coursera.cs.princeton.edu/algs4/assignments/8puzzle.html 题目要求:设计一个程序解决8 puzzle问题以及该问题的推广 ...

  10. yii2的form表单用法

    使用表单 本章节将介绍如何创建一个从用户那搜集数据的表单页.该页将显示一个包含 name 输入框和 email 输入框的表单.当搜集完这两部分信息后,页面将会显示用户输入的信息. 为了实现这个目标,除 ...