hdu 2647 Reward

来源:互联网 发布:高中数学补课软件 编辑:程序博客网 时间:2024/05/22 17:19

题目链接:点这里

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 11 22 21 22 1

Sample Output

1777-1

【题意】

现在老板要发工资,每个员工的基本工资都是888,不能少于这个数量,而且有人还有些特殊要求,也就是说有人要求工资比别人高。这样他才能高兴。然后问你让所有人开心的最小花费。如果不能让所有人开心那就输出-1.

【分析】

拓扑排序的问题,而且那个-1就是典型的排序失败的场景,然后每次找到入度为0的点加上当前的权值,然后删掉对应的边,依次下去,直到所有的点全算出来,如果点删完之后还有边的话就说明拓扑失败,那么直接输出-1就行。

【代码】

#include<iostream>#include<cstdio>#include<cstring>#include<string.h>#include<algorithm>#include<vector>#include<cmath>#include<stdlib.h>#include<time.h>#include<stack>#include<set>#include<map>#include<queue>#include<sstream>using namespace std;#define rep0(i,l,r) for(int i = (l);i < (r);i++)#define rep1(i,l,r) for(int i = (l);i <= (r);i++)#define rep_0(i,r,l) for(int i = (r);i > (l);i--)#define rep_1(i,r,l) for(int i = (r);i >= (l);i--)#define MS0(a) memset(a,0,sizeof(a))#define MS1(a) memset(a,-1,sizeof(a))#define MSi(a) memset(a,0x3f,sizeof(a))#define sin1(a) scanf("%d",&(a))#define sin2(a,b) scanf("%d%d",&(a),&(b))#define sll1(a) scanf("%lld",&(a))#define sll2(a,b) scanf("%lld%lld",&(a),&(b))#define sdo1(a) scanf("%lf",&(a))#define sdo2(a,b) scanf("%lf%lf",&(a),&(b))#define inf 0x3f3f3f3f#define lson i<<1,l,mid#define rson ((i<<1)|1),mid+1,r#define uint unsigned inttypedef pair<int,int> PII;#define A first#define B second#define pb push_back#define MK make_pair#define ll long longtemplate<typename T>void read1(T &m){    T x=0,f=1;    char ch=getchar();    while(ch<'0'||ch>'9')    {        if(ch=='-')f=-1;        ch=getchar();    }    while(ch>='0'&&ch<='9')    {        x=x*10+ch-'0';        ch=getchar();    }    m = x*f;}template<typename T>void read2(T &a,T &b){    read1(a);    read1(b);}template<typename T>void read3(T &a,T &b,T &c){    read1(a);    read1(b);    read1(c);}template<typename T>void out(T a){    if(a>9) out(a/10);    putchar(a%10+'0');}template<typename T>void outn(T a){    if(a>9) out(a/10);    putchar(a%10+'0');    puts("");}using namespace std;///---------------------------------------------------------------------------------const int maxm = 10005;vector<int>G[maxm];int in[maxm];int ans;void topo(int len){    rep0(i,0,len)    {        int now[maxm];        int flag=0;        rep1(j,1,len)        {            if(in[j]==0)            {                now[flag++]=j;                in[j]=-1;            }        }        ans+=flag*(888+i);        rep0(j,0,flag)        {            rep0(k,0,G[now[j]].size())            {                in[G[now[j]][k]]--;            }        }    }}int main(){//    freopen("in.txt","r",stdin);    int len,edge;    while(sin2(len,edge)!=EOF)    {        ans=0;        memset(in,0,sizeof(in));        for(int i=1;i<=len;i++)            G[i].clear();        int from,to;        while(edge--)        {            read2(from,to);            G[to].pb(from);            in[from]++;        }        topo(len);        bool flag=false;        rep1(i,1,len)        {            if(in[i]>0)                flag=true;        }        if(flag) puts("-1");        else            outn(ans);    }    return 0;}
原创粉丝点击