HDOJ 2647 Reward 【逆拓扑排序+分层】

题意:每一个人的基础工资是888。 因为一部分人要显示自己水平比較高,要求发的工资要比其它人中的一个人多。问你能不能满足他们的要求,假设能的话终于一共要发多少钱,假设不能就输出-1.

策略:拓扑排序。

这道题有些难点:一:数据大,建二维数组肯定不行,要换其它的数据结构(vector, 或者是链式前向星(本题代码用的是链式前向星)); 二:要逆拓扑排序(就是将++in[b]换成++in[a])。 三要分层次(依据上一个的钱数+1就可以)。

不懂什么是链式前向星 移步:http://blog.csdn.net/acdreamers/article/details/16902023

代码:

#include<stdio.h>
#include<string.h>
#include<queue>
#define MAXN 10005
int head[MAXN*2];
struct EdgeNode{
	int to;
	int next;
};
EdgeNode edges[MAXN*2];
int in[MAXN], queue[MAXN], money[MAXN];
int n, ans;
int toposort()
{
	ans = 0;
	memset(money, 0, sizeof(money));
	int i, j;
	int iq = 0;
	for(i = 1; i <= n; i ++){
		if(!in[i]){
			queue[iq++] = i;
		}
	}
	for( i = 0; i < iq; i ++){
		int temp = queue[i];
		ans += money[temp];
		for(j = head[temp]; j != -1; j = edges[j].next){
			if(!--in[edges[j].to]){
				queue[iq++] = edges[j].to;
				money[edges[j].to] = money[temp]+1;//这里是分层次。
			}
		}
	}
	return iq == n;
}
int main()
{
	int m, i, a, b;
	while(scanf("%d%d", &n, &m) == 2){
		memset(head, -1, sizeof(head));
		memset(in, 0, sizeof(in));
		for(i = 0; i < m; i ++){
			scanf("%d%d", &a, &b);
				in[a]++;
				edges[i].to = a;
				edges[i].next = head[b];
				head[b] = i;
		}
		int sum = 888*n;
		int flag = toposort();
		printf("%d\n", flag?sum+ans:-1);
	}
	return 0;
}
 

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

时间: 2024-11-05 11:43:08

HDOJ 2647 Reward 【逆拓扑排序+分层】的相关文章

杭电 2647 Reward (拓扑排序反着排)

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

HDU 2647 Reward【拓扑排序】

题意:工厂发工资,最低工资是888元,然后比他高一层得人的工资是889,依次类推 因为是从工资低的人推到工资高的人,所以反向建图 然后就是自己写的时候犯的错误,以为工资是后一个人比前一个人高1元,然后就直接判断是否能形成拓扑序列之后,用n*888+(n-1)*n/2来算了 这样不对,是后一层的工资比前一层得工资多1元,用一个数组记录下来钱就可以了 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4

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 Dandelion's uncle is a boss of

hdu 2647 Reward(拓扑排序)

1.反向建图,有利于计算 2.代码: #include<cstdio> #include<cstring> #include<stdlib.h> #define Max(a,b) ((a)>(b)?(a):(b)) using namespace std; typedef struct ArcNode { int adjvex; struct ArcNode * nextarc; } ArcNode; typedef struct VNode { int vert

hdu 2647 Reward (拓扑排序分层)

Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3815    Accepted Submission(s): 1162 Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wa

HDU 2467 Reward(逆拓扑排序)

拓扑排序的变形,逆序建图就好了 Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3951    Accepted Submission(s): 1203 Problem Description Dandelion's uncle is a boss of a factory. As the spring festival

HDU2647 Reward 【拓扑排序】

Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3927    Accepted Submission(s): 1199 Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he w

Reward(拓扑排序)

http://acm.hdu.edu.cn/showproblem.php?pid=2647 Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5336    Accepted Submission(s): 1619 Problem Description Dandelion's uncle is a boss of a fa

HDU4857 逃生【逆拓扑排序】

逃生 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1450    Accepted Submission(s): 448 Problem Description 糟糕的事情发生啦,现在大家都忙着逃命.但是逃命的通道很窄,大家只能排成一行. 现在有n个人,从1标号到n.同时有一些奇怪的约束条件,每个都形如:a必须在b之前. 同时,社会