Reward

来源:互联网 发布:淘宝高质量流量 编辑:程序博客网 时间:2024/06/07 20:56

Reward

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 40   Accepted Submission(s) : 13
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
 
a 比b的工资高,可以理解为先给b发工资再给a发。并且a的工资是a的入度中的最大值加1。所以每次选出入度为0的点就更新她的出度点。
注意下面的代码块:

      for(int j = head[u]; edge[j].x == u && j < (int)edge.size(); j ++)      {          cost[edge[j].y] = max(cost[u] + 1 , cost[edge[j].y]);          indegree[edge[j].y] --;      }



#include <iostream>#include <cstdio>#include <vector>#include <queue>#include <algorithm>using namespace std;int cost[10010];int n , m ;typedef struct{   int x ;   int y ;}node ;vector<node>edge;int vis[10010];int head[10010];int indegree[10010];void init(){    edge.clear();    for(int i = 0; i < 10010; i ++){        head[i] = -1;        indegree[i] = 0;        cost[i] = 0;        vis[i] = 0;    }}bool cmp(node a , node b){    if(a.x == b.x) return a.y < b.y;    return a.x < b.x;}__int64 tupo(){  int u ;  for(int i = 0; i < n; i ++)  {      u = -1;      int flag = 0;      for(int j = 1; j <= n; j ++)      {          if(!vis[j]) flag = 1;          if(!vis[j] && indegree[j] == 0)          {              u = j; break;          }      }      if(flag && u == -1) return -1;      vis[u] = 1;      for(int j = head[u]; edge[j].x == u && j < (int)edge.size(); j ++)      {          cost[edge[j].y] = max(cost[u] + 1 , cost[edge[j].y]);          indegree[edge[j].y] --;      }  }  __int64 sum = 0;  for(int i = 1; i <= n; i ++)    sum += cost[i];    sum += n * 888;    return sum;}int main(){    node temp;    while(scanf("%d%d",&n,&m)!=EOF)    {        init();        for(int i = 0; i < m; i ++)        {            scanf("%d%d",&temp.y,&temp.x);            edge.push_back(temp);            indegree[temp.y] ++;        }        sort(edge.begin() , edge.end() , cmp);        head[edge[0].x] = 0;        for(int i = 1; i < (int)edge.size(); i ++)        {            if(edge[i].x != edge[i-1].x)                head[edge[i].x] = i;        }        printf("%I64d\n",tupo());    }    return 0;}


0 0
原创粉丝点击