zoukankan      html  css  js  c++  java
  • 杭电 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 }
    
    
    
     
  • 相关阅读:
    node sql语句占位符用法(防sql注入攻击)
    请求node服务的静态资源请求不到
    node实现图片上传功能
    前端代码静态资源存阿里云
    git代码提交
    vue处理后台返回流的形式的文件下载
    20172305 2018-2019-1 《Java软件结构与数据结构》第五周学习总结
    20172305 2018-2019-1 《Java软件结构与数据结构》第四周学习总结
    20172305 2018-2019-1 《程序设计与数据结构》实验一报告
    20172305 2018-2019-1 《Java软件结构与数据结构》第三周学习总结
  • 原文地址:https://www.cnblogs.com/yexiaozi/p/5743295.html
Copyright © 2011-2022 走看看