hdu 2647 Reward(拓扑排序,反着来)
Reward
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 51 Accepted Submission(s) : 21
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
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
then m lines ,each line contains two integers a and b ,stands for a's reward should be more than b's.
Output
Sample Input
2 1
1 2
2 2
1 2
2 1
Sample Output
1777
-1
#include <iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
int i,n,m;
int a[],cnt[];
vector<int> s[];
long long sum;
bool toposort()
{
queue<int> Q;
int num=;
for(int i=;i<=n;i++)
if (cnt[i]==) {Q.push(i); a[i]=;}
while(!Q.empty())
{
int u=Q.front();
num++;
Q.pop();
for(int i=;i<s[u].size();i++)
{
cnt[s[u][i]]--;
if (cnt[s[u][i]]==)
{
Q.push(s[u][i]);
a[s[u][i]]=max(a[u]+,a[s[u][i]]);
}
}
}
if (num<n) return ;
return ;
}
int main()
{ while(~scanf("%d%d",&n,&m))
{
for(i=;i<=n;i++) s[i].clear();
memset(cnt,,sizeof(cnt));
for(i=;i<=m;i++)
{
int x,y;
scanf("%d%d",&y,&x);
s[x].push_back(y);
cnt[y]++;
}
memset(a,,sizeof(a));
if (!toposort())
{
printf("-1\n");
continue;
}
sum=;
for(i=;i<=n;i++) sum+=a[i];
printf("%lld\n",sum);
}
return ;
}
hdu 2647 Reward(拓扑排序,反着来)的更多相关文章
- hdu 2647 Reward(拓扑排序+反图)
题目链接:https://vjudge.net/contest/218427#problem/C 题目大意: 老板要给很多员工发奖金, 但是部分员工有个虚伪心态, 认为自己的奖金必须比某些人高才心理平 ...
- HDU.2647 Reward(拓扑排序 TopSort)
HDU.2647 Reward(拓扑排序 TopSort) 题意分析 裸的拓扑排序 详解请移步 算法学习 拓扑排序(TopSort) 这道题有一点变化是要求计算最后的金钱数.最少金钱值是888,最少的 ...
- ACM: hdu 2647 Reward -拓扑排序
hdu 2647 Reward Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Des ...
- HDU 2647 Reward (拓扑排序)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题意是给你n点m条有向边,叶子点(出度为0)上的值为888,父亲点为888+1,依次计算... ...
- hdu 2647 Reward(拓扑排序+优先队列)
Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he ...
- HDU 2647 逆向拓扑排序
令每一个员工都有一个自己的等级level[i] , 员工等级越高,那么工资越高,为了使发的钱尽可能少,所以每一级只增加一单位的钱 输入a b表示a等级高于b,那么我们反向添加边,令b—>a那么i ...
- 题解报告:hdu 2647 Reward(拓扑排序)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 Problem Description Dandelion's uncle is a boss ...
- HDU 2647 Reward(拓扑排序+判断环+分层)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题目大意:要给n个人发工资,告诉你m个关系,给出m行每行a b,表示b的工资小于a的工资,最低工 ...
- HDU 2647 Reward【反向拓扑排序】
Reward Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- hdu 2647 Reward
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2647 Reward Description Dandelion's uncle is a boss o ...
随机推荐
- 将数据库的数据导入solr索引库中
在solr与tomcat整合文章中,我用的索引库是mycore,现在就以这个为例. 首先要准备jar包:solr-dataimporthandler-4.8.1.jar.solr-dataimport ...
- 项目中 添加 swift代码 真机调试 错误
错误: dyld: Library not loaded: @rpath/libswiftCore.dylib Referenced from: /private/var/mobile/Contain ...
- ASP.NET中application对象的用法(面试题)
ASP.NET中application对象的用法 本文导读:Application对象是HttpApplicationState类的一个实例,Application状态是整个应用程序全局的.Appli ...
- 解决Eclipse无法添加Tomcat服务器的问题
eclipse配置好以后,如果Tomcat服务器在文件系统的位置发生了变化,则需要重新配置Tomcat服务器,这时会遇到无法设置服务器的问题 即图中框起来的部分无法进行操作,这时需要 关闭Eclips ...
- 50个很棒的Python模块
50个很棒的Python模块 我很喜欢Python,Python具有强大的扩展能力,我列出了50个很棒的Python模块,包含几乎所有的需要:比如Databases,GUIs,Images, Soun ...
- linux中将程序加入到开机自动启动
如果将在linux中将命令或者程序设置为开机自动启动,只需要进入到将对应命令加入到/etc/rc.d/rc.local文件里即可,如下 打开文件,vi /etc/rc.d/rc.local #!/bi ...
- python--lambda和def函数
1.Python lambda和Python def区别分析 Python支持一种有趣的语法,它允许你快速定义单行的最小函数.这些叫做lambda的函数,是从Lisp借用来的,可以用在任何需要函数的地 ...
- 【python问题系列--1】SyntaxError:Non-ASCII character '\xe5' in file kNN.py on line 2, but no encoding declared;
因为Python在默认状态下不支持源文件中的编码所致.解决方案有如下三种: 一.在文件头部添加如下注释码: # coding=<encoding name> 例如,可添加# coding= ...
- 安卓无法生成R文件原因
原因个人总结出来: 清单文件报错,则无法生成R文件 gen和bin目录可以删除
- NSTimer定时器
NSTimer的避免内存泄露与控制器的正确引用和释放: