hdu 2643 拓扑排序加优先队列

来源:互联网 发布:lol末日人工智能攻略 编辑:程序博客网 时间:2024/05/17 03:34

Reward

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


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
 

Author
dandelion
 

Source
曾是惊鸿照影来

加优先队列的拓扑排序,,,就不说啥了

#include<map>#include<vector>#include<cstdio>#include<iostream>#include<cstring>#include<string>#include<algorithm>#include<cmath>#include<stack>#include<queue>#include<set>#define inf 0x3f3f3f3f#define mem(a,x) memset(a,x,sizeof(a))using namespace std;typedef long long ll;typedef pair<int,int> pii;inline int in(){    int res=0;char c;    while((c=getchar())<'0' || c>'9');    while(c>='0' && c<='9')res=res*10+c-'0',c=getchar();    return res;}priority_queue<pii,vector<pii>,greater<pii> > q;vector<int> v[10006];int rd[10206];int money[10006];int main(){    int n,m;    while(~scanf("%d%d",&n,&m))    {        for(int i=0;i<=n;i++)        {            v[i].clear();        }        mem(rd,0);        for(int i=0;i<m;i++)        {            int t1=in(),t2=in();            v[t2].push_back(t1);            rd[t1]++;        }        mem(money,0);        for(int i=1;i<=n;i++)        {            if(rd[i]==0)q.push(pii(0,i));        }        while(!q.empty())        {            pii now=q.top();            q.pop();            int x=now.second;            for(int i=0;i<(int)v[x].size();i++)            {                int t=v[x][i];                money[t]=now.first+1;                rd[t]--;                if(rd[t]==0)q.push(pii(money[t],t));             }        }        bool ok=1;        for(int i=1;i<=n;i++)if(rd[i]>0)        {            ok=0;            break;        }        if(!ok)        {            puts("-1");            continue;        }        int ans=888*n;        for(int i=1;i<=n;i++)ans+=money[i];        cout<<ans<<endl;    }    return 0;}



0 0
原创粉丝点击