• 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;
    }
  • 相关阅读:
    c盘瘦身、windows解除上网限速、贴膜注意事项
    windows7导入k8s用户证书
    ubuntu报错解决和注意事项
    ubuntu默认root密码问题,第一次使用ubuntu需要设置root密码
    java程序员修炼之道
    选择器代码
    css的使用技巧资料
    移动开发的相关资料
    使用phantomjs生成网站快照
    PHP Simple HTML DOM Parser Manual-php解析DOM
  • 原文地址:https://www.cnblogs.com/shinianhuanniyijuhaojiubujian/p/8994786.html
Copyright © 2020-2023  润新知