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

Author

dandelion

Source

曾是惊鸿照影来

Recommend

yifenfei


题目大意

老板给员工发工资,工资由初始工资与奖金组成。初始工资为888元,奖金为任意非负整数。
有些员工比较奇怪,认为自己的工资需要比别人的工资高,老板要在满足每个人的前提下分出的钱最少。求至少多少钱。

多组数据若无解输出-1

solution

  • 拓扑排序就好了..

  • 如果 a 认为他的工资要比 b 高,那就连一条由 b 指向 a 的边

  • 这样建图以后,一个人的工资就是指向他的所有人的工资的最大值+1

  • 然后就是裸的拓扑排序了

code

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;

template<typename T>
void input(T &x) {
    x=0; T a=1;
    register char c=getchar();
    for(;c<'0'||c>'9';c=getchar())
        if(c=='-') a=-1;
    for(;c>='0'&&c<='9';c=getchar())
        x=x*10+c-'0';
    x*=a;
    return;
}

template<typename Type>
struct Stack {
    static const int MAXN=10010;
    Type a[MAXN]; int top;
    Stack() { top=0; }
    void push(Type x) { a[++top]=x;return; }
    void pop() { top--;return; }
    Type Top() { return a[top]; }
    bool empty() { return !top; }
};

Stack<int> s;

#define MAXN Stack<int>::MAXN
#define MAXM 20010

struct Edge {
    int u,v,next;
    Edge(int u=0,int v=0,int next=0):
        u(u),v(v),next(next) {}
};

Edge edge[MAXM];
int head[MAXN],cnt;

void addedge(int u,int v) {
    edge[++cnt]=Edge(u,v,head[u]);
    head[u]=cnt;
    return;
}

int n,m;
int indeg[MAXN];
int price[MAXN];

int topo() {
    int flag=0,ans=0;
    for(int i=1;i<=n;i++) {
        price[i]=888;
        if(indeg[i]==0) {
            flag++;
            s.push(i);
            ans+=price[i];
        }
    }
    while(!s.empty()) {
        int u=s.Top();
        s.pop();
        for(int i=head[u];i;i=edge[i].next) {
            int v=edge[i].v;
            indeg[v]--;
            if(price[u]+1>price[v])
                price[v]=price[u]+1;
            if(indeg[v]==0) {
                flag++;
                s.push(v);
                ans+=price[v];
            }
        }
    }
    if(flag!=n) return -1;
    return ans;
}

int main() {
    while(~scanf("%d%d",&n,&m)) {
        cnt=0;
        for(int i=1;i<=n;i++)
            indeg[i]=head[i]=0;
        for(int i=1;i<=m;i++) {
            int u,v;
            input(v),input(u);
            addedge(u,v);
            indeg[v]++;
        }
        printf("%d\n",topo());
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值