• HDU 2647 Reward


    Reward

    Time Limit: 1000ms
    Memory Limit: 32768KB
    This problem will be judged on HDU. Original ID: 2647
    64-bit integer IO format: %I64d      Java class name: Main
     
     

    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

    Source

     
    解题:拓扑排序。
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <climits>
     7 #include <vector>
     8 #include <queue>
     9 #include <cstdlib>
    10 #include <string>
    11 #include <set>
    12 #include <stack>
    13 #define LL long long
    14 #define pii pair<int,int>
    15 #define INF 0x3f3f3f3f
    16 using namespace std;
    17 const int maxn = 10010;
    18 int n,m,in[maxn],money[maxn];
    19 vector<int>g[maxn];
    20 queue<int>q;
    21 int topSort() {
    22     int ans = 0;
    23     while(!q.empty()) q.pop();
    24     for(int i = 1; i <= n; i++) {
    25         if(!in[i]) {
    26             q.push(i);
    27             money[i] = 888;
    28         }
    29     }
    30     int sum = n;
    31     while(!q.empty()){
    32         sum--;
    33         int u = q.front();
    34         q.pop();
    35         for(int i = 0; i < g[u].size(); i++){
    36             if(--in[g[u][i]] == 0){
    37                 q.push(g[u][i]);
    38                 money[g[u][i]] = money[u]+1;
    39             }
    40         }
    41     }
    42     if(sum > 0) return -1;
    43     else{
    44         for(int i = 1; i <= n; i++)
    45             ans += money[i];
    46         return ans;
    47     }
    48 }
    49 int main() {
    50     int i,u,v;
    51     while(~scanf("%d %d",&n,&m)) {
    52         for(i = 1; i <= n; i++) {
    53             g[i].clear();
    54             in[i] = 0;
    55             money[i] = 0;
    56         }
    57         for(i = 0; i < m; i++) {
    58             scanf("%d %d",&v,&u);
    59             g[u].push_back(v);
    60             in[v]++;
    61         }
    62         printf("%d
    ",topSort());
    63     }
    64     return 0;
    65 }
    View Code
  • 相关阅读:
    hadoop集群默认配置和常用配置
    linux 修改目录文件权限,目录文件所属用户,用户组
    linux 修改目录文件权限,目录文件所属用户,用户组
    centos配置ssh免密码登录
    Hadoop的Python框架指南
    Python 调试工具
    Django 1.6 的测试驱动开发
    MongoDB学习笔记一:MongoDB的下载和安装
    Flash-制作空心文字
    Tomcat类载入器机制(Tomcat源代码解析六)
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/3945261.html
Copyright © 2020-2023  润新知