zoukankan      html  css  js  c++  java
  • hdu 2647 Reward(拓扑排序,反着来)

    Reward

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
    Total Submission(s) : 51   Accepted Submission(s) : 21

    Font: Times New Roman | Verdana | Georgia

    Font Size: ← →

    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
    #include <iostream>
    #include<cstdio>
    #include<cstring>
    #include<vector>
    #include<queue>
    using namespace std;
    int i,n,m;
    int a[10002],cnt[10002];
    vector<int> s[10002];
    long long sum;
    bool toposort()
    {
        queue<int> Q;
        int num=0;
        for(int i=1;i<=n;i++)
            if (cnt[i]==0) {Q.push(i); a[i]=888;}
        while(!Q.empty())
        {
            int u=Q.front();
            num++;
            Q.pop();
            for(int i=0;i<s[u].size();i++)
            {
                cnt[s[u][i]]--;
                if (cnt[s[u][i]]==0)
                    {
                        Q.push(s[u][i]);
                        a[s[u][i]]=max(a[u]+1,a[s[u][i]]);
                    }
            }
        }
        if (num<n) return 0;
        return 1;
    }
    int main()
    {
    
        while(~scanf("%d%d",&n,&m))
        {
            for(i=1;i<=n;i++) s[i].clear();
            memset(cnt,0,sizeof(cnt));
            for(i=1;i<=m;i++)
            {
                int x,y;
                scanf("%d%d",&y,&x);
                s[x].push_back(y);
                cnt[y]++;
            }
            memset(a,0,sizeof(a));
            if (!toposort())
            {
                printf("-1\n");
                continue;
            }
            sum=0;
            for(i=1;i<=n;i++) sum+=a[i];
            printf("%lld\n",sum);
        }
        return 0;
    }
  • 相关阅读:
    Flask之model以及缓存
    面向对象设计原则
    【二】、UML基础知识——图图解乾坤
    在大学拼学业,搞副业,我也曾迷茫,但我一直在路上
    vsftpd简介和ftpserver在win10上的下载及安装过程
    使用IDEA详解Spring中依赖注入的类型
    多么痛的领悟——计算机组成原理第一讲
    Java 发展简史:初生遇低谷,崛起于互联网
    写公众号一个月关注量破900,聊聊我的感受
    用IDEA详解Spring中的IoC和DI(挺透彻的,点进来看看吧)
  • 原文地址:https://www.cnblogs.com/stepping/p/5700373.html
Copyright © 2011-2022 走看看