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


相关推荐

  • pycharm与anaconda_python关系抽取

    pycharm与anaconda_python关系抽取1、Python是一种解释型、面向对象、动态数据类型的高级程序设计语言。虽然Python3.5自带了一个解释器IDLE用来执行.py脚本,但是却不利于我们书写调试大量的代码。常见的是用Notepade++写完脚本,再用idle来执行,但却不便于调试。这时候就出现了PyCharm等IDE,来帮助我们调试开发。2、PyCharm是一种PythonIDE,带有一整套可以帮助用户在使用P…

    2022年8月29日
    2
  • 抽象工厂模式与工厂方法模式有哪些不同_工厂方法和抽象工厂

    抽象工厂模式与工厂方法模式有哪些不同_工厂方法和抽象工厂Abstract Factory动机实例模式定义结构要点总结笔记动机在软件系统中,经常面临着”一系列相互依赖的对象“的创建工作;同时,由于需求的变化,往往存在更多系列对象的创建工作如果应对这种变换?如何绕过常规的对象创建方法(new),提供一种”封装机制“来避免客户程序和这种”多系列具体对象创建工作“的紧耦合?实例数据库连接的时候会有很多关联的对象,这些对象是一个整体朴素class EmployeeDAO{public: vector<EmployeeDAO> GetEm

    2022年8月9日
    4
  • 一款MVC5+EF+Bootstrap搭建的后台通用管理系统模板[通俗易懂]

    一款MVC5+EF+Bootstrap搭建的后台通用管理系统模板[通俗易懂]最近闲来无事,就用MVC5+EF+Bootstrap搭建了一个通用的后台管理系统的模板,里面使用到的技术包括:MVC,EF,T4模板批量生成Jquery,jqGridBootstrapDDDAutoMapper等开发工具:VS2015+SQL2012项目框架如下图:项目的效果图如下:JuCheapV2.0源代码http://……

    2022年9月11日
    4
  • Esp8266学习之旅① 搭建开发环境,开始一个“hellow world”串口打印。

    Esp8266学习之旅① 搭建开发环境,开始一个“hellow world”串口打印。本系列博客学习由非官方人员半颗心脏潜心所力所写,仅仅做个人技术交流分享,不做任何商业用途。如有不对之处,请留言,本人及时更改。1、Esp8266之搭建开发环境,开始一个“hellowworld”串口打印。2、Esp8266之利用GPIO开始使用按钮点亮你的“第一盏灯”。3、Esp8266之利用“软件定时器”定时0.5秒闪烁点亮一盏LED。4、Esp8266之了解P

    2022年5月30日
    55
  • 音视频编解码常用知识点

    音视频编解码常用知识点目录视频播放器原理流媒体协议封装格式(容器)编解码转码帧(Frame)帧率(Framerate)分辨率比特率(码率)采样率采样位数声道数有损压缩和无损压缩帧内压缩和帧间压缩对称编码和不对称编码音频编码声音数字化三要素音频编码标准视频编码色彩空间RGB色彩空间YUV色彩空间压缩原理熵与冗余帧内编码…

    2022年7月13日
    26
  • 基于STM32F4单片机对步进电机的控制(有代码)「建议收藏」

    基于STM32F4单片机对步进电机的控制(有代码)「建议收藏」步进电机是将电脉冲控制信号转变为角位移或线位移的一种常用的数字控制执行元件,又称为脉冲电机。在驱动电源的作用下,步进电机受到脉冲的控制,其转子的角位移量和速度严格地与输入脉冲的数量和脉冲频率成正比。步进电机每接收一个电脉冲,转子就转过一个相应的角度(步距角)。改变通电顺序可改变步进电动机的旋转方向;改变通电频率可改变步进电动机的转速。因此,通过控制输入电脉冲的数目、频率及电动机绕组的通电顺序就可以…

    2022年5月6日
    45

发表回复

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

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