zoukankan      html  css  js  c++  java
  • HDU——T 2647 Reward

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

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


    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 1 1 2 2 2 1 2 2 1
     
    Sample Output
    1777 -1
     
    Author
    dandelion
     
    Source
     
    Recommend
    yifenfei   |   We have carefully selected several similar problems for you:  1285 1811 2680 2112 2094 
     
    记录每一层的人数,统计答案。。。
    唉,蒟蒻就是弱、、边的方向搞错了调半天。。。
     1 #include <algorithm>
     2 #include <cstring>
     3 #include <cstdio>
     4 #include <queue>
     5 
     6 using namespace std;
     7 
     8 const int N(100000+5);
     9 const int M(200000+5);
    10 int sumedge,head[N];
    11 struct Edge
    12 {
    13     int v,next;
    14     Edge(int v=0,int next=0):v(v),next(next){}
    15 }edge[M];
    16 inline void ins(int u,int v)
    17 {
    18     edge[++sumedge]=Edge(v,head[u]);
    19     head[u]=sumedge;
    20 }
    21 
    22 queue<int>que;
    23 int rd[N],ans,cnt,sum[N];
    24 
    25 inline void init()
    26 {
    27     sumedge=0;
    28     for(;!que.empty();) que.pop();
    29     memset(rd,0,sizeof(rd));
    30     memset(sum,0,sizeof(sum));
    31     memset(edge,0,sizeof(edge));
    32     memset(head,0,sizeof(head));
    33 }
    34 
    35 int AC()
    36 {
    37     for(int n,m,if_;~scanf("%d%d",&n,&m);init())
    38     {
    39         if_=n;ans=0;cnt=1;
    40         for(int u,v;m--;)
    41         {
    42             scanf("%d%d",&u,&v);
    43             ins(v,u); rd[u]++;
    44         }
    45         for(int i=1;i<=n;i++)
    46             if(!rd[i]) que.push(i);
    47         for(int u,v;!que.empty();)
    48         {
    49             if_--;ans+=888;
    50             u=que.front(); que.pop();
    51             for(int v,i=head[u];i;i=edge[i].next)
    52             {
    53                 v=edge[i].v;
    54                 if(--rd[v]==0) 
    55                     sum[v]=sum[u]+1,que.push(v);
    56             }
    57         }
    58         for(int i=1;i<=n;i++) ans+=sum[i];
    59         if(if_) puts("-1");
    60         else printf("%d
    ",ans);
    61     }
    62     return 0;
    63 }
    64 
    65 int I_want_AC=AC();
    66 int main(){;}
    ——每当你想要放弃的时候,就想想是为了什么才一路坚持到现在。
  • 相关阅读:
    SystemV和BSD的区别
    Linux init 系列一 System V风格
    ps -ef和ps aux的区别
    Linux中/etc/resolv.conf文件简析
    Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=stoc
    在Linux中利用Service命令添加系统服务及开机自启动
    实例具体解释Django的 select_related 和 prefetch_related 函数对 QuerySet 查询的优化(二)
    关于程序性能优化基础的一些个人总结
    C++开发人脸性别识别总结
    double型转换成string型
  • 原文地址:https://www.cnblogs.com/Shy-key/p/7421987.html
Copyright © 2011-2022 走看看