zoukankan      html  css  js  c++  java
  • hdu 2647 Reward

    题目连接

    http://acm.hdu.edu.cn/showproblem.php?pid=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 leq 10000,m leq 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

    简单的拓扑排序。。

     1 #include<algorithm>
     2 #include<iostream>
     3 #include<cstdlib>
     4 #include<cstring>
     5 #include<cstdio>
     6 #include<vector>
     7 #include<queue>
     8 #include<map>
     9 using std::cin;
    10 using std::cout;
    11 using std::endl;
    12 using std::find;
    13 using std::sort;
    14 using std::map;
    15 using std::pair;
    16 using std::queue;
    17 using std::vector;
    18 using std::multimap;
    19 #define pb(e) push_back(e)
    20 #define sz(c) (int)(c).size()
    21 #define mp(a, b) make_pair(a, b)
    22 #define all(c) (c).begin(), (c).end()
    23 #define iter(c) decltype((c).begin())
    24 #define cls(arr,val) memset(arr,val,sizeof(arr))
    25 #define cpresent(c, e) (find(all(c), (e)) != (c).end())
    26 #define rep(i, n) for (int i = 0; i < (int)(n); i++)
    27 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
    28 const int N = 10010;
    29 typedef unsigned long long ull;
    30 struct Node {
    31     int vex, val;
    32     Node(int i = 0, int j = 0) :vex(i), val(j) {}
    33 };
    34 struct TopSort {
    35     vector<int> G[N];
    36     int topNum, inq[N];
    37     inline void init(int n) {
    38         topNum = 0;
    39         cls(inq, 0);
    40         rep(i, n) G[i].clear();
    41     }
    42     inline void built(int m) {
    43         int a, b;
    44         rep(i, m) {
    45             scanf("%d %d", &a, &b);
    46             --a, --b;
    47             inq[a]++;
    48             G[b].push_back(a);
    49         }
    50     }
    51     inline void bfs(int n) {
    52         int ans = 0;
    53         queue<Node> q;
    54         rep(i, n) {
    55             if (!inq[i]) { q.push(Node(i, 888)); topNum++; }
    56         }
    57         while (!q.empty()) {
    58             Node t = q.front(); q.pop();
    59             ans += t.val;
    60             rep(i, sz(G[t.vex])) {
    61                 if (--inq[G[t.vex][i]] == 0) q.push(Node(G[t.vex][i], t.val + 1)), topNum++;
    62             }
    63         }
    64         printf("%d
    ", topNum == n ? ans : -1);
    65     }
    66 }work;
    67 int main() {
    68 #ifdef LOCAL
    69     freopen("in.txt", "r", stdin);
    70     freopen("out.txt", "w+", stdout);
    71 #endif
    72     int n, m;
    73     while (~scanf("%d %d", &n, &m)) {
    74         work.init(n);
    75         work.built(m);
    76         work.bfs(n);
    77     }
    78     return 0;
    79 }
    View Code
    By: GadyPu 博客地址:http://www.cnblogs.com/GadyPu/ 转载请说明
  • 相关阅读:
    pandas 数据结构基础与转换
    Python 基础常用
    css 横向滚动条webkit-scrollbar
    hive mysql 初始化
    hive 的理解
    hive 踩坑
    hbase 调试各种报错
    hbase shell常用命令
    mysql 性能测试工具 mysqlslap
    【CDH学习之一】CDH简介
  • 原文地址:https://www.cnblogs.com/GadyPu/p/4637069.html
Copyright © 2011-2022 走看看