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

    
    RewardTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 9951    Accepted Submission(s): 3173


    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
     
    拓扑排序:  对一个有向无环图(Directed Acyclic Graph简称DAG)G进行拓扑排序,是将G中所有顶点排成一个线性序列,使得图中任意一对顶点u和v,若<u,v> ∈E(G),则u在线性序列中出现在v之前。通常,这样的线性序列称为满足拓扑次序(Topological Order)的序列,简称拓扑序列。
    拓扑序列算法思想
    (1)从有向图中选取一个没有前驱(即入度为0)的顶点,并输出之;
    (2)从有向图中删去此顶点以及所有以它为尾的弧;
         重复上述两步,直至图空,或者图不空但找不到无前驱的顶点为止。
    #include<stdio.h>
    #include<stdlib.h>
    #include<iostream>
    #include<algorithm>
    #include<string.h>
    using namespace std;
    struct node
    {
        int v;
        struct node *next;
    }*h[10000];
    int d[100000],f[100000],mon[100000];
    int n,m;
    void LA(int u,int v)
    {
        struct node*p=(struct node*)malloc(sizeof(struct node));
        p->v=v;
        p->next=h[u];
        h[u]=p;
    }
    void init()
    {
        for(int i=0; i<=n; i++)
        {
            d[i]=0;
            f[i]=0;
            mon[i]=888;
            h[i]=NULL;
        }
    }
    int main()
    {
        while(~scanf("%d%d",&n,&m))
        {
            init();
            for(int i=0; i<m; i++)
            {
                int a,b;
                scanf("%d%d",&a,&b);
                LA(b,a);
                d[a]++;
            }
            int top=0,t=0,p=1;
            while(p)
            {
                p=0;
                top++;
                for(int i=1; i<=n; i++)
                {
                    if(!d[i])
                    {
                        p=1;
                        f[i]=top;
                        mon[i]=mon[i]+top-1;
                        t++;
                    }
                }
                for(int i=1; i<=n; i++)
                {
                    if(f[i]==top)
                    {
                       int u=i;
                        struct node*q;
                        for(q=h[u]; q!=NULL; q=q->next)
                        {
                            int x=q->v;
                            d[x]--;
                        }
                        d[i]=-1;
                    }
                }
            }
            if(t==n)
            {
                int sum=0;
                for(int i=1; i<=n; i++)
                {
                     sum+=mon[i];
                }
                printf("%d
    ",sum);
            }
            else
                printf("-1
    ");
        }
    }
    

      

  • 相关阅读:
    C#设计模式之订阅发布模式
    ASP.NET Core依赖注入(DI)
    ASP.NET 开源导入导出库Magicodes.IE 完成Csv导入导出
    .NET IoC模式依赖反转(DIP)、控制反转(Ioc)、依赖注入(DI)
    曾经优秀的人,怎么就突然不优秀了?
    IDEA中文注释难看的简单解决办法
    JasperReport报表中输出Excel时,部分列不显示的问题
    为什么Spring Security看不见登录失败或者注销的提示
    JQuery文件上传插件JQuery.upload.js的用法简介
    一个很酷炫也挺实用的JS库leader-line
  • 原文地址:https://www.cnblogs.com/da-mei/p/9053351.html
Copyright © 2011-2022 走看看