hdu 2647 Reward

简介:

click here ~~

                         Reward
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5763    Accepted Submission(s): 1756



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


AI 代码解读

题目大意:老板要给很多员工发奖金, 但是部分员工有个虚伪心态, 认为自己的奖金必须比某些人高才心理平衡; 但是老板很人道, 想满足所有人的要求, 并且很吝啬,想画的钱最少

输入若干个关系:
a b
a c
c b
意味着a 的工资必须比b的工资高 同时a 的工资比c高; c的工资比b高

当出现环的时候输出-1
解题思路:拓扑排序,注意初始化,上代码:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 20010;

int head[maxn], ip, indeg[maxn];
int n, m, seq[maxn];

struct note
{
    int v,next;
} edge[maxn];

void Init()
{
    memset(head, -1, sizeof(head));
    memset(indeg, 0, sizeof(indeg));
    ip = 0;
}

void addedge(int u, int v)//增加边
{
    edge[ip].v = v;
    edge[ip].next = head[u];
    head[u] = ip++;
}

int topo()//拓扑排序
{
    queue<int> q;
    for(int i=1; i<=n; i++)
        if(indeg[i] == 0)
            q.push(i);
    int k = 0;
    bool res = false;
    while(!q.empty())
    {
        if(q.size() != 1)
            res = true;
        int u = q.front();
        q.pop();
        k++;
        for(int i=head[u]; i!=-1; i=edge[i].next)
        {
            int v = edge[i].v;
            indeg[v]--;
            if(indeg[v] == 0)
            {
                seq[v] = seq[u]+1;
                q.push(v);
            }
        }
    }
    if(k < n) return -1;
    if(res) return 0;
    return 1;
}
int main()
{
    int u, v;
    while(cin>>n>>m)
    {
        Init();
        memset(seq, 0, sizeof(seq));//初始化
        while(m--)
        {
            cin>>u>>v;
            addedge(v,u);
            indeg[u]++;
        }
        if(topo() >= 0)
        {
            int ans=0;
            for(int i=1; i<=n; i++)
                ans += seq[i];
            ans += 888*n;
            printf("%d\n",ans);
        }
        else
            puts("-1");
    }
    return 0;
}
AI 代码解读
目录
相关文章
HDU 2084 数塔
在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的: 有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少?
191 0
HDU 2669 Romantic
题意:找出最小的非负整数X,使之满足式子X*a + Y*b = 1。
128 0
HDOJ/HDU 2568 前进(简单题)
Problem Description 轻松通过墓碑,进入古墓后,才发现里面别有洞天。 突然,Yifenfei发现自己周围是黑压压的一群蝙蝠,个个扇动翅膀正准备一起向他发起进攻! 形势十分危急! 好在此时的yifenfei已经不是以前那个经常被lemon抢走MM的菜鸟了!面对众多蝙蝠的嗜血狂攻,只见yifenfei使出轻灵的剑法,刷,刷,刷,瞬间搞定…… 现已知yifenfei使用了2招(剑招A和剑招B):剑招A,一招能杀死一半的蝙蝠。
999 0