zoukankan      html  css  js  c++  java
  • hdu 2647 Reward(拓扑排序+优先队列)

    Problem Description
    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 <algorithm>
    #include <cstring>
    #include <cstdio>
    #include <vector>
    #include <queue>
    #include <stack>
    #include <cstdlib>
    #include <iomanip>
    #include <cmath>
    #include <cassert>
    #include <ctime>
    #include <map>
    #include <set>
    using namespace std;
    #pragma comment(linker, "/stck:1024000000,1024000000")
    #define lowbit(x) (x&(-x))
    #define max(x,y) (x>=y?x:y)
    #define min(x,y) (x<=y?x:y)
    #define MAX 100000000000000000
    #define MOD 1000000007
    #define pi acos(-1.0)
    #define ei exp(1)
    #define PI 3.1415926535897932384626433832
    #define ios() ios::sync_with_stdio(true)
    #define INF 0x3f3f3f3f
    #define mem(a) ((a,0,sizeof(a)))
    typedef long long ll;
    vector<int>v[10006];
    int vis[10006],degree[10006];
    int n,m,x,y,cnt,flag;
    struct node
    {
        int x;
        int y;
        bool operator<(const node &a) const
        {
            return a.y<y;
        }
    }ans,pos;
    void toposort()
    {
        priority_queue<node>q;
        for(int i=1;i<=n;i++)
        {
            if(!degree[i])
            {
                ans.x=i;
                ans.y=0;
                vis[i]=0;
                q.push(ans);
            }
        }
        while(!q.empty())
        {
            pos=q.top();
            q.pop();
            for(int i=0;i<v[pos.x].size();i++)
            {
                degree[v[pos.x][i]]--;
                if(!degree[v[pos.x][i]])
                {
                    ans.x=v[pos.x][i];
                    ans.y=pos.y+1;
                    q.push(ans);
                    vis[v[pos.x][i]]=pos.y+1;
                }
            }
        }
        cnt=flag=0;
        for(int i=1;i<=n;i++)
        {
            if(vis[i]==-1) flag=1;
            else cnt+=888+vis[i];
        }
        printf("%d
    ",flag?-1:cnt);
    }
    int main()
    {
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            for(int i=1;i<=n;i++)
                v[i].clear();
            memset(vis,-1,sizeof(vis));
            memset(degree,0,sizeof(degree));
            for(int i=0;i<m;i++)
            {
                scanf("%d%d",&x,&y);
                v[y].push_back(x);
                degree[x]++;
            }
            toposort();
        }
        return 0;
    }
  • 相关阅读:
    一本通 1259:【例9.3】求最长不下降序列
    一本通 1258:【例9.2】数字金字塔
    洛谷 P1198 [JSOI2008]最大数
    洛谷 P2863 [USACO06JAN]牛的舞会The Cow Prom
    【BZOJ1062】糖果雨(NOI2008)-数形结合+二维树状数组
    【BZOJ4070】雅加达的摩天楼(APIO2015)-分块+最短路
    【BZOJ2326】数学作业(HNOI2011)-递推+矩阵快速幂
    【BZOJ2734】集合选数(HNOI2012)-状压DP
    【BZOJ3213】抛硬币(ZJOI2013)-期望DP+KMP+高精度
    【BZOJ3590】Quare(SNOI2013)-状压DP
  • 原文地址:https://www.cnblogs.com/shinianhuanniyijuhaojiubujian/p/8994786.html
Copyright © 2011-2022 走看看