zoukankan      html  css  js  c++  java
  • hdu 2647 Reward (topsort)

    Reward
    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 13132    Accepted Submission(s): 4199

    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

    CC++:

     1 #include <map>
     2 #include <queue>
     3 #include <cmath>
     4 #include <vector>
     5 #include <string>
     6 #include <cstdio>
     7 #include <cstring>
     8 #include <climits>
     9 #include <iostream>
    10 #include <algorithm>
    11 #define INF 0xffffff
    12 using namespace std;
    13 
    14 const int my_max = 20010;
    15 int n, m, a, b, my_cnt, my_indeg[my_max];
    16 vector <int> my_G[my_max];
    17 
    18 int topsort()
    19 {
    20     my_cnt = 0;
    21     int my_ans = 0, t = 887;
    22     queue <int> Q;
    23     while (t ++)
    24     {
    25         int flag = -1, my_temp = 0;
    26         for (int i = 1; i <= n; ++ i)
    27             if (my_indeg[i] == 0)
    28             {
    29                 my_indeg[i] = -1;
    30                 flag = 1;
    31                 Q.push(i);
    32                 my_cnt ++;
    33                 my_temp ++;
    34             }
    35 
    36         if (flag == -1) break;
    37         my_ans += my_temp * t;
    38 
    39         while (my_temp --)
    40         {
    41             int my_now = Q.front();
    42             for (int i = 0; i < my_G[my_now].size(); ++ i)
    43                 my_indeg[my_G[my_now][i]] --;
    44             my_G[my_now].clear();
    45             Q.pop();
    46         }
    47     }
    48     if (my_cnt == n)
    49         return my_ans;
    50 
    51     for (int i = 1; i <= n; ++ i)
    52         my_G[i].clear();
    53     return -1;
    54 }
    55 
    56 int main()
    57 {
    58     while (~scanf("%d%d", &n, &m))
    59     {
    60         memset(my_indeg, 0, sizeof(my_indeg));
    61         while (m --)
    62         {
    63             scanf("%d%d", &a, &b);
    64             ++ my_indeg[a];
    65             my_G[b].push_back(a);
    66         }
    67 
    68         printf("%d
    ", topsort());
    69     }
    70     return 0;
    71 }
  • 相关阅读:
    Swap Nodes in Pairs
    Search for a Range——稍微升级版的二分查找
    Set Matrix Zeroes——常数空间内完成
    Ubuntu系统---C++之Eclipse 开始工程项目
    Ubuntu系统---C++之Eclipse编译器 CDT插件安装
    Ubuntu系统---开机总会显示错误报告处理
    Ubuntu系统---C++之Eclipse IDE 编译器安装
    Ubuntu系统---终端下用g++进行c++项目
    Ubuntu系统---进行C++项目开发的工具
    YOLO---Darknet下的 GPU vs CPU 速度
  • 原文地址:https://www.cnblogs.com/GetcharZp/p/9458015.html
Copyright © 2011-2022 走看看