hdu2647——Reward

HDU2647_reward-拓扑排序 真的是简单的拓扑排序应用吗??师兄们别坑啦~~wa了几次就因为数据没处理好。。思路简单,但数据的处理很微妙,本以为自己总是对的,没想到还有脱离自己方法的测试数据。。但还是受益匪浅——每每碰到WA,检查思路,暴力枚举测试数据(最无可奈何,最实用的方法)。 阅读详情

Reward

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4306    Accepted Submission(s): 1318


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
 

Author
dandelion
 

Source
 

Recommend

一开始我以为是差分约束,然后写了下发现超时,无奈去看了下发现是topo排序,然后改正后就AC了

#include <map>  
#include <set>  
#include <list>  
#include <stack>  
#include <vector>  
#include <queue>  
#include <cmath>  
#include <cstdio>  
#include <cstring>  
#include <iostream>  
#include <algorithm>  

using namespace std;

const int N = 10010;
const int M = 20010;

int tot, n, m;
int head[N];
int num[N];
int in_degree[N];

struct node
{
	int next;
	int to;
}edge[M];

void addedge(int from, int to)
{
	edge[tot].to = to;
	edge[tot].next = head[from];
	head[from] = tot++;
}

bool topo_sort()
{
	queue <pair<int, int> > du;
	memset (num, 0, sizeof(num) );
	int cnt = 0;
	while ( !du.empty() )
	{
		du.pop();
	}
	for (int i = 1; i <= n; ++i)
	{
		if (!in_degree[i])
		{
			du.push( make_pair(0, i) );
			num[i] = 0;
		} 
	}
	while ( !du.empty() )
	{
		pair<int, int> tmp = du.front();
		du.pop();
		cnt++;
		int u = tmp.second;
		int res = tmp.first;
		for (int i = head[u]; ~i; i = edge[i].next)
		{
			in_degree[edge[i].to]--;
			if ( !in_degree[edge[i].to] )
			{
				num[edge[i].to] = res + 1;
				du.push( make_pair(res + 1, edge[i].to) );
			}
		}
	}
	return cnt == n;
}

int main()
{
	while (~scanf("%d%d", &n, &m))
	{
		int u, v;
		memset (head, -1, sizeof(head) );
		memset (in_degree, 0, sizeof(in_degree) );
		tot = 0;
		for (int i = 0; i < m; ++i)
		{
			scanf("%d%d", &u, &v);
			addedge(v, u);
			in_degree[u]++;
		}
		bool flag = topo_sort();
		if (!flag)
		{
			printf("-1\n");
			continue;
		}
		int ans = 0;
		for (int i = 1; i <= n; ++i)
		{
			ans += num[i];
		}
		ans += 888 * n;
		printf("%d\n", ans);
	}
	return 0;
}


HDU_2647_Reward(拓扑排序) 老板要给n个员工发年终奖金,然后给出m个需求 (a,b),表示a的奖金要比b的多,然后呢,老板决定每个员工至少可以拿888元,现在老板总共至少需要发多少奖金。 如果满足不了就输出-1。 阅读详情

相关推荐

HDU——2647Reward(DFS或差分约束)

Reward Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7078Accepted Submission(s): 2205 Problem Description Dandelion's uncl...

weixin_30809173的博客 143

hdu 2647 Reward

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 rewa

Remilia's 1033

HDU 2647Reward(拓扑排序+队列)

