HDU_Reward_拓扑排序
Reward
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7009 Accepted Submission(s): 2174
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.
then m lines ,each line contains two integers a and b ,stands for a's reward should be more than b's.
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_拓扑排序的更多相关文章
- 算法与数据结构(七) AOV网的拓扑排序
今天博客的内容依然与图有关,今天博客的主题是关于拓扑排序的.拓扑排序是基于AOV网的,关于AOV网的概念,我想引用下方这句话来介绍: AOV网:在现代化管理中,人们常用有向图来描述和分析一项工程的计划 ...
- 有向无环图的应用—AOV网 和 拓扑排序
有向无环图:无环的有向图,简称 DAG (Directed Acycline Graph) 图. 一个有向图的生成树是一个有向树,一个非连通有向图的若干强连通分量生成若干有向树,这些有向数形成生成森林 ...
- 【BZOJ-2938】病毒 Trie图 + 拓扑排序
2938: [Poi2000]病毒 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 609 Solved: 318[Submit][Status][Di ...
- BZOJ1565 [NOI2009]植物大战僵尸(拓扑排序 + 最大权闭合子图)
题目 Source http://www.lydsy.com/JudgeOnline/problem.php?id=1565 Description Input Output 仅包含一个整数,表示可以 ...
- 图——拓扑排序(uva10305)
John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...
- Java排序算法——拓扑排序
package graph; import java.util.LinkedList; import java.util.Queue; import thinkinjava.net.mindview. ...
- poj 3687(拓扑排序)
http://poj.org/problem?id=3687 题意:有一些球他们都有各自的重量,而且每个球的重量都不相同,现在,要给这些球贴标签.如果这些球没有限定条件说是哪个比哪个轻的话,那么默认的 ...
- 拓扑排序 - 并查集 - Rank of Tetris
Description 自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球. 为了更好的符合那些爱好者的喜好,Lele又想了一个新点子:他将制作一个全球 ...
- *HDU1285 拓扑排序
确定比赛名次 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
随机推荐
- iOS中xib与storyboard原理,与Android界面布局的异同
用文本标记语言来进行布局,用的最多的应该是HTML语言.HTML能够理解为有一组特殊标记的XML语言. 一.iOS中xib与storyboard显示原理 在iOS中基本的布置界面的方式有3种:代码.x ...
- C#文件运行类的VB.NET版本号
主要差别在于事件处理要採用AddHandler和RemoveHandler,以及AddressOf三个keyword,其他基本一样. VB的操作稍微繁琐.但仍然能够实现.
- Apache配置基于域名的虚拟主机
一.设定 模拟域名 www.wang.org.blog.wang.org.bbs.wang.org 网站文件夹 /var/html/www./var/html/blog./var/html/bbs ...
- Ubuntu中取消秘钥环
1.打开应用程序->附件->password和加密密钥(或者在终端中输入 seahorse) 2.切换到password选项卡,会看到一个password密钥环 3.右击->更改pa ...
- View载入具体解释
文章一開始我要对前面一篇文章做点补充 相信大家都知道View有两个方法. public boolean post(Runnable action) public boolean postDelayed ...
- LeetCode 884. Uncommon Words from Two Sentences (两句话中的不常见单词)
题目标签:HashMap 题目给了我们两个句子,让我们找出不常见单词,只出现过一次的单词就是不常见单词. 把A 和 B 里的word 都存入 map,记录它们出现的次数.之后遍历map,把只出现过一次 ...
- intellij idea 写 Helloworld
http://www.jetbrains.com/idea/webhelp/creating-and-running-your-first-java-application.html Creating ...
- MongoDB 开机自启动
MongoDB安装了以后,应当设置开机自启动. 假设启动命令如下: sudo /db/mongodb/265/bin/mongod --config /db/conf/mongodb/mongodb. ...
- Codeforces Round #327 (Div. 2)C. Median Smoothing 构造
C. Median Smoothing A schoolboy named Vasya loves reading books on programming and mathematics. He ...
- 又发现2个高人写的Delphi图文并茂的消息研究
http://ymg97526.blog.163.com/blog/static/173658160201139101120862/http://ymg97526.blog.163.com/blog/ ...