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


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

输入若干个关系:
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;
}
目录
相关文章
|
Java 文件存储
hdu1128 Self Numbers
hdu1128 Self Numbers
43 0
|
算法 Java
HDU 2084 数塔
在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的: 有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少?
183 0
|
Java 人工智能 Windows
|
Java 测试技术
HDU 1232 畅通工程
畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 50540    Accepted Submission(s): 26968 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇。
1026 0
|
Java 测试技术
HDU 1847
Good Luck in CET-4 Everybody! Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3204    Accepted Submission(s): 2008 Problem Description 大学英语四级考试就要来临了,你是不是在紧张的复习?也许紧张得连短学期的ACM都没工夫练习了,反正我知道的Kiki和Cici都是如此。
857 0

热门文章

最新文章