• 杭电 2647 Reward (拓扑排序反着排)


    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 1
    1 2
    2 2
    1 2
    2 1

    Sample Output

    1777
    -1

    大意:
      农场主要发工资,但是有一定规则,有些人得工资要比一些人高,每个人工资最低是888.先输入两个整数n,m,代表工人的数量编号1~n和m种规则,接下来的m行每行有两个整数a,b表示a的工资比b高,输入最小钱数,若不存在输出-1。

    因为无法确定工资最高的人工资是多少,所以反着排序,让工资最小的在前面,输入a,b时记录b约束a,前驱为0的人工资为888,依次累加。

     1 #include<cstdio>
     2 #include<queue>
     3 #include<string.h>
     4 using namespace std;
     5 int n,m,i,j,num[20010],head[20010],mon[20010],sum,ans;
     6 struct stu
     7 {
     8     int to,next;
     9 }st[20010];
    10 void init()
    11 {
    12     memset(num,0,sizeof(num));
    13     memset(head,-1,sizeof(head));
    14     memset(mon,0,sizeof(mon));
    15     sum=0;
    16     ans=0;
    17 }
    18 void add(int a,int b)
    19 {
    20     st[i].to=b;
    21     st[i].next=head[a];
    22     head[a]=i;
    23     num[b]++;
    24 }
    25 void topo()
    26 {
    27     int i,j;
    28     queue<int>que;
    29     while(!que.empty())
    30     {
    31         que.pop();
    32     }
    33     for(i = 1 ; i <= n ; i++)
    34     {
    35         if(num[i] == 0)
    36         {
    37             mon[i]=888;
    38             que.push(i);
    39         }
    40     }
    41     while(!que.empty())
    42     {
    43         ans++;
    44         m=que.front();
    45         que.pop();
    46         sum+=mon[m];
    47         for(i = head[m] ; i != -1 ; i = st[i].next)
    48         {
    49             if(--num[st[i].to] == 0)
    50             {
    51                 que.push(st[i].to);
    52                 mon[st[i].to]=mon[m]+1;
    53             }
    54         }
    55     }
    56     if(ans == n)
    57         printf("%d
    ",sum);
    58     else
    59         printf("-1
    ");
    60 }
    61 int main()
    62 {
    63     int a,b;
    64     while(scanf("%d %d",&n,&m)!=EOF)
    65     {
    66         init();
    67         for(i = 0 ; i < m ; i++)
    68         {
    69             scanf("%d %d",&a,&b);
    70             add(b,a);
    71         }
    72         topo();
    73     }
    74     
    75 }
    
    
    
     
  • 相关阅读:
    电脑一族,打电脑时候的健康的坐姿
    根据时间戳,增量同步数据的解决办法
    写在前面
    《T-GCN: A Temporal Graph Convolutional Network for Traffic Prediction》 论文解读
    关于Graph Convolutional Network的初步理解
    图形学_opengl纹理映射
    推荐算法_CIKM-2019-AnalytiCup 冠军源码解读_2
    推荐算法_CIKM-2019-AnalytiCup 冠军源码解读
    leetcode_雇佣 K 名工人的最低成本(优先级队列,堆排序)
    图形学_Bezier曲线
  • 原文地址:https://www.cnblogs.com/yexiaozi/p/5743295.html
Copyright © 2020-2023  润新知