HDU 2647 Reward

Reward

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


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个工人 老板要给工人发奖金,奖金最低为 888 给你m个关系 a b 表示a的奖金要比b的奖金多。。问你老板最少要花费
多少钱 如果矛盾的话 ,就输出-1 。
思路 : 存图的时候注意要是钱最小的为没有入度的点。 ans 记录每个人的得到的最少的钱。 fincnt 判断是否有矛盾存在
如果有矛盾存在那么 fincnt不可能等于n 。
代码:
#include<stdio.h>
#include<string.h>
#include<queue>
#include<iostream>
#include<algorithm>
#define N 105

using namespace std;

struct node
{
	int u,v,next;
}edge[N];

int ingree[N],head[N];
int n,m,cnt,fin;

void add(int u,int v)
{
	edge[++cnt].u=u;
	edge[cnt].v=v;
	edge[cnt].next=head[u];
	head[u]=cnt;
	return ;
}

int main()
{
	int u,v,i;
	while(scanf("%d %d",&n,&m)!=EOF)
	{
		if(n==0&&m==0) break;
		memset(ingree,0,sizeof(ingree));
		cnt=0;
		memset(head,-1,sizeof(head));
		for(i=1;i<=m;i++)
		{
			scanf("%d %d",&u,&v);
			add(u,v);
			ingree[v]++;
		}
		fin=0;
		queue< int >q;
		for(i=0;i<n;i++)
		{
			if(ingree[i]==0)
			{
				q.push(i);
				fin++;
			}
		}
		
		int temp;
		while(!q.empty())
		{
			temp=q.front();
			//printf("%d ",temp);
			q.pop();
			for(int k=head[temp];k!=-1;k=edge[k].next){
				v=edge[k].v;
				ingree[v]--;
				if(ingree[v]==0)
				{
					fin++;
					q.push(v);
				}
			}
		}
		
		if(fin==n) printf("YES\n");
		else printf("NO\n");
		
	}
	
	return 0;
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值