拓扑排序 HDU 2647

注意顺序

Reward

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 4289    Accepted Submission(s): 1311

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 <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <limits.h>
#include <ctype.h>
#include <string.h>
#include <string>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <deque>
#include <vector>
#include <set>
#include <map>
using namespace std;
#define MAXN 10011
int money[MAXN];
int head[MAXN];
int into[MAXN];

struct node{
    int to;
    int next;
}edge[20010];

int main(){
    int n,m;
    int i,j;

    while(~scanf("%d%d",&n,&m)){
        for(i=1;i<=n;i++){
            money[i] = 888;
        }
        memset(into,0,sizeof(into));
        memset(head,-1,sizeof(head));
        int k = 0;
        while(m--){
            scanf("%d%d",&j,&i);
            edge[k].to = j;
            edge[k].next = head[i];
            head[i] = k++;
            into[j]++;
        }
        queue<int>t;
        for(i=1;i<=n;i++){
            if(into[i] == 0){
                t.push(i);
            }
        }
        int num = 0;
        int ans = 0;
        while(!t.empty()){
            int u = t.front();
            ans+=money[u];
            t.pop();
            num++;
            for(k=head[u];k!=-1;k=edge[k].next){
                if(--into[edge[k].to] == 0){//--的位置注意
                    t.push(edge[k].to);
                    money[edge[k].to] = money[u] + 1;
                }
                //t.push(edge[k].to);
                //money[edge[k].to] = money[u] + 1;
            }
        }
        if(num != n){
            ans = -1;
        }
        printf("%d\n",ans);
    }

    return 0;
}
时间: 2024-11-05 13:48:40

拓扑排序 HDU 2647的相关文章

拓扑排序 - hdu 1285

2017-09-12 19:50:58 writer:pprp 最近刚开始接触拓扑排序,拓扑排序适用于:无圈图的顶点的一种排序, 用来解决有优先级别的排序问题,比如课程先修后修,排名等. 主要实现:用矩阵来储存图,用indegree数组记录每个顶点的入度, 从入度为0的开始,每次删除该入度为0的点,然后修改其他顶点的入度, 在进行查找入度为0的顶点,循环下去就可以 题意如下:给你n个队伍,m个优先顺序,让你输出总的排名的优先顺序. 代码如下: /* @theme:拓扑排序 hdu 1285 @w

拓扑排序 HDU - 5695

众所周知,度度熊喜欢各类体育活动. 今天,它终于当上了梦寐以求的体育课老师.第一次课上,它发现一个有趣的事情.在上课之前,所有同学要排成一列, 假设最开始每个人有一个唯一的ID,从1到NN,在排好队之后,每个同学会找出包括自己在内的前方所有同学的最小ID,作为自己评价这堂课的分数.麻烦的是,有一些同学不希望某个(些)同学排在他(她)前面,在满足这个前提的情况下,新晋体育课老师--度度熊,希望最后的排队结果可以使得所有同学的评价分数和最大. Input第一行一个整数TT,表示T(1≤T≤30)T(

拓扑排序 HDU 1285 确定比赛名次

确定比赛名次 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15037    Accepted Submission(s): 6022 Problem Description 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直

拓扑排序 HDU 3342

Legal or Not Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4555    Accepted Submission(s): 2072 Problem Description ACM-DIY is a large QQ group where many excellent acmers get together. It is

hdu 2647 Reward (拓扑排序分层)

Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3815    Accepted Submission(s): 1162 Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wa

hdu 2647 (拓扑排序 邻接表建图的模板) Reward

题目链接http://acm.hdu.edu.cn/showproblem.php?pid=2647 老板给员工发工资,每个人的基本工资都是888,然后还有奖金,然后员工之间有矛盾,有的员工希望比某员工的奖金多,老板满足了所以员工的这种心思,而且老板下午发的总工资最少,问最少是多少?比如 a b 表示a的工资比b要高(高一块钱),当出现a b   b c   c a这种环的时候输出-1 拓扑排序http://www.cnblogs.com/tonghao/p/4721072.html 小指向大

HDU 2647 Reward(图论-拓扑排序)

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 ,a

ACM: hdu 2647 Reward -拓扑排序

hdu 2647 Reward Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u 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

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