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 ...
随机推荐
- Python开发【第二篇】:初始Python
Python的前世今生 Python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,中国人称他为龟叔,他在圣诞节期间在阿姆斯特丹为了打发时间,决定开发一个新的脚本 ...
- 数据结构5_java---二叉树,树的建立,树的先序、中序、后序遍历(递归和非递归算法),层次遍历(广度优先遍历),深度优先遍历,树的深度(递归算法)
1.二叉树的建立 首先,定义数组存储树的data,然后使用list集合将所有的二叉树结点都包含进去,最后给每个父亲结点赋予左右孩子. 需要注意的是:最后一个父亲结点需要单独处理 public stat ...
- Vulnhub靶场渗透练习(三) bulldog
拿到靶场后先对ip进行扫描 获取ip 和端口 针对项目路径爆破 获取两个有用文件 http://192.168.18.144/dev/ dev,admin 更具dev 发现他们用到框架和语言 找到一 ...
- linux网络通讯相关命令
ifconfig 1.查看当前使用的网卡 watch cat /proc/net/dev 看下哪张网卡的流量变化大一般就是哪张网卡是在线使用的 2.ifconfig查看所有网卡信息,ifconfig ...
- 我的Python学习方向-前端辅助-后端框架django学习-浅谈(一)
初始python,很直观的感受是编译格式多样,代码简介易懂 作为一门通用编程语言,python能编写多种用途的编程语言,当然对于我目前,我的方向便是借助其前端编辑器,实现后台框架的连接学习 1.首先便 ...
- C#的集合类型及使用技巧
在日常开发过程中,我们不能避免的要对批量数据处理,这时候就要用到集合.集合总体上分为线性集合和非线性集合.线性集合是指元素具有唯一的前驱和后驱的数据结构类型:非线性集合是指有多个前驱和后驱的数据结构类 ...
- 百万年薪python之路 -- MySQL数据库之 常用数据类型
MySQL常用数据类型 一. 常用数据类型概览 # 1. 数字: 整型: tinyint int bigint 小数: float: 在位数比较短的情况下不精确 double: 在位数比较长的情况下不 ...
- js小数加减乘除时精度不准确
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/qq_33237207/article/d ...
- Python 破解Linux密码
简介:因为Linux的密码都是加密过的(例如:$6$X.0bBN3w$NfM7YYHevVfCnZAVruItAEydaMJCF.muefZsxsgLK5DQoahW8Pqs1BSmoAFfi5J/b ...
- pymysql 防止sql注入案例
from pymysql import connect def main(): """sql演示""" # 1.输入一个语句,根据id展示相 ...