zoukankan      html  css  js  c++  java
  • HDU2647 topsort

    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
     
     
    拓扑排序专题
    vector 存图 注意vector的使用
    算是拓扑排序的模板题目吧
    多线程的  此题精妙在L数组 L初始为0
     
    #include<bits/stdc++.h>
    using namespace std;
    vector<int> mp[20005];
    int n,m;
    int a,b;
    int in[20005];
    int L[20005];
    int flag;
    int re;
    struct node
    {
        int sum;
    };
    queue<node>q;
    struct node N,now;
    int main()
    {
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            for(int i=1;i<=n;i++)
                mp[i].clear();
            memset(in,0,sizeof(in));
            memset(L,0,sizeof(L));
            for(int i=0;i<m;i++)
            {
                scanf("%d%d",&a,&b);
                mp[b].push_back(a);
                in[a]++;
            }
            for(int i=1;i<=n;i++)
            {
                if(in[i]==0)
                {
                    N.sum=i;
                    q.push(N);
                }
            }
            int jishu=n;
            int temp;
               while(!q.empty())
                {
                    now=q.front();
                    q.pop();
                    jishu--;
                    temp=L[now.sum];
                    //cout<<now.sum<<endl;
                    for(unsigned int i=0;i<mp[now.sum].size();i++)
                    {
                         if(--in[mp[now.sum][i]]==0)
                          {
                              N.sum=mp[now.sum][i];
                              L[N.sum]=temp+1;
                              q.push(N);
                          }
                    }
                }
                re=0;
                if(jishu>0)
                    printf("-1
    ");
                else
                {
                    for(int i=1;i<=n;i++)
                        re+=L[i];
                    printf("%d
    ",re+888*n);
                }
    }
        return 0;
    }
    
  • 相关阅读:
    XMLHttpRequest 对象相关
    slideToggle()---单击隐藏/浮现--jQuery--click() 方法
    如何打开无线网卡开关(常见无线网卡开关样式)-----记一次令人脸红的羞耻操作
    之前写的页面导出Excel表格
    word 快捷键 部分
    windows 快捷键 部分
    推荐几款屏幕录制工具(可录制GIF)
    【myeclipse2014-2017】使用相关
    嵌套的JsonObject与JSONArray的取值---JSON中嵌套JSONArray
    图片素材库
  • 原文地址:https://www.cnblogs.com/hsd-/p/4931642.html
Copyright © 2011-2022 走看看