hdu2647 Reward 拓扑排序

  此题的关键在于分层次,最低一层的人的奖金是888,第二层是888+1 ……

  分层可以这样实现。建立反向图。在拓扑排序的时候,第一批入度为0的点就处于第一层,第二批处于第二层 ……

  由于是逐个遍历入度为0的点,所以怎么实现上面所说的第一批,第二批就需要动点脑。

  可以试试下面的测试数据:

4 3
1 3
2 3
4 3

4 3
1 2
2 3
4 3

4 2
1 2
3 4

6 3
1 2
3 4
5 6

测试结果依次是:  3555  3556  3554   5331

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 10100, M =20010;
struct node
{
    int to,  next;
};
node edge[M];
int ind[N], head[N],que[N],ans[N];
int iq,tot,t;
void topo(int n)
{
    int i,k,j=0,x,y;
    t=-1;x=0;
    for(i=1;i<=n;i++)
        if(ind[i]==0) que[j++]=i;
    ans[++t]=j-x;
    y=0;
    x=j;
    for(i=0;i<j;i++)
    {
        if(i==ans[t]+y)
        {
            ans[++t]=j-x;
            x=j;
            y=i;
        }

        int u=que[i];
        for(k=head[u]; k!=-1; k=edge[k].next)
        {
            ind[edge[k].to]--;
            if(ind[edge[k].to]==0)
                que[j++]=edge[k].to;
        }
    }
    iq=j;
}
void addedge(int i,int j)
{
    edge[tot].to=j;edge[tot].next=head[i];head[i]=tot++;
}
void init()
{
    tot=0;
    memset(ind,0,sizeof(ind));
    memset(head,-1,sizeof(head));
}
int main()
{
    //freopen("test.txt","r",stdin);
    int n,m,i,j,k;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        init();
        int flag=0;
        while(m--)
        {
            scanf("%d%d",&i,&j);
            if(i==j) flag=1;
            if(flag) continue;
            ind[i]++;
            addedge(j,i);
        }
        topo(n);
        if(iq<n||flag) printf("-1\n");
        else
        {
            int s=0;
            for(i=0;i<=t;i++) s+=ans[i]*(i+888);
            printf("%d\n",s);
        }
    }
    return 0;
}
时间: 2024-11-08 06:56:19

hdu2647 Reward 拓扑排序的相关文章

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(拓扑排序)

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

hdu 2647 Reward 拓扑排序。

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

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

hdu2647(拓扑排序)

这道题题意很简单,老板给员工发福利,有些员工要求自己的福利必须比某个人高,老板希望在满足所有人的要求下,总花费最小. 拓扑排序分层,反向建表,正向也可以,只不过计算稍微麻烦些,但更接近题意. 这道题我还是卡了一会的,一开始用下标模拟堆栈的方法wa了好多次,后来试着调用stl的栈,接着wa,才发现是自己的分层策略和栈的性质不相容. 我的分层策略是,若有个点,在删去一个边之后入度变为0,则这个点的层数为刚刚删掉的那条边的起点的层数加1. 使用这种策略加上用栈会在处理一些特殊情况是发生错误.后来改为用

HDU 2647 Reward 拓扑排序

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 about how

hdu 2647 Reward(拓扑排序+反图)

题目链接:https://vjudge.net/contest/218427#problem/C 题目大意: 老板要给很多员工发奖金, 但是部分员工有个虚伪心态, 认为自己的奖金必须比某些人高才心理平衡: 但是老板很人道, 想满足所有人的要求, 并且很吝啬,想画的钱最少 输入若干个关系 a b a c c b 意味着a 的工资必须比b的工资高 同时a 的工资比c高: c的工资比b高 当出现环的时候输出-1 #include<iostream> #include<cstring> #

HDU2647 Reward 【拓扑排序】

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

HDU2647(拓扑排序+反向建图)

题意不说了,说下思路. 给出的关系是a要求的工资要比b的工资多,由于尽可能的让老板少付钱,那么a的工资就是b的工资+1,可以确定关系为a>b,根据拓扑排序建边的原则是把"小于"关系看成有向边,那么我们可以建边v->u. #include <stdio.h> #include <string.h> #include <string> #include <iostream> #include <algorithm> #