HDU 2647 拓扑排序

Reward

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


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

题意:老板要给员工发工资,每个人发888,但是有些人想比别人多,因此老板希望用最少的钱满足所有的条件。

解题思路:把每个人都做一个结点,把给定的关系连边,,拓扑排序判断是矛盾,假设不矛盾的话就给每个人赋值,假设要求要多,就+1,。

代码:

 

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string>
#include<string.h>
#include<queue>
using namespace std;
const maxn=10010;
int in[maxn],head[maxn],tol,num[maxn],cnt,m,n,weight[maxn];
struct node
{
        int next,to;
}edge[100000];
void add(int u,int v)
{
        edge[tol].to=v;
        edge[tol].next=head[u];
        head[u]=tol++;
}
int topo()
{
        int i,j,k;
        cnt=0;
        int ans=0;
        queue<int> q;
        for(i=0;i<n;i++)
        if(in[i]==0)
        {
                q.push(i);
                weight[i]=888;
        }
        while(!q.empty())
        {
                int u=q.front();q.pop();
                num[cnt++]=u;
                in[u]--;
                for(i=head[u];i!=-1;i=edge[i].next)
                {
                        int v=edge[i].to;
                        if(--in[v]==0)
                        {
                                q.push(v);
                                weight[v]=weight[u]+1;
                        }
                }
        }
        //cout<<cnt<<endl;
        if(cnt<n)return -1;
        for(i=0;i<n;i++)ans+=weight[i];
        return ans;
}
int main()
{
        int i,j,k;
        while(~scanf("%d%d",&n,&m))
        {
                memset(head,-1,sizeof(head));tol=0;
                memset(weight,0,sizeof(weight));
                memset(in,0,sizeof(in));
                while(m--)
                {
                      scanf("%d%d",&i,&j);
                      i--;j--;
                      add(j,i);
                      in[i]++;
                }
                //for(i=0;i<n;i++)cout<<in[i]<<" ";cout<<endl;
                printf("%d\n",topo());
        }
        return 0;
}

 

posted @ 2013-09-08 23:29  线性无关  阅读(98)  评论(0)    收藏  举报