zoukankan      html  css  js  c++  java
  • hdu 2647 还是逆向拓扑

    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
    嗯。,。 这道题目是要统计费用 然后为了方便统计层数上的钱数  需要逆向构图
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #define maxn 11000    
    #include<vector>
    #include<iostream>
    using namespace std;//拓扑排序的分层问题 
    int m,n,degree[maxn];
    vector<int> mapp[maxn];// 
    int topo()
    {
        int flag,sum=0; 
        vector<int> ans;
        int tt=0,re=0;//tt表示层数
        while(1)
        {
            flag=0;
            ans.clear();
            for(int i=1;i<=m;i++) if(degree[i]==0)//每次得处理一层的 /./
            {
                ans.push_back(i);
                sum+=(888+tt);
                degree[i]=-1;
                flag=1;
                re++;
            } 
            if(re>=m) return sum;//当遍历的点数到达m return
            if(flag==0) return 0;
            for(int j=0;j<ans.size();j++)
            {
                int rett=ans[j];
                for(int k=0;k<mapp[rett].size();k++)
                {
                    int temp=mapp[rett][k];
                    degree[temp]--;
                }
            }
            tt++;
        }
        return sum;
    }
    int main()
    {
        int a,b,i,j;
        while(scanf("%d%d",&m,&n)!=EOF)
        {
            if(m==0&&n==0) break;
            memset(degree,0,sizeof(degree));
            for(i=0;i<=m;i++) mapp[i].clear();
            for(i=0;i<n;i++)
            {
                scanf("%d%d",&a,&b);
                mapp[b].push_back(a);
                degree[a]++;  
            }
            int t=topo();
            if(t==0) printf("-1
    ");
            else printf("%d
    ",t);
        }
        return 0;
    }
  • 相关阅读:
    LeetCode:25 K个一组翻转链表
    LeetCode:3 无重复字符的最长子串(双指针)
    Java——参数问题与final实例域
    Java——对象的构造
    配置远程服务器 安装iis 远程服务器网络无法连接
    未能找到元数据文件
    ef 设计model 标签
    visualsvn for vs2017 初始化错误
    resharper 2018.2.3破解
    C# winform 自定义函数中找不到Form中的控件和定义的全局变量
  • 原文地址:https://www.cnblogs.com/z1141000271/p/5798053.html
Copyright © 2011-2022 走看看