HDOJ-----2647拓扑排序

Reward

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


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

本题是发工资,n个人和m个要求,下面m行两个数a, b,表明a的钱必须比b多,问最少要多少钱,基本工资888,每次多1块钱就好了

正向排序的话只能表明a的钱比b的钱要多,但不知道a比b多多少,即还有多少人满足a>x>b,反向建表的话,就类似于并查集向上一步一步找根节点,每次+1

这题真的很恼火,不知道为什么用单纯的邻接表不能实现,总是WA,又不是爆内存,希望大神给出指点

///****问题已解决

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<map>
#define maxn 40010
using namespace std;
int ans, m, n, flag, cnt;
int head[maxn], in[maxn], vis[maxn];
struct node{
	int to, next;
}edge[maxn];
void add(int u, int v, int i){
	edge[i].to = u;
	edge[i].next = head[v];
	head[v] = i;
	in[u]++;
}
void tuopu(){
	ans = cnt = 0;
	for(int i = 1; i <= m; i++){
		vis[i] = 888;
	}
	for(int i = 0; i < m; i++){
		for(int j = 1; j <= m; j++){
			if(in[j] == 0){
				flag = j;
				ans++;
				break;
			}
		}
		cnt += vis[flag];
		in[flag] = -1;
		for(int j = head[flag]; j != -1; j = edge[j].next){
			in[edge[j].to]--;
			//vis[edge[j].to] = vis[flag]+1;原来是这里少加了一个判断
			if(vis[edge[j].to] < vis[flag]+1){       //给一组数据 10 4
				vis[edge[j].to] = vis[flag]+1; 		 //	     	 1  2
			} 										 //          2  3
		}											 //          3  4
	}												//           1  5
}//不加判断的话,从3 4这一组开始执行,(4的入度为0,反向建表)到2 1这组数据时1的钱应该是891,到5的时候又直接赋值成889,就错了
int main(){
	int a, b;
	while(~scanf("%d%d", &m, &n)){
		memset(head, -1, sizeof(head));
		memset(in, 0, sizeof(in));
		for(int i = 0; i < n; i++){
			scanf("%d%d", &a, &b);
			add(a, b, i);
		}
		tuopu();
		if(ans == m){
			printf("%d\n", cnt);
		}
		else{
			printf("-1\n");
		}
	}
	return 0;
}










附正确代码


#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#define maxn 40010
using namespace std;
int ans, m, n, flag, cnt;
int head[maxn], in[maxn], vis[maxn];
struct node{
	int to, next;
}edge[maxn];
void add(int u, int v, int i){
	edge[i].to = u;
	edge[i].next = head[v];
	head[v] = i;
	in[u]++;
}
void tuopu(){
	ans = cnt = 0;
	queue<int> Q;
	for(int i = 1; i <= m; i++){
		vis[i] = 888;
		if(in[i] == 0){
			Q.push(i);
		}
	}
	while(!Q.empty()){
		flag = Q.front();
		Q.pop();
		in[flag] = -1;
		cnt += vis[flag];
		ans++;
		for(int i = head[flag]; i != -1; i = edge[i].next){
			if(!(--in[edge[i].to])){
				Q.push(edge[i].to);
			}
			vis[edge[i].to] = vis[flag]+1;
		}
	}
}
int main(){
	int a, b;
	while(~scanf("%d%d", &m, &n)){
		memset(head, -1, sizeof(head));
		memset(in, 0, sizeof(in));
		for(int i = 0; i < n; i++){
			scanf("%d%d", &a, &b);
			add(a, b, i);
		}
		tuopu();
		if(ans == m){
			printf("%d\n", cnt);
		}
		else{
			printf("-1\n");
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值