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;
    }
  • 相关阅读:
    [ML] the notes
    [Java] 在 jar 文件中读取 resources 目录下的文件
    [LeetCode] 53. Maximum Subarray 解题思路
    [git] git 分支管理和工作流程
    debug实战:Unmanaged High Memory非托管高内存
    batch insert 1 million datas into mysql
    nuget的小Tips
    debug实战:进程Hang+High CPU
    debug实战:COM组件GetToSTA导致高内存+GC被阻塞
    svn cleanup failed问题解决
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5692904.html
Copyright © 2011-2022 走看看