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


相关推荐

  • 用docker部署jar包_war包和jar包部署区别

    用docker部署jar包_war包和jar包部署区别对于springboot项目运行,直接是java-jar的方式运行,如果想要放到docker中运行,有三种方式:方式一:1.上传jar到服务器的指定目录2.在该目录下创建Dockerfile文件viDockerfile3.然后将下面的内容复制到Dockerfile文件中FROMjava:8MAINTAINERbin…

    2022年10月19日
    0
  • mySQL函数根据经纬度计算两点距离

    mySQL函数根据经纬度计算两点距离

    2022年2月23日
    42
  • Java快速输入输出使用详解(解决Java输入输出超时问题)

    Java快速输入输出使用详解(解决Java输入输出超时问题)Java快速输入输出使用详解一、背景:  Scanner类输入时,输入效率比较慢,输入数据大于10^5左右时(你觉得数据有点多时就用快速输入即可),某些题目会超时。所以需要输入快一点的方法。  一般情况下输入数据多导致题目超时时,直接使用快速输入中的:1.简单方法即可。二、快速输入:1.简单方法(我常用的:只是加了个包装流BufferedReader)importjava.io.Bu…

    2022年5月9日
    75
  • 动态库依赖关系_查看运行的动态库

    动态库依赖关系_查看运行的动态库1前言这两天在编写一个插件系统Demo的时候,发现了个很奇怪的问题:插件加载器中已经链接了ld库,但是应用程序在链接插件加载器的时候,却还需要显式的来链接ld库。否则就会报:DSOmissingfromcommandline。这个报错翻译过来就是没有在命令行中指定该动态库。这个报错就很搞事了,你说你明明知道需要哪个库,为什么不直接帮我链接呢,非得我显示的在命令行中指定呢?2现象描…

    2022年9月1日
    0
  • 前端学到什么程度可以找到工作(应届毕业生有什么优势)

    目录1.前端开发下载安装VScode优化配置2、插件安装3、设置字体大小4、开启完整的Emmet语法支持5、视图2.Node.js入门2.1、什么是Node.js2.2、Node.js有什么用2.3、安装下载:2.4、快速入门2.5、服务器端应用开发3、ES6入门3.2、let声明变量3.3、const声明常量3.4、解构赋值创建3.5、模板字符串创建3.6、声明对象简写创建3.7、定义方法简写3.8、对象拓展

    2022年4月16日
    44
  • linux降内核版本_linux内核降级

    linux降内核版本_linux内核降级1,实验环境:Vmware12.5.1,Ubuntu16.0464位,Linux3.16.1(高版本无法启动qemu)Busybox1.20.2,u-boot-2016.09.tar.bz22.整体流程说明安装交叉编译工具链安装qemu模拟器编译arm架构u-boot用u-boot测试qemu是否正常启动(至此为第二次实验需要完成的内容)编译arm架构内核Qemu运行内核制作文件系统…

    2022年7月23日
    76

发表回复

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

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