HDU 2647 Reward 【拓扑排序反向建图+队列】

HDU 2647 Reward 【拓扑排序反向建图+队列】

题目 Reward

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>     //EOF,NULL
#include<cstring>    //memset
#include<cstdlib>    //rand,srand,system,itoa(int),atoi(char[]),atof(),malloc
#include<cmath>           //ceil,floor,exp,log(e),log10(10),hypot(sqrt(x^2+y^2)),cbrt(sqrt(x^2+y^2+z^2))
#include<algorithm>  //fill,reverse,next_permutation,__gcd,
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<utility>
#include<iterator>
#include<iomanip>             //setw(set_min_width),setfill(char),setprecision(n),fixed,
#include<functional>
#include<map>
#include<set>
#include<limits.h>     //INT_MAX
#include<bitset> // bitset<?> n
using namespace std;

typedef long long ll;
typedef pair<int,int> P;
#define all(x) x.begin(),x.end()

#define readc(x) scanf("%c",&x)
#define read(x) scanf("%d",&x)
#define read2(x,y) scanf("%d%d",&x,&y)
#define read3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define print(x) printf("%d\n",x)
#define mst(a,b) memset(a,b,sizeof(a))
#define pb(x) push_back(x)
#define lowbit(x) x&-x
#define lson(x) x<<1
#define rson(x) x<<1|1
const int INF =0x3f3f3f3f;
const int mod = 1e9+7;
const int MAXN = 10010;
int n,m;
int a,b;
int in[MAXN];
int sum,cnt;
int price[MAXN];
vector<int>Edge[MAXN];
vector<int> ans;
queue<int> q;

void Init(){
  for(int i = 1;i <= n;i++){
      Edge[i].clear();
  }
  memset(in,0,sizeof in);
  while(!q.empty())  q.pop();
  sum = cnt = 0;
}

int main(){
  while(read2(n,m)!=EOF){
      Init();

      for(int i = 0 ; i < m;i++){
        read2(a,b);
        Edge[b].push_back(a);   //反向建图
        in[a] ++;
      }

      for(int i = 1 ;i <= n ;i++){
         if(in[i] == 0){
           q.push(i);
           price[i] = 888;
         }
      }
      
      
      while(!q.empty()){
          int p = q.front();
          sum += price[p];
          cnt++;
          q.pop();
          for(int i = 0; i < Edge[p].size(); i++){
             int y  = Edge[p][i];
             in[y] --;
             price[y] = price[p]+1;
             if(in[y] == 0){
               q.push(y);
             }
          }
      }
      if(cnt < n){
        printf("-1\n");
      }
      else{
        print(sum);
      }
  }


}

 

转载于:https://www.cnblogs.com/llke/p/10780133.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/101021.html原文链接:https://javaforall.net

(0)
上一篇 2021年7月3日 下午5:00
下一篇 2021年7月3日 下午6:00


相关推荐

  • Google Borg论文[通俗易懂]

    Google Borg论文[通俗易懂]Borg的论文逐字翻译,拒绝机器翻译,有一些自己的理解,不一定对,作为参考就行

    2025年8月20日
    5
  • android开发笔记之 Android代码混淆打包

    android开发笔记之 Android代码混淆打包大家应该都听过代码混淆吧,如果大家有去反编译过别人的APK的话,应该会看到好多包名和类名是a,b.c….之类的的吧,这里就提到了一个概念:混淆。那就让我们了解下这个东西吧作用:为了防止自己的劳动成果被别人窃取,混淆代码能有效防止被反编译缺省情况下,proguard会混淆所有代码,但是下面几种情况是不能改变java元素的名称,否则就会这样就会导致程序出错。一,我们用到反射的地方。

    2022年5月30日
    44
  • SqlServer2019安装教程-自定义安装

    SqlServer2019安装教程-自定义安装SqlServer2019安装教程-基本安装:https://blog.csdn.net/qq_33556442/article/details/102848891搜索SqlServer2019进入官方网站,点击下载(时间稍微有点长)选择下载的环境,这里是Windows的下载(时间稍微有点长,请耐心等待)进入最终的下载界面,点击【Continue】https:/…

    2022年7月13日
    20
  • QCustomPlot 官方文档学习1

    QCustomPlot 官方文档学习1      用一些实例来作为QCustomPlot学习的指南,如果用QtCreater提升一个Widget,就能够通过 ui-&gt;customPlot或者其他的名字访问各个Widget;Youcancreateanewgraphintheplotvia customPlot-&gt;addGraph().Thenyouassignthegraphsome…

    2022年10月16日
    5
  • OpenClaw龙虾AI概念 ,值得关注的4大核心环节| 商经情报局

    OpenClaw龙虾AI概念 ,值得关注的4大核心环节| 商经情报局

    2026年3月13日
    2
  • java 获取当天的日期_Java 获取当前日期和时间

    java 获取当天的日期_Java 获取当前日期和时间有三种方法 方法一 用 java util Date 类来实现 并结合 java text DateFormat 类来实现时间的格式化 看下面代码 1importjava util 2importjava text 3 以下默认时间日期显示方式都是汉语语言方式 4 一般语言就默认汉语就可以了 时间日期的格式默认为 MEDIUM 风格 比如 2008 6 1620 54 535 以下

    2026年3月16日
    1

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号