hdu 2647 还是逆向拓扑

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
嗯。,。 这道题目是要统计费用 然后为了方便统计层数上的钱数  需要逆向构图
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 11000    
#include<vector>
#include<iostream>
using namespace std;//拓扑排序的分层问题 
int m,n,degree[maxn];
vector<int> mapp[maxn];// 
int topo()
{
    int flag,sum=0; 
    vector<int> ans;
    int tt=0,re=0;//tt表示层数
    while(1)
    {
        flag=0;
        ans.clear();
        for(int i=1;i<=m;i++) if(degree[i]==0)//每次得处理一层的 /./
        {
            ans.push_back(i);
            sum+=(888+tt);
            degree[i]=-1;
            flag=1;
            re++;
        } 
        if(re>=m) return sum;//当遍历的点数到达m return
        if(flag==0) return 0;
        for(int j=0;j<ans.size();j++)
        {
            int rett=ans[j];
            for(int k=0;k<mapp[rett].size();k++)
            {
                int temp=mapp[rett][k];
                degree[temp]--;
            }
        }
        tt++;
    }
    return sum;
}
int main()
{
    int a,b,i,j;
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        if(m==0&&n==0) break;
        memset(degree,0,sizeof(degree));
        for(i=0;i<=m;i++) mapp[i].clear();
        for(i=0;i<n;i++)
        {
            scanf("%d%d",&a,&b);
            mapp[b].push_back(a);
            degree[a]++;  
        }
        int t=topo();
        if(t==0) printf("-1\n");
        else printf("%d\n",t);
    }
    return 0;
}

转载于:https://www.cnblogs.com/z1141000271/p/5798053.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值