Reward (HDU-2647)(拓扑排序)
作者:互联网
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
题意:老板打算给员工们发奖励,但是员工会去存在一些要求,例如,输入a b 即a员工的奖励要比b员工的奖励多。老板决定每个员工的最低奖励为888元。老板想要知道自己最少需要多少钱俩奖励员工。
思路:所以最低工资为888,它的上一层的工资就是889,所以可以逆向建图,利于计算每个员工的工资。
AC代码:
#include <bits/stdc++.h>
const int maxx=100010;
const int inf=0x3f3f3f3f;
using namespace std;
vector<int>path[maxx];
int sum[maxx];//记录每个员工的工资
int rudu[maxx];//记录每个员工的11入度
int n,m,x,y;
queue<int>q;
void topo()
{
for(int i=1; i<=n; i++)
{
if(rudu[i]==0)
{
q.push(i);
sum[i]=888;
rudu[i]--;
}
}
while(!q.empty())
{
int x=q.front();
q.pop();
for(int i=0; i<path[x].size(); i++)
{
rudu[path[x][i]]--;
if(rudu[path[x][i]]==0)
{
q.push(path[x][i]);
sum[path[x][i]]=sum[x]+1;
rudu[path[x][i]]--;
}
}
}
int flag=0;
int ans=0;
for(int i=1; i<=n; i++)
{
if(rudu[i]!=-1)
flag++;
}
if(flag)
printf("-1\n");
else
{
for(int i=1; i<=n; i++)
ans+=sum[i];
printf("%d\n",ans);
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
for(int i=0;i<n;i++)
path[i].clear();
memset(path,0,sizeof(path));
memset(sum,0,sizeof(sum));
memset(rudu,0,sizeof(rudu));
for(int i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
rudu[x]++;
path[y].push_back(x);
}
topo();
}
return 0;
}
标签:HDU,rewards,2647,demands,int,maxx,员工,奖励,Reward 来源: https://blog.csdn.net/weixin_43846139/article/details/100015125