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

                                        Reward

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

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中工资关系,每次输入两个人,要求前面的人的工资比后面高。员工的最低工资是888,老板一共需要支付多少工资?如果支付工资是不能满足所有人的要求,输出-1

思路

反向建图,让工资低的那个人的顶点指向工资高的人。然后拓扑排序判断是否有环,有环输出-1 。没有环就将图分层,每一层工资增加1 ,最后输出所需发的工资。

注意建图时用vector,二维数组会爆内存

AC代码

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <string>
#define l long long
#define ms(a) memset(a,0,sizeof(a))
#define pi acos(-1.0)
#define INF 0x3f3f3f3f
const double E=exp(1);
const int maxn=1e4+10;
using namespace std;
int n,m;
vector<int> v[maxn];
int vis[maxn];
int money[maxn];
bool toposort()
{
	int ans=0;
	queue<int>que;
	for(int i=1;i<=n;i++)
	{
		// 把刚开始入度为0的点放入
		if(!vis[i])
			que.push(i);
	}
	while(!que.empty())
	{
		int res=que.front();
		que.pop();
		// 每次pop出去的点都是入度为0的点,统计这些点的个数
		ans++;
		for(int i=0;i<v[res].size();i++)
		{
			vis[v[res][i]]--;
			if(vis[v[res][i]]==0)
			{
				// 将入度为0的点加入队列
				que.push(v[res][i]);
				// 更新一下后继点的工资,有点难理解,建议画一下图找几组样例试一下
				money[v[res][i]]=max(money[res]+1,money[v[res][i]]);
			}
		}
	}
	if(ans==n)
		return true;
	else
		return false;
}
int main(int argc, char const *argv[])
{
	ios::sync_with_stdio(false);
	int x,y;
	while(cin>>n>>m)
	{
		ms(vis);
		ms(money);
		for(int i=0;i<maxn;i++)
			v[i].clear();
		for(int i=0;i<m;i++)
		{
			cin>>x>>y;
			// 反向建图
			v[y].push_back(x);
			vis[x]++;
		}
		if(!toposort())
			cout<<-1<<endl;
		
		else
		{
			ll ans=888*n;
			for(int i=1;i<=n;i++)
				ans+=money[i];
			cout<<ans<<endl;
		}
	}
	return 0;
}
 

 

转载于:https://www.cnblogs.com/Friends-A/p/10324412.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值