HD--2647 Reward

来源:互联网 发布:华讯网络奖金能拿多少 编辑:程序博客网 时间:2024/05/17 03:52

Reward

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


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 <cstdio>  #include <cstring>  #include <cmath>  #include <queue>  #define MAX 20000+10  using namespace std;  struct record  {      int to, next;  }edge[MAX];  int head[MAX];  int in[MAX];//记录入度   int reward[MAX];//存储奖金   queue<int >q;  int n, m;  void build()  {      int x, y;      for(int i = 0; i < m; i++)      {          scanf("%d%d", &x, &y);          edge[i].to = x;//反向 建           edge[i].next = head[y];//记录对应边 的序号  若没有为-1           head[y] = i;          in[x]++;      }  }  void toposort()  {      int i, j;      int node;//当前节点      int sum = 0;//处理数  每处理一个节点 该点出队列       for(i = 1; i <= n; i++)      {          if(in[i] == 0)//入度为0的点 进队列           {              reward[i] = 888;              q.push(i);          }       }       while(!q.empty())       {          node = q.front();          q.pop();          sum++;//每次自增一           for(i = head[node]; i != -1; i = edge[i].next)//处理完当前node对应边 继续next对应边           {              --in[edge[i].to];//入度自减一               if(in[edge[i].to] == 0)              {                  reward[edge[i].to] = reward[node] + 1;                  q.push(edge[i].to);              }          }       }      if(sum != n)//存在某个人的要求不能满足      printf("-1\n");      else      {          int ans = 0;          for(i = 1; i <= n; i++)          {              ans += reward[i];          }          printf("%d\n", ans);      }   }   int main()  {      while(~scanf("%d%d", &n, &m))      {          memset(head, -1, sizeof(head));//初始化 因为边序号是从0开始的            memset(in, 0, sizeof(in));          build();//建立序列           toposort();//反向拓扑       }      return 0;  }   


0 0
原创粉丝点击