hdu 2647 Reward (topsort)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 13132 Accepted Submission(s): 4199
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.
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.
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.
2 1
1 2
2 2
1 2
2 1
1777
-1
C\C++:
#include <map>
#include <queue>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <climits>
#include <iostream>
#include <algorithm>
#define INF 0xffffff
using namespace std; const int my_max = ;
int n, m, a, b, my_cnt, my_indeg[my_max];
vector <int> my_G[my_max]; int topsort()
{
my_cnt = ;
int my_ans = , t = ;
queue <int> Q;
while (t ++)
{
int flag = -, my_temp = ;
for (int i = ; i <= n; ++ i)
if (my_indeg[i] == )
{
my_indeg[i] = -;
flag = ;
Q.push(i);
my_cnt ++;
my_temp ++;
} if (flag == -) break;
my_ans += my_temp * t; while (my_temp --)
{
int my_now = Q.front();
for (int i = ; i < my_G[my_now].size(); ++ i)
my_indeg[my_G[my_now][i]] --;
my_G[my_now].clear();
Q.pop();
}
}
if (my_cnt == n)
return my_ans; for (int i = ; i <= n; ++ i)
my_G[i].clear();
return -;
} int main()
{
while (~scanf("%d%d", &n, &m))
{
memset(my_indeg, , sizeof(my_indeg));
while (m --)
{
scanf("%d%d", &a, &b);
++ my_indeg[a];
my_G[b].push_back(a);
} printf("%d\n", topsort());
}
return ;
}
hdu 2647 Reward (topsort)的更多相关文章
- HDU.2647 Reward(拓扑排序 TopSort)
HDU.2647 Reward(拓扑排序 TopSort) 题意分析 裸的拓扑排序 详解请移步 算法学习 拓扑排序(TopSort) 这道题有一点变化是要求计算最后的金钱数.最少金钱值是888,最少的 ...
- 题解报告:hdu 2647 Reward(拓扑排序)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 Problem Description Dandelion's uncle is a boss ...
- 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 Reward Description Dandelion's uncle is a boss o ...
- HDU 2647 Reward (拓扑排序)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题意是给你n点m条有向边,叶子点(出度为0)上的值为888,父亲点为888+1,依次计算... ...
- 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(图论-拓扑排序)
Reward Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is comin ...
- hdu 2647 Reward(拓扑排序,反着来)
Reward Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submis ...
随机推荐
- [BZOJ3813] 奇数国 - 线段树
3813: 奇数国 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 912 Solved: 508[Submit][Status][Discuss] ...
- [BZOJ1054] 移动玩具
1054: [HAOI2008]移动玩具 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2741 Solved: 1537[Submit][Stat ...
- JQ获取元素属性值
最近在学习JAVA Web,自己也是做个下列表左右选择的小案例. 获取某个元素的属性值一直以为是要调用atrr方法,不过好像获取元素的数组形式再遍历每个元素的时候想获取到它的属性值用attr方法有问题 ...
- 同一台机上配置多个redis服务
"D:\Program Files\Redis\redis-server.exe" --service-run F:Redis2\redis.windows-service6380 ...
- VAD树结构体的属性以及遍历
Windows内核分析索引目录:https://www.cnblogs.com/onetrainee/p/11675224.html VAD树的属性以及遍历 前面学习过的PFNDATABSAE是管理物 ...
- chrome devtools tip(1)--调试伪类
开发中我们经常遇到,添加些focus,hover事件,样式,但当我们去打开 chrome devtools,浮动上去的时候,然后准备去改变样式的时候,结果由于光标移动了,样式不见了,非常不方便调试,其 ...
- Arduino学习笔记⑤ 模拟IO实验
1.前言 还记得前几个我们都是在讲解数字IO,而其实我们生活中大多数信号都是模拟信号,如声音以及温度变化.在Arduino中,常用0~5v的电压来表示模拟信号. 1.1 模拟输入功能 ...
- 百万年薪python之路 -- 装饰器进阶
本文链接:https://blog.csdn.net/xiemanR/article/details/72510885 一:函数装饰函数 def wrapFun(func): def inner(a, ...
- Java学习笔记十二--集合(三)
第一节课 返回值 方法名 作用 void add(index,elemnet) 在指定的索引处添加元素 object get(index) 返回指定索引处的元素 int indexOf(object) ...
- IIS服务器文件跨域问题(几乎可以解决大多数跨域问题)
Windows server 服务器 1:在管理工具中选择,找到IIS 2:打开IIS管理,找到网站 3:找到HTTP响应头标 4:打开HTTP响应头标 5:添加 添加:Access-Control- ...