Reward Hangzhou Electric 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
 

 

Sample Output
1777
-1
Subject to the effect: the boss wages, the basic wage is 888, requiring the person in front than in the back of many people, at least ask how much can be made. If the output can not meet the requirements -1
Ideas: first determine the topological sorting ,, there is no ring, there is a ring, then output -1, I started thinking about the money and more people in front of the result should be the same libido, and behind the back of the human person than in front of people. . but. . wa N times.
The subject has two pits: the prime minister is likely to equal wages for 2 people, the second is the need to come forward. Since the first individual wages based wage ,, That might be more than one person is the basis of wages. . So wages must Descending
AC Code:
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<vector>
using namespace std;
typedef long long ll;
const int N=1E5+7;
vector<int >ve[N];
int re[N];
int in[N];
int main(){
    int n,m;
    while(~scanf("%d%d",&n,&m)){
        memset(re,0,sizeof(re));
        memset(in,0,sizeof(in));
        queue<int >que;
        
        for(int i=1;i<=m;i++){
            int x,y;
            scanf("%d%d",&x,&y);
            in[x]++;
            ve[y].push_back(x);
        }
        
        for(int i=1;i<=n;i++){
            if(in[i]==0){
                que.push(i);
            }
        }
        
        int n1=0;
        ll sum=0;
        while(que.size()){
            int xx=que.front();
            que.pop();
            sum+=888+re[xx];
            n1++;
            for(int i=0;i<ve[xx].size();i++){
                in[ve[xx][i]]--;
                if(in[ve[xx][i]]==0){
                    re[ve[xx][i]]=re[xx]+1;
                    que.push(ve[xx][i]);
                }
            }
        }
        if(n1!=n){
            puts("-1");
        }
        else {
            printf("%lld\n",sum);
        }
        for(int i=1;i<=n;i++){
            ve[i].clear();
        }
    }
    return 0;
}

 

 
 

Guess you like

Origin www.cnblogs.com/Accepting/p/11314515.html