HDU 1811 拓扑排序

Reward

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9852    Accepted Submission(s): 3147


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

题意:就是每人至少有888元,有些人则要比他的上司(上一级)更多奖金,其实就是加一元钱。只需要反向建图。


ac code:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <iterator>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
using namespace std;

#define si1(a) scanf("%d",&a)
#define si2(a,b) scanf("%d%d",&a,&b)
#define sd1(a) scanf("%lf",&a)
#define sd2(a,b) scanf("%lf%lf",&a,&b)
#define ss1(s)  scanf("%s",s)
#define pi1(a)    printf("%d\n",a)
#define pi2(a,b)  printf("%d %d\n",a,b)
#define mset(a,b)   memset(a,b,sizeof(a))
#define forb(i,a,b)   for(int i=a;i<b;i++)
#define ford(i,a,b)   for(int i=a;i<=b;i++)

typedef long long LL;
const int N=1e5+5;
const int M=6666666;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
const double eps=1e-7;
vector <int > V[12000];
int dge[12000];
int d[12000];
bool vis1[12000];
queue<int > q;

int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        LL ans=0;
        for(int i=0;i<=n;i++)
            V[i].clear();
        while(!q.empty()) q.pop();
        mset(vis1,0);
        mset(dge,0);
        mset(d,0);
        int u,v,sum=0;
        for(int i=0;i<m;i++)
        {
            si2(u,v);
            dge[u]++;
            V[v].push_back(u);
            if(!vis1[u])sum++,vis1[u]=1;
            if(!vis1[v])sum++,vis1[v]=1;
        }
        for(int i=1;i<=n;i++)
        {
            if(!dge[i]&&vis1[i])
                q.push(i);
        }
        int flag=0,num=0;
        if(q.empty()) flag=1;
        int cnt=0,tmp=0;
        while(!q.empty())
        {
            u=q.front();q.pop();
            for(int i=0;i<V[u].size();i++)
            {
                v=V[u][i];
                if(!--dge[v])
                {
                    q.push(v);
                }
                d[v]=max(d[v],d[u]+1);
            }

            ans+=d[u];
            num++;
        }
        //printf("%lld\n",ans);
        if(num!=sum) flag=1;
        if(flag)
            printf("-1\n");
        else
        {
            ans=n*888+ans;
            printf("%lld\n",ans);
        }
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值