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;
    }
  • 相关阅读:
    最小生成树与Prim算法
    图的存储——链式前向星
    *转载 Tarjan有向图详解
    图的连通性算法-Kosaraju
    最小花费8597
    PDF提取图片(错误纠正)
    字符串删除指定符号(不限位置)
    python迭代有限制,突破限制
    storm资源冲突
    storm单节点问题(转载)
  • 原文地址:https://www.cnblogs.com/shinianhuanniyijuhaojiubujian/p/8994786.html
Copyright © 2011-2022 走看看