• Reward_toposort


    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 7610    Accepted Submission(s): 2418


    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<queue>
    #include<string.h>
    #include<stdio.h>
    using namespace std;
    const int N=10005;
    int in[N],mo[N];
    vector<int>v[N];
    int cnt,sum,n;
    queue<int>qu;
    void init()
    {
        while(!qu.empty()) qu.pop();
        for(int i=1;i<=n;i++)
        {
            v[i].clear();
            mo[i]=888;//刚开始都为888;
            in[i]=0;
        }
    }
    void toposort()
    {
        sum=0;cnt=0;
        while(!qu.empty())
        {
            int t=qu.front();
            qu.pop();
            sum+=mo[t];
            cnt++;//没有矛盾的情况,最后应该是n
            for(int i=0;i<v[t].size();i++)
            {
                in[v[t][i]]--;
                if(in[v[t][i]]==0) qu.push(v[t][i]);
                mo[v[t][i]]=mo[t]+1;//比t多,在t的基础上加一;
            }
        }
    }
    int main()
    {
        int m,a,b;
        while(cin>>n>>m)
        {
            init();
            for(int i=1;i<=m;i++)
            {
                cin>>a>>b;
                v[b].push_back(a);//这里应该是反的,a必须比b多。刚开始没注意,一直wa!!
                in[a]++;
            }
            int flag=0;
            for(int i=1;i<=n;i++)
            {
                if(!in[i]){qu.push(i);flag=1;}
            }
            if(flag==0) cout<<"-1"<<endl;
            else
            {
                toposort();
                if(cnt!=n) cout<<"-1"<<endl;
                else cout<<sum<<endl;
            }
    
        }
        return 0;
    }
  • 相关阅读:
    Android控件之ListView探究二
    解决activity页面跳转时显示桌面
    TextView实现滚动显示的效果
    QPainter的坐标系系统的转换
    MODBUS_RTU通信协议
    收藏的博客 -- Qt有关的GitHub/Gitee开源项目
    VS联调多个解决方案的项目
    软件工具——GitGUI使用教程
    Python中对文件的操作
    VMware虚拟机三种网络模式详解 --------- NAT(地址转换模式)
  • 原文地址:https://www.cnblogs.com/iwantstrong/p/5759044.html
Copyright © 2020-2023  润新知