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 10+Ubuntu 16.04在MBR分区上安装双系统(转)

    以下内容转自这篇博客: http://www.cnblogs.com/Duane/p/5424218.html http://www.cnblogs.com/Duane/p/6776302.html( ...

  2. MySQL大小写问题的简单说明(关键字/函数/表名)(转)

    MySQL语句中字母大小写规则随着语句元素的不同而变化,同时还要取决于MySQL服务器主机上的操作系统. SQL关键字与函数名 关键字和函数名不区分字母的大小写.如.abs.bin.now.versi ...

  3. 8、Java并发性和多线程-静态条件与临界区

    以下内容转自http://ifeve.com/race-conditions-and-critical-sections/: 在同一程序中运行多个线程本身不会导致问题,问题在于多个线程访问了相同的资源 ...

  4. 设计模式(一)单例模式:创建模式 ASPNET CORE WEB 应用程序的启动 当项目中 没有STARTUP.CS 类如何设置启动 配置等等

    设计模式(一)单例模式:创建模式 先聊一下关于设计的几个原则(1)单一原则(SRP):一个类应该仅有一个引起它变化的原因 :意思就是 (一个类,最好只负责一件事情,并且只有一个引起它变化的原因(2)开 ...

  5. C#反射发出System.Reflection.Emit学习

    一.System.Reflection.Emit概述 Emit,可以称为发出或者产生.与Emit相关的类基本都存在于System.Reflection.Emit命名空间下.反射,我们可以取得形如程序集 ...

  6. configure: error: mysql configure failed. Please check config.log for more information.

    为php添加mysql模块时报错 configure: error: mysql configure failed. Please check config.log for more informat ...

  7. oc77--结构体,NSNumber,NSValue,NSDate,NSCalendar

    // // main.m // OC中的常用结构体 // #import <Foundation/Foundation.h> int main(int argc, const char * ...

  8. 51nod 1353 树

    树背包 设f[i][j]表示第i个点,和子节点组成的联通块大小为j,其他都可行的方案 j=0表示可行的总方案 #include<cstdio> #include<iostream&g ...

  9. maven 国内完整源

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...

  10. Android Studio笔记

    1. toolbar xml: <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:la ...