杭电2647 Reward

原创 2015年11月20日 20:29:33

Reward

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


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
 


此题优先队列无解,必须反向建图:

#include<stdio.h>
#include<queue>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
struct node
{
	int u,v,w,next;
}t[1100000];
int head[110000],i,j,k,l,m,n,dig[110000],cnt,u,v,ans,flag[110000],num;
void topo()
{
	//priority_queue<int >q;
	//priority_queue<int>q;
	queue<int>q;
	int i,c=0;
	for(i=1;i<=m;i++)
	if(dig[i]==0)
	{
		q.push(i);
	//	ans++;
	}
	while(!q.empty())
	{
		//int x=q.top();
		int x=q.front();
		q.pop();
		ans++;
		num+=flag[x];
		for(i=head[x];i!=-1;i=t[i].next)
		{
			int y=t[i].v;
			dig[y]--;
			if(dig[y]==0)
			{
				q.push(y);
				flag[y]=flag[x]+1;
			}
		}
	}
	if(ans!=m)
	num=-1;
}
int main()
{
	while(scanf("%d%d",&m,&n)!=EOF)
	{
		memset(head,-1,sizeof(head));
		memset(flag,0,sizeof(flag));
		memset(dig,0,sizeof(dig));
		cnt=ans=0;
		for(i=0;i<n;i++)
		{
			scanf("%d%d",&v,&u);
			t[cnt].u=u;
			t[cnt].v=v;
			t[cnt].next=head[u];
			head[u]=cnt++;
			dig[v]++;
		}
		num=888*m;
		topo();
		printf("%d\n",num);
	}
}



版权声明:本文为博主原创文章,未经博主允许不得转载。

hdu2647 Reward(拓扑排序)

老板要给很多员工发奖金, 但是部分员工有个虚伪心态, 认为自己的奖金必须比某些人高才心理平衡 输入若干个关系 a b a c c b 意味着a 的工资必须比b的工资高 同时a 的工资比c高; c的工资...
  • ygqwan
  • ygqwan
  • 2013年09月19日 09:35
  • 1822

【杭电】[2647]Reward

Reward Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is...
  • u011493189
  • u011493189
  • 2016年08月06日 09:58
  • 102

【杭电2647】Reward

Reward Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status ...
  • idealistic
  • idealistic
  • 2016年08月05日 21:13
  • 109

【HDU】2647 Reward 拓扑排序

Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S...
  • u013368721
  • u013368721
  • 2014年07月14日 09:01
  • 878

HDU 2647 Reward(拓扑排序)

原题链接:Here! 分析:因为最近一直在做拓扑排序的问题,所以没多想就采用拓扑排序来解决这个问题,后来一想,跟并查集好像...... 1.判断图中是否存在环只需看能够处理多少节点,如果 处理节点数...
  • qq_33266889
  • qq_33266889
  • 2016年11月27日 00:28
  • 260

HDU 2647 Reward(拓扑排序)

Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub...
  • llwwlql
  • llwwlql
  • 2016年05月31日 15:26
  • 290

hdu 2647 Reward(拓扑排序)

Problem Description Dandelion’s uncle is a boss of a factory. As the spring festival is coming , he...
  • feizaoSYUACM
  • feizaoSYUACM
  • 2017年04月27日 14:46
  • 160

HDU 2647 - Reward(拓扑排序)

Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S...
  • coco430
  • coco430
  • 2013年10月04日 21:41
  • 380

hdu 2647 Reward(拓扑排序)

RewardProblem Description Dandelion’s uncle is a boss of a factory. As the spring festival is comin...
  • blessLZH0108
  • blessLZH0108
  • 2017年07月20日 10:14
  • 162

HDU 2647 Reward(反向拓扑)

这题给的内存忒小了,不能用邻接矩阵存,只能用邻接表
  • u013617636
  • u013617636
  • 2014年11月12日 22:22
  • 461
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:杭电2647 Reward
举报原因:
原因补充:

(最多只允许输入30个字)