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


相关推荐

  • java批量修改数据库数据_sql批量更新多条数据

    java批量修改数据库数据_sql批量更新多条数据批量更新mysql更新语句很简单,更新一条数据的某个字段,一般这样写:代码如下:UPDATEmytableSETmyfield=’value’WHEREother_field=’other_value’;如果更新同一字段为同一个值,mysql也很简单,修改下where即可:代码如下:UPDATEmytableSETmyfield=’value’WHEREother_…

    2025年6月10日
    3
  • docker 镜像构建_dockerfile引入两个镜像

    docker 镜像构建_dockerfile引入两个镜像前言如果我们已经安装了一个python3的环境,如果另一台机器也需要安装同样的环境又要敲一遍,很麻烦,这里可以配置Dockerfile文件,让其自动安装,类似shell脚本Dockerfile编写

    2022年7月30日
    102
  • 浅析日本队崛起原因_createNewFile

    浅析日本队崛起原因_createNewFile2019独角兽企业重金招聘Python工程师标准>>>…

    2025年7月9日
    5
  • 安卓activity生命周期_请描述activity的生命周期

    安卓activity生命周期_请描述activity的生命周期Activity作为Android的四大组件之一,非常的重要,也是最常用的四大组件,使用Activity必须要在AndroidManifest中进行注册,那么作为Android的基础,Activity的生命周期你是否完全掌握了呢?下面就让我们来一起回顾一下Activity的生命周期吧!首先,你需要知道Activity的生命周期分为两种。 典型情况下的生命周期(就是我们开发中经常用到的…

    2022年8月16日
    9
  • 将图片转换为Base64编码字符串、解析Base64编码字符串后生成图片「建议收藏」

    将图片转换为Base64编码字符串、解析Base64编码字符串后生成图片「建议收藏」代码publicclassTest{publicstaticvoidmain(String[]args){//测试1:将图片转换成base64编码字符串Stringcode=imgToCode(“C:\\Users\\mingming\\Pictures\\Screenshots\\1.png”);System.out.println(“生成的base64编码字符串是:”+code);//测试

    2022年7月13日
    12
  • leetcode394字符串解码(回溯|栈)「建议收藏」

    leetcode394字符串解码(回溯|栈)「建议收藏」给定一个经过编码的字符串,返回它解码后的字符串。编码规则为: k[encoded_string],表示其中方括号内部的 encoded_string 正好重复 k 次。注意 k 保证为正整数。你可以认为输入字符串总是有效的;输入字符串中没有额外的空格,且输入的方括号总是符合格式要求的。此外,你可以认为原始数据不包含数字,所有的数字只表示重复的次数 k ,例如不会出现像 3a 或 2[4] 的输入。示例 1:输入:s = “3[a]2[bc]”输出:”aaabcbc”示例 2:输入:s =

    2022年8月8日
    6

发表回复

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

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