贪心——Hierarchy

Hierarchy

题面翻译

题目描述

n n n 的公司有 n n n 个员工,每个员工 i i i 有一个初始的权值 q i q_i qi ,每一个员工有且只有一个上司。 有 m m m 条申请,每个申请由三个数 a i a_i ai b i b_i bi c i c_i ci 构成,代表将 a i a_i ai 任命为 b i b_i bi 的上司所需要的花费为 c i c_i ci,同时必须保证 q a i > q b i q_{a_i}>q_{b_i} qai>qbi。试求使每个员工(顶头上司除外)都有且只有一个上司所花费的最小代价。

输入格式

第一行 n n n,第二行 n n n 个数表示 q i q_i qi,第三行 m m m,之后 m m m 行每行三个数表示 a i a_i ai b i b_i bi c i c_i ci

输出格式

一个整数表示最小代价,若无解则输出 -1

说明/提示

数据规模与约定

1 ≤ n ≤ 1 0 3 1 \le n \le 10^3 1n103 0 ≤ m ≤ 1 0 4 0 \le m \le 10^4 0m104, 0 ≤ q i ≤ 1 0 6 0 \le q_i \le 10^6 0qi106 0 ≤ c i ≤ 1 0 6 0 \le c_i \le 10^6 0ci106 1 ≤ a i , b i ≤ n 1 \le a_i,b_i \le n 1ai,bin

题目描述

Nick’s company employed $ n $ people. Now Nick needs to build a tree hierarchy of «supervisor-surbodinate» relations in the company (this is to say that each employee, except one, has exactly one supervisor). There are $ m $ applications written in the following form: «employee $ a_{i} $ is ready to become a supervisor of employee $ b_{i} $ at extra cost $ c_{i} $ ». The qualification $ q_{j} $ of each employee is known, and for each application the following is true: $ q_{ai}>q_{bi} $ .

Would you help Nick calculate the minimum cost of such a hierarchy, or find out that it is impossible to build it.

输入格式

The first input line contains integer $ n $ ( $ 1<=n<=1000 $ ) — amount of employees in the company. The following line contains $ n $ space-separated numbers $ q_{j} $ ( $ 0<=q_{j}<=10^{6} $ )— the employees’ qualifications. The following line contains number $ m $ ( $ 0<=m<=10000 $ ) — amount of received applications. The following $ m $ lines contain the applications themselves, each of them in the form of three space-separated numbers: $ a_{i} $ , $ b_{i} $ and $ c_{i} $ ( $ 1<=a_{i},b_{i}<=n $ , $ 0<=c_{i}<=10^{6} $ ). Different applications can be similar, i.e. they can come from one and the same employee who offered to become a supervisor of the same person but at a different cost. For each application $ q_{ai}>q_{bi} $ .

输出格式

Output the only line — the minimum cost of building such a hierarchy, or -1 if it is impossible to build it.

样例 #1

样例输入 #1

4
7 2 3 1
4
1 2 5
2 4 1
3 4 1
1 3 5

样例输出 #1

11

样例 #2

样例输入 #2

3
1 2 3
2
3 1 2
3 1 3

样例输出 #2

-1

提示

In the first sample one of the possible ways for building a hierarchy is to take applications with indexes 1, 2 and 4, which give 11 as the minimum total cost. In the second sample it is impossible to build the required hierarchy, so the answer is -1.

思路

通过看题以及画图可知,这道题有点类似并查集的合并操作,但本道题有个代价,所以我们就得用类似kruscal(最小生成树)的思想(本质上贪心)。

  • 细节1:本道题有点权和边权,但最小生成树只能控制边权,对于点权无法控制,因此点权我们只能自行控制。(具体怎么控制呢?我们可以就类似之前有道题就是有那个等级嘛,这个点权也类似与等级,如果一个人的等级比另一个人的等级小,那么就不能连边(也就是kruscal的连边操作))。
  • 细节2:边界条件:如何合并次数不等于总点数,那么失败。

代码

//有点类似并查集,但更精确来说是最小生成树
//要看权重和权值,让我想到了这里的权重很像是等级

#include<iostream>
#include<algorithm>

using namespace std;

const int N = 1e6+10;

struct E{
    int x,y,w;
}e[N];
bool fa[N];//判断是否有上司
int a[N];
int n,m;
int ans,cnt;

bool cmp(E a,E b){
    return a.w<b.w;
}

void cruskal(){
    
    for(int i=1;i<=m;i++){
        if(!fa[e[i].y]&&a[e[i].x]>a[e[i].y]){
            fa[e[i].y]=true;
            ans+=e[i].w;
            cnt++;
        }
        
    }
    
    if(cnt!=n-1){
        ans=-1;
    }
}

int main(){
    cin>>n;
    
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    
    cin>>m;
    for(int i=1;i<=m;i++){
        cin>>e[i].x>>e[i].y>>e[i].w;
    }
    
    sort(e+1,e+1+m,cmp);
    
    cruskal();
    
    cout<<ans;
    
    return 0;
}
  • 14
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

green qwq

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值