ACM: hdu 2647 Reward -拓扑排序
hdu 2647 Reward
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
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
/*/
题意:
老板发工资,但是老板很小气,但是至少每个人得发888元,而且,每个员工之间有业绩比较,业绩高的工资得高,至少多1元,问至少要发多少钱。 思路:
拓扑排序,查找有多少个不同等级的点,每个点的值与前面累加1; 计总和加上每个人888. 简单的拓扑排序,没必要多说了: AC代码:
/*/
#include"algorithm"
#include"iostream"
#include"cstring"
#include"cstdlib"
#include"string"
#include"cstdio"
#include"vector"
#include"cmath"
#include"queue"
using namespace std;
#define memset(x,y) memset(x,y,sizeof(x))
#define memcpy(x,y) memcpy(x,y,sizeof(x))
#define MX 10005 int indegree[MX];
vector<int>next_v[MX]; void init() {
for(int i=0; i<MX; i++)
next_v[i].clear();
memset(indegree,0);
} int toposort(int n) {
int p[MX];
int cnt=0,money=0,num,summoney=n*888 ;
while(cnt!=n) {
num=0;
for(int i=1; i<=n; i++) {
if(!indegree[i]) {
indegree[i]=-1;
p[num]=i;
num++;
}
}
if(!num) {//没有员工,工资最高,不能排序,返回错误;
return -1;
} else {
summoney+=money*num;
money++;
cnt+=num; //表示已经历遍了这么多个员工
for(int i=0; i<num; i++) {
for(int j=0; j<next_v[p[i]].size(); j++) {
indegree[next_v[p[i]][j]]--;
}
}
}
}
return summoney;
} int main() {
int n,m,a,b;
while(~scanf("%d%d",&n,&m)) {
init();
for(int qq=0; qq<m; qq++) {
scanf("%d%d",&a,&b);
int flag=0;
for(int i=0; i<next_v[b].size(); i++) {
if(next_v[b][i]==a)
flag=1;
}
if(!flag) {
indegree[a]++;
next_v[b].push_back(a);
}
}
printf("%d\n",toposort(n));
}
return 0;
}
ACM: hdu 2647 Reward -拓扑排序的更多相关文章
- HDU.2647 Reward(拓扑排序 TopSort)
HDU.2647 Reward(拓扑排序 TopSort) 题意分析 裸的拓扑排序 详解请移步 算法学习 拓扑排序(TopSort) 这道题有一点变化是要求计算最后的金钱数.最少金钱值是888,最少的 ...
- 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 Reward(拓扑排序+反图)
题目链接:https://vjudge.net/contest/218427#problem/C 题目大意: 老板要给很多员工发奖金, 但是部分员工有个虚伪心态, 认为自己的奖金必须比某些人高才心理平 ...
- 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 ...
随机推荐
- C#4.0图解教程 - 第24章 反射和特性 – 2.特性
1.特性 定义 Attribute用来对类.属性.方法等标注额外的信息,贴一个标签(附着物) 通俗:给 类 或 类成员 贴一个标签,就像航空部为你的行李贴一个标签一样 注意,特性 是 类 和 类的成员 ...
- [Nodejs]十分钟快速编写简单静态文件服务器
学了几天Nodejs 后我又干上了前端的活.这次遇到的问题是,我想在不同的设备上方便的查看我编写的网页,很自然的就想到要是能在本地搭建一个简单的http服务器的话,那局域网内的所有设备都可以访问了,这 ...
- CentOS-6.5安装配置Tengine
一.安装pcre: cd /usr/local/src wget http://downloads.sourceforge.net/project/pcre/pcre/8.34/pcre-8.34.t ...
- android 相对布局里面的一些属性
一. 有关于RelativeLayout布局的一些属性 1. 相对于兄弟控件的位置:android:layout_below Android:layout_toLeftof Android:la ...
- php开启mysqli扩展之后如何连接数据库
Mysqli是php5之后才有的功能,没有开启扩展的朋友可以打开您的php.ini的配置文件;相对于mysql有很多新的特性和优势,需要了解的朋友可以参考下 Mysqli是php5之后才有的功能,没有 ...
- 【leetcode】Reverse Integer
题目描述: Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 很简单 ...
- 轻松学习RSA加密算法原理
转自:http://blog.csdn.net/sunmenggmail/article/details/11994013 http://blog.csdn.net/q376420785/articl ...
- 利用Visual GDB在Visual Studio中进行Android开发
转载请注明http://www.cnblogs.com/adong7639/p/4119467.html 无意中发现了Visual GDB这个工具,可以再Visual Studio中进行Android ...
- c语言的字符串操作(比较详细)
1)字符串操作 strcpy(p, p1) 复制字符串 strncpy(p, p1, n) 复制指定长度字符串 strcat(p, p1) 附加字符串 strncat(p, p1, n) 附加指定长度 ...
- Linux学习笔记(11)软件包管理
Linux中的软件包分为源码包(脚本安装包)及二进制包(RPM包.系统默认包).其中源码包的优点是: 1)源码包是开源的,如果有足够的能力,可以修改源代码: 2)可自由选择所需的功能: 3)源码包需编 ...