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;
    }
  • 相关阅读:
    我的Java学习路线图
    请求重定向和请求转发的区别
    PHP代码审计学习-php安全基础
    无密码正向直连内网linux目标机复现
    Windows API 学习
    Http请求走私
    免杀手法-tcp套字节传递shellcode学习
    自启动模块构造-计划任务
    自启动模块构造-快速启动目录
    进程注入免杀学习
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5692904.html
Copyright © 2011-2022 走看看