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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • MATLAB绘图怎么变得更好看[通俗易懂]

    MATLAB绘图怎么变得更好看[通俗易懂]同样用的都是MATLAB,为啥大佬们画的图都那么好看,而你画的图都是简单、普通,那是因为我们掌握的基础元素不一样,只有掌握了最基本的基础元素,再加上日益增长的审美,才会有一张好图出来。二维绘图 函数名 说明 plot 基本的线性坐标绘图 loglog X-Y轴双对数坐标绘图 …

    2022年6月20日
  • 菜鸟教程-maven[通俗易懂]

    菜鸟教程-maven[通俗易懂]Maven基于项目对象模型(缩写:POM)概念 Maven是一个项目管理工具,可以对Java项目进行构建、依赖管理。 Maven是一个基于Java的工具,所以要做的第一件事情就是安装JDK。 Maven提倡使用一个共同的标准目录结构,Maven使用约定优于配置的原则,大家尽可能的遵守这样的目录结构。如下所示: 目录 目的 ${basedir} 存放pom.xml和所有的子目录 ${basedir}/src/main/java 项目的ja

    2022年9月8日
  • JWT原理讲解_达林顿管工作原理

    JWT原理讲解_达林顿管工作原理JWT(JSONWEBTOKEN)jwt有3部分组成:(head、Payload、signature)下面我们将分三步生成JWT的值。1.Head头部:一般包含两部分{“typ”:”JWT”,//固定为jwt”alg”:”HS256″//加密算法}第一部分的值:将Head进行Base64加密就是第一部分的值。2.Payload荷载:包含公共部分与自定义部分{ //公共部分,非强制可以省略。 “iss”:”servic

    2022年10月17日
  • 爱比较病毒测试(不是震网病毒特点的是)

    CIH、爱虫、冲击波、熊猫烧香,对这4种网络病毒你了解多少?现在的网络病毒能够分为三类,第一类是“蠕虫”病毒,这类型的病毒是在感染电脑后经过不停地复制自己,将电脑原来的运行和储存资源耗费掉,最终电脑由于资源耗费干净而无法运行直至死机;第二类是“进犯型”病毒,这类病毒和蠕虫病毒不一样,进犯型病毒一旦感染就会对电脑软件甚至是硬件做出破坏性的进犯,威力和破坏性极端强大。最终第三类是“木马”类病毒,这类病…

    2022年4月14日
  • dubbo负载均衡策略(XML、注解、SpringBoot配置)「建议收藏」

    dubbo负载均衡策略(XML、注解、SpringBoot配置)「建议收藏」本示例是在上一篇文章中搭建的实例来讲解,详情先查看:SpringBoot集成dubbo最新实战教程:dubbo-spring-boot-starter一.简介在集群负载均衡时,Dubbo提供了多种均衡策略,默认为random随机调用。二.负载均衡策略1.RandomLoadBalance随机,按权重设置随机概率。 在一个截面上碰撞的概率高,但调用量越大分布越均…

    2022年7月27日
  • ODrive应用 #4 配置参数&指令「建议收藏」

    参数与指令我们将使用作为每个ODrive对象的占位符。每个ODrive控制器都是一个ODrive对象。在odrivetool中通常是odrv0。此外,我们将<axis>用作每个轴的占位符,这是ODrive对象的属性(例如odrv0.axis0)。轴表示电动机的连接位置。(M0和axis0对应,M1和axis1对应)文章目录参数与指令轴相应的指令状态机启动程序控制模式控制指令…

    2022年4月14日

发表回复

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

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