zoukankan      html  css  js  c++  java
  • hdu 2647(拓扑排序)

    Reward

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 7231    Accepted Submission(s): 2263


    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
     
    Author
    dandelion
     
    Source
     
    题意:一个公司有n个人,现在知道了m个关系,输入 a b的话代表 a 的工资要比 b 高,最低工资是 888 元,问老板最后需要发的最少工资?
    题解:建立 b - > a的有向图,进行拓扑排序.然后从每一个起始点的工资从888开始,下一个点比上一个点高 1 ,计算即可。
    #include <iostream>
    #include <cstdio>
    #include <string.h>
    #include <queue>
    #include <algorithm>
    #include <math.h>
    using namespace std;
    const int N = 10005;
    const int M = 20005;
    struct Edge{
        int v,next;
    }edge[M];
    int head[N];
    int indegree[N];
    int w[N];
    int tot;
    void init(){
        memset(head,-1,sizeof(head));
        memset(indegree,0,sizeof(indegree));
        tot = 0;
    }
    void addEdge(int u,int v,int& tot){
        edge[tot].v = v,edge[tot].next = head[u],head[u] = tot++;
    }
    int main(){
        int n,m;
        while(scanf("%d%d",&n,&m)!=EOF){
            init();
            for(int i=1;i<=m;i++){
                int u,v;
                scanf("%d%d",&u,&v);
                addEdge(v,u,tot);
                indegree[u]++;
            }
            queue<int> q;
            int tot = 0;
            for(int i=1;i<=n;i++){
                if(!indegree[i]) {
                    q.push(i);
                    w[i] = 888;
                    tot+=w[i];
                }
            }
            int cnt = 0;
            while(!q.empty()){
                int u = q.front();
                q.pop();
                cnt++;
                for(int k = head[u];k!=-1;k=edge[k].next){
                    int v = edge[k].v;
                    indegree[v]--;
                    if(!indegree[v]){
                        w[v] = w[u]+1;
                        tot+=w[v];
                        q.push(v);
                    }
                }
            }
            if(cnt!=n) printf("-1
    ");
            else{
                printf("%d
    ",tot);
            }
        }
        return 0;
    }
  • 相关阅读:
    [BZOJ1013][JSOI2008]球形空间产生器sphere 高斯消元
    [Luogu1848][USACO12OPEN]书架Bookshelf DP+set+决策单调性
    [BZOJ1025][SCOI2009]游戏 DP+置换群
    [BZOJ1024][SCOI2009]生日快乐 搜索
    [BZOJ2002][Hnoi2010]Bounce弹飞绵羊 LCT
    「BZOJ 4565」「HAOI 2016」字符合并「区间状压DP」
    「BZOJ 5161」最长上升子序列「状压DP」
    「SPOJ TTM 」To the moon「标记永久化」
    「学习笔记」字符串大礼包
    「CF724G」Xor-matic Number of the Graph「线性基」
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5692904.html
Copyright © 2011-2022 走看看