zoukankan      html  css  js  c++  java
  • 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
    ",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
    ");
          }
          else{
            print(sum);
          }
      }
    
    
    }
    
  • 相关阅读:
    [QT][待解决问题]对话框ui载入卡顿问题
    [QT] Tab键切换焦点顺序
    [QT][问题记录]发布软件时遇到的问题
    [QT][转载] Qt信号和槽
    [QT][转载]Qt:使用C++还是QML
    [qt][问题记录] 无法定位程序输入点 _ZdaPvj 于动态链接库 libstdc++-6.dll
    [QT]安装中出现的问题(安装qt5.8,出现Could not start:"{0,3010,1603,5100} msiexec ...")
    [转载]Lwip之IP/MAC地址冲突检测
    转载:TCP连接的状态详解以及故障排查
    git push (第一次) (转)
  • 原文地址:https://www.cnblogs.com/llke/p/10780133.html
Copyright © 2011-2022 走看看