分层拓扑排序好题

Reward

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

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
曾是惊鸿照影来

Recommend
yifenfei | We have carefully selected several similar problems for you: 3342 1811 2094 2729 1596

拓扑图分层,位于第0层的可以获得888奖金,位于第1层的可以获得889奖金,依次类推.
最终的奖金总数就是888*N+(所有节点层数总和).
程序实现基于BFS和连接表

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

const int maxn = 1e4+5;
const int maxm = 2e4+5;
int i,j,k,n,m,a,b,cnt;
struct node{
    int in,dep,head;
}point[maxn];
struct Node{
   int to,next;
}edge[maxm];

void add_edge(int i, int u, int v){
     edge[i].to = v;
     edge[i].next = point[u].head;
     point[u].head = i;
     point[v].in++;
}

void init(){
     memset(point,0,sizeof(point));
     for (i=1; i<=m; i++) {
          scanf("%d%d",&a,&b);
          add_edge(i,b,a);
     }
     cnt = 0;
}

bool tuopu(){
    int sum = 0;
    int u,v;
    queue<int>que;

    for (i=1; i<=n; i++) if (point[i].in==0) que.push(i);
    while (!que.empty()){
         u = que.front();
         que.pop();
         sum++;
         cnt += point[u].dep;
         for (i=point[u].head; i!=0; i=edge[i].next) {
             v = edge[i].to;
             if (--point[v].in==0) {
                que.push(v);
                point[v].dep = point[u].dep+1;
             }
         }
    }
    return sum==n;
}

int main(){
    while (~scanf("%d%d",&n,&m)){
         init();
         printf("%d\n",tuopu()?888*n+cnt:-1);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 数仓分层理论是数据仓库设计中的一个重要概念。根据数据仓库的目标和使用需求,数据仓库可以划分为不同的层次。常见的数仓分层理论包括三层架构和四层架构。 三层架构包括原子层、集成层和应用层。原子层是数据仓库中最底层的层次,负责采集和存储原始数据。集成层是在原子层的基础上进行数据清洗、整合和转换,使得数据能够被应用层使用。应用层是最上层的层次,提供给用户各种报表、分析和决策支持的功能。 四层架构在三层架构的基础上增加了数据存储层。数据存储层是为了提高数据查询和分析的性能而引入的,通常使用列式存储或者索引技术来优化数据的存储和访问。 数仓分层理论的目的是将数据仓库的功能和需求进行划分,使得数据仓库的设计更加灵活和可扩展。不同层次的数据可以根据需要进行更新和维护,同时也方便用户根据自己的需求进行数据查询和分析。数仓分层理论在数据仓库的设计和实施中起到了重要的指导作用。\[2\]\[3\] #### 引用[.reference_title] - *1* [大数据面试题--数仓](https://blog.csdn.net/weixin_42759988/article/details/123074673)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [原创|3万字剖析数仓面试题,珍藏版](https://blog.csdn.net/huzechen/article/details/123288181)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值