hdu 2647 Reward(拓扑排序+优先队列)

本文介绍了一个关于工厂老板如何在满足员工间奖励比较需求的同时,使用最少的资金进行奖励分配的问题。通过拓扑排序的方法,实现了对该问题的有效解决,并提供了完整的代码实现。
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
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#pragma comment(linker, "/stck:1024000000,1024000000")
#define lowbit(x) (x&(-x))
#define max(x,y) (x>=y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.1415926535897932384626433832
#define ios() ios::sync_with_stdio(true)
#define INF 0x3f3f3f3f
#define mem(a) ((a,0,sizeof(a)))
typedef long long ll;
vector<int>v[10006];
int vis[10006],degree[10006];
int n,m,x,y,cnt,flag;
struct node
{
    int x;
    int y;
    bool operator<(const node &a) const
    {
        return a.y<y;
    }
}ans,pos;
void toposort()
{
    priority_queue<node>q;
    for(int i=1;i<=n;i++)
    {
        if(!degree[i])
        {
            ans.x=i;
            ans.y=0;
            vis[i]=0;
            q.push(ans);
        }
    }
    while(!q.empty())
    {
        pos=q.top();
        q.pop();
        for(int i=0;i<v[pos.x].size();i++)
        {
            degree[v[pos.x][i]]--;
            if(!degree[v[pos.x][i]])
            {
                ans.x=v[pos.x][i];
                ans.y=pos.y+1;
                q.push(ans);
                vis[v[pos.x][i]]=pos.y+1;
            }
        }
    }
    cnt=flag=0;
    for(int i=1;i<=n;i++)
    {
        if(vis[i]==-1) flag=1;
        else cnt+=888+vis[i];
    }
    printf("%d\n",flag?-1:cnt);
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=1;i<=n;i++)
            v[i].clear();
        memset(vis,-1,sizeof(vis));
        memset(degree,0,sizeof(degree));
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&x,&y);
            v[y].push_back(x);
            degree[x]++;
        }
        toposort();
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/shinianhuanniyijuhaojiubujian/p/8994786.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值