Reward Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 12918Accepted Submission(s...

weixin_33924770的博客 219

HDU 2647 Reward(拓扑排序)

HDU 2647 Reward(拓扑排序) http://acm.hdu.edu.cn/showproblem.php?pid=2647 题意:老板要给N个员工发工资,要求每个人最少888块且要满足M个要求.比如1号员工的工资要比2号员工的工资多.老板准备满足所有要求且最终发的奖金总数要最少.如果可行输出总数,不可行输出-1. 分析: 其实就是将所有员工的奖金拓扑排序,然后从888开始安排

code 1360

HDU.2647 Reward(拓扑排序 TopSort)

HDU.2647 Reward(拓扑排序 TopSort)题意分析裸的拓扑排序 详解请移步 算法学习 拓扑排序(TopSort) 这道题有一点变化是要求计算最后的金钱数。最少金钱值是888,最少的差额是1,如1号的人比2号钱要多,2号至少是1号人的钱数+1.根据拓扑排序的思想,我们只需要类比二叉树的层序遍历,每次取出入度为0的节点做处理,累加金钱数量,然后再取出入度为0的节点处理。直到取出所

Pengwill's Blog 442

题解报告:hdu 2647 Reward(拓扑排序)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to distribute rewards to his worker...

weixin_30508309的博客 193

HDU-2647-Reward

HDU-2647-Reward 传送门 这道题拓扑排序啦~ 差不多也是模板题。 就是这个地方的建图是逆向建图 题目大意:一个老板要发奖金。起价是888.员工之间有要求。(什么a需要比b高啥的) 给出m个约束条件。 然后问老板最少需要准备多少钱可以满足所有的要求。 如果无法满足所有要求的话就输出-1; 很显然。不是DAG的话就是 不满足要求的。 这个可以在bfs过程中判断~ 然后把ans[](每个员...

qq_44624316的博客 164

Reward HDU - 2647

题目链接:https://cn.vjudge.net/problem/HDU-2647 题目 老板发工资以及报酬,一共n个人,m个关系,每个关系中给出a和b,表示a的报酬要大于b的报酬,每个人的基本工资都是888元,问老板最少发多少工资。如果不能合理分配,输出-1。 分析 每输入一对a,b,就将a的度加1,然后拓扑排序就可以了。注意如果有环的话,那么一定不能合理分配。另外需要再开一个数组d...

黄油^小饼干的博客 264

HDU - 2647 Reward

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...

hpu风之旅 226

HDU 2647Reward

链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 Sample Input 2 1 1 2 2 2 1 2 2 1 Sample Output 1777 -1 题意:老板要发工资,但一些员工有自己的小算盘,并向老板提了出来,员工a和b,a的工资要比b的工资高。老板想尽量满足员工的要求,并且使自己支付的总金额最少。每个人的最低工资为888。(这老板可...

。。。的博客 216

HDU2647 Reward 拓扑排序

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题目大意:有n个人m对关系,每对关系表示a的工资要比b的高,最低工资为888,问你这n个人满足m对关系的最低总工资是多少。 分析:我们以这m对关系来建图,要使a的工资要比b的工资高,只需保证a到b的单向性即可。 实现代码如下: #include #include

AC_Gibson的专栏 527

HDU2647 Reward 题解

题目链接:Reward - HDU 2647 - Virtual Judge (vjudge.net) 题目描述: 思路: 先建一个高度树(图),如果遇到环直接返回-1(因为一个人的工资不可能比自己的工资更高)。然后有个细节,0 0的情况,要特判一下。 建高度树(图): DFS先序遍历(类似树形DP)+DFS判断环路+剪枝+思维。 根据题意,应该将最底部高度固定为1,然后DFS先序遍历往上排高度。(一般的DFS求深度是从上往...

issey的博客 247

hdu2647Reward

这个题目是拓扑排序,因为边的数目非常大,所以普通的建图会爆内存,所以这里采用vector容器。 1:建完图后对入度为0的点加入队列,然后对整幅图进行扫描,得到每个点的位置。 2:要用一个数组存相对位置,所以不能只用一个变量存储,因为每次出队列只能有一个元素。。 2:用数组模拟果然比stl快一些。。。 题目链接: 哈哈 我在这里 题面: Reward Time Limit

大洋深处 1187

HDU 2647Reward(拓扑排序)

题目地址:

scf0920 1526

拓扑排序学习小记 HDU 1285 确定比赛名次 + HDU 2647 Reward

拓扑排序,以下摘自http://blog.csdn.net/midgard/article/details/4101025: 拓扑排序是对有向无环图(DAG图)的一种排序。表示了顶点按边的方向出现的先后顺序。如果有环,则无法表示两个顶点的先后顺序。 在现实生活中,也会有不少应用例子,比如学校课程布置图,要先修完一些基础课,才可以继续修专业课。 一个简单的求拓扑排序的算法:首先要找到任意入

whyorwhnt的专栏 1172

hdu2647Reward

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 参考

xiaobei1a2b3c的专栏 598
上一篇: POJ1836——Alignment
下一篇: POJ3169——Layout && hdu3592——World Exhibition
tokers
博客等级 码龄12年 71粉丝 700原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值