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


相关推荐

  • 教师资格证面试容易过吗_面试官不录用你的暗示

    教师资格证面试容易过吗_面试官不录用你的暗示什么是连接查询?笛卡尔积如何避免?内连接和外连接的概念是什么?表连接的原理是什么?SimpleNested-LoopJoin、IndexNested-LoopJoin、BlockNested-LoopJoin分别是什么概念?Nested-LoopJoin和BlockNested-LoopJoin对比分析驱动表用什么比较合适?

    2022年8月30日
    4
  • 关于extreme sports的作文_grantable

    关于extreme sports的作文_grantable尊敬的前辈,    大家好!    我在网上看到了关于extremedb的文章,在实际操作时碰到了一些问题,想和大家探讨一下。1、我先安装了extremedb_3.1_win32_sql_log_eval_1B.EXE,2、然后用安装目录(C:/McObject/eXtremeDB_SQL_LOG/win32/host/bin)下的mcocomp.exe对test.mco文件进行了

    2022年10月14日
    3
  • matlab 画折线图并美化

    matlab 画折线图并美化使用函数:plot(绘图);set(设置坐标属性);print(输出图片)代码如下:X=1:9;Y=10:10:90;f2=figure(2);set(gcf,’unit’,’centimeters’,’position’,[0,0,8,6])plot(X,Y,’-ok’,’LineWidth’,1,’MarkerSize’,5,’MarkerFaceColor’,’k’)set(gca,’FontName’,’TimesNewRoman’,’FontSize’

    2022年6月7日
    207
  • pycharm:系统找不到指定文件[通俗易懂]

    pycharm:系统找不到指定文件[通俗易懂]解决办法:打开你的项目里面有一个idea文件→找到idea文件下的workspace.xml文件,修改name=”SDK_HOME”,这一行的value→将它的路径修改为Python安装包的路径

    2022年8月27日
    1
  • idea配置JDK、Tomcat 环境

    idea配置JDK、Tomcat 环境

    2021年9月30日
    124
  • SBC应用「建议收藏」

    SBC应用「建议收藏」在VoIP呼叫中主要使用会话发起协议(SIP),H.323和MGCP呼叫信令协议,Sbc在主叫和被叫的信令/媒体路径之间引入。通常,SBC隐藏网络拓扑,接管呼入并生成到新的请求分支到被叫。技术上叫做背靠背B2BUA;这样做的好处是信令流和媒体流都能得到SBC控制。方便处理录音、生成保持背景音乐等媒体相关的处理;假如SBC不具备媒体服务的处理能力,也能够转发媒体流…

    2025年8月28日
    4

发表回复

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

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