zoukankan      html  css  js  c++  java
  • Reward HDU

     
     
    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. 

    InputOne 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.OutputFor 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


    题意:输入两个数n,m。n表示公司人数,m表示不同人之间的关系。a b表示a的工资大于b.
    题解:拓扑排序,找入度为0的点,所有的拓扑完没有剩余,说明没有环

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<cstring>
    #include<sstream>
    #include<cmath>
    #include<stack>
    #include<map>
    #include<cstdlib>
    #include<vector>
    #include<string>
    #include<queue>
    using namespace std;
    
    #define ll long long
    #define llu unsigned long long
    #define INF 0x3f3f3f3f
    const double PI = acos(-1.0);
    const int maxn =  1e4+10;
    const int mod = 1e9+7;
    int degree[maxn],add[maxn];
    vector<int>G[maxn];
    int n,m;
    int toposort()
    {
        queue<int>que;
        for(int i=1;i<=n;i++)
            if(degree[i] == 0)
                que.push(i);
        int ans = 0;
        while(que.size())
        {
            int ptr = que.front();
            que.pop();
            ans++;
            for(int i=0;i<G[ptr].size();i++)
            {
                degree[G[ptr][i]]--;
                if(degree[G[ptr][i]] == 0)
                {
                    que.push(G[ptr][i]);
                    add[G[ptr][i]] = add[ptr] + 1;
                }
            }
        }
        if(ans != n)
            return -1;
        int sum = 0;
        for(int i=1;i<=n;i++)
            sum += add[i] + 888;
        return sum;
    }
    int main()
    {
        while(~scanf("%d%d",&n,&m))
        {
            memset(degree,0,sizeof degree);
            memset(add,0,sizeof add);
            for(int i=1;i<=n;i++)
                G[i].clear();
            for(int i=0;i<m;i++)
            {
                int a,b;
                scanf("%d%d",&a,&b);
                G[b].push_back(a);
                degree[a]++;
            }
            printf("%d
    ",toposort());
        }
    }
  • 相关阅读:
    python之基础知识篇
    OpenGL 着色器管理类 Shader
    OpenGL 彩色三角形
    OpenGL 你好,三角形 练习一 二 三
    OpenGL 矩阵绘画
    OpenGL 第一个三角形!!!!
    OpenGL 读取,使用 .vert .frag 着色器 文件 C++
    C++ 一定要使用strcpy_s()函数 等来操作方法c_str()返回的指针
    OpenGL 入门指南(配置GLFW ,着色语言高亮,以及一些常见问题)
    汽车加工厂
  • 原文地址:https://www.cnblogs.com/smallhester/p/10389783.html
Copyright © 2011-2022 走看看