hdu2647

来源:互联网 发布:淘宝减肥产品哪个好 编辑:程序博客网 时间:2024/06/06 23:17

拓扑 水题


http://acm.hdu.edu.cn/showproblem.php?pid=2647

Reward

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


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
#include <iostream>#include <queue>#include <stdio.h>#include <stdlib.h>#include <string.h>using namespace std;const int M=10005;struct Node{    int data;    Node *next;}*e[M];///!!!int ind[M],add[M];void addEdage(int u,int v){    Node *t=new Node;///!!!    t->data=u;    t->next=e[v];///定义成指针型的才能来回=    e[v]=t;///}int main(){    int v,u,n,m;    queue<int>que;    while(cin>>n>>m)    {        memset(e,0,sizeof(e));        memset(ind,0,sizeof(ind));        while(m--)        {            cin>>u>>v;            addEdage(u,v);            ind[u]++;        }        for(int i=1; i<=n; i++)            add[i]=888;        for(int i=1; i<=n; i++)        {            if(ind[i]==0)                que.push(i);        }        int count=0,sum=0;        while(!que.empty())        {            int t=que.front();            sum+=add[t];            count++;            que.pop();            Node *hh;            for(hh=e[t]; hh; hh=hh->next)///            {                if(--ind[hh->data]==0)                {                    que.push(hh->data);                    add[hh->data]=add[t]+1;                }            }        }        if(count==n)            cout<<sum<<endl;        else            cout<<"-1"<<endl;    }    return 0;}


0 0
原创粉丝点击