CF 17B Hierarchy

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 ai is
ready to become a supervisor of employee bi at
extra cost ci?.
The qualification qj of
each employee is known, and for each application the following is true: qai?>?qbi.

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

Input

The first input line contains integer n (1?≤?n?≤?1000)
— amount of employees in the company. The following line contains n space-separated numbers qj (0?≤?qj?≤?106)—
the employees‘ qualifications. The following line contains number m (0?≤?m?≤?10000)
— amount of received applications. The following mlines contain the applications themselves, each of them in the form of three space-separated numbers: ai,bi and ci (1?≤?ai,?bi?≤?n, 0?≤?ci?≤?106).
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 qai?>?qbi.

Output

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

Sample test(s)

input

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

output

11

input

3
1 2 3
2
3 1 2
3 1 3

output

-1

Note

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.

预处理+贪心就可以了,因为根只有一个,所以在输入的时候预处理子节点的最小值贪心就好了=。=

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
using namespace std;
const int INF=0x3ffffff;
int f[1100];

int main()
{
    int n,m;
    int temp,u,v,w;
    while(cin>>n)
    {
        for(int i=1;i<=n;i++)
        {
            cin>>temp;
            f[i]=INF;
        }
        cin>>m;
        for(int i=1;i<=m;i++)
        {
            cin>>u>>v>>w;
            if(f[v]>w)//预处理最小值
                f[v]=w;
        }
        int flag=1,k=1;
        int ans=0;
        for(int i=1;i<=n;i++)//只能有一个INF,即根节点
        {
            if(f[i]==INF&&k)
            {
                ans-=INF;
                k=0;
            }
            else if(f[i]==INF)
            {
                flag=0;
                break;
            }
            ans+=f[i];
        }
        if(flag)
            cout<<ans<<endl;
        else
            cout<<-1<<endl;
    }
    return 0;
}

CF 17B Hierarchy

时间: 2024-07-30 01:12:11

CF 17B Hierarchy的相关文章

微信 {&quot;errcode&quot;:40029,&quot;errmsg&quot;:&quot;invalid code, hints: [ req_id: Cf.y.a0389s108 ]&quot;}

{"errcode":40029,"errmsg":"invalid code, hints: [ req_id: Cf.y.a0389s108 ]"} 问题:微信网页授权后,获取到 openid 了,一刷新又没了 微信网页授权获取到的 code 只能使用一次(5分钟内有效),使用一次后,马上失效. 页面授权跳转成功,根据 code 也换取到 openid 了. 此时刷新页面,并不会再次进行授权,而是直接刷新了一下上一次授权跳转后的链接,带的还是

CF with friends and user&#39;s influence considered on NYC data(updated Aug,11st)

Here is the code link: https://github.com/FassyGit/LightFM_liu/blob/master/U_F1.py I use NYC data as other experimens. The split of the training data was seperated by the timeline, and I have normalised the interaction matrix by replacing the checkin

Android性能调优篇之Hierarchy Viewer工具的使用

详细内容请查看我的简书地址:Android性能调优篇之Hierarchy Viewer工具的使用 或者我的个人博客地址:Android性能调优篇之Hierarchy Viewer工具的使用

Bounding Volume Hierarchy BVH in OpenCASCADE

Bounding Volume Hierarchy BVH in OpenCASCADE [email protected] Abstract. Bounding Volume Hierarchy(BVH) organizes geometric objects in the tree based on spatial relationships. Each node in the tree contains an axis-aligned bounding box of all the obj

CF 750

今天CF打的块残废了     就是一废物 A 在24点之前到 直接模拟即可 #include<stdio.h> #include<algorithm> #include<cstring> #include<string> #include<cmath> using namespace std; #define LL long long #define MAXN 1010 #define inf 1000000000.0 int main() {

Salesforce用Apex判断Role Hierarchy的简单代码示例

由于role不同于Profile,带有阶层性质,所以有一些自定义功能要依赖于这种阶层的设定.这样就涉及到role hierarchy的判断问题. 我是一个绝懒之人,所以去网上搜了一下,能找到的方案都或多或少有些缺陷 . 我所提供的方案也是如此,但是想比于浪费太多SOQL查询次数来讲,role的数量不超过50000条已经是足够好了.// 这里Update一下,其实根本不会有那么多的Role,因为默认500,向Salesforce技术支持提票才能达到10000. Talk is cheap, sho

CF #394 (2) 5/6

Codeforces Round #394 (Div. 2) 总结:有毒的一场比赛.做了三题,结果A被叉,B.C挂综测,还hack失败一发,第一次在CF体会到了-50分的感觉..不知道是不是人品好,比赛时room炸了,然后,unrated.. A  水题,判一下0 0,然后abs(a-b)<=1 B  水题,组个间距比较一下,但一个数的时候要判一下 C  直接暴力上的题 D  也是xjb暴力 题意:给出n,l,r, a[], p[],另有两个数组b[], c[],ci=bi-ai.l<=ai,

关于The hierarchy of the type TestBeforeAdvice is inconsistent的问题

今天准备写一个spring aop的demo,创建了TestBeforeAdvice类,该类实现了MethodBeforeAdvice接口,eclipse报了"The hierarchy of the type TestBeforeAdvice is inconsistent"的错误. 后百度找到原因:spring-aop.jar要依赖aopalliance.jar,所以需要导入aopalliance.jar. 以后碰到该问题,可考虑是否有依赖的jar未到入.

一场CF的台前幕后(上)——转

前奏 大约4月份的时候,业界毒瘤pyx噔噔噔跑过来说:“酷爱!我YY了一道题!准备当CF的C” 我当时就被吓傻了."Yet another Chinese round?" “区间取模,区间求和” 感觉这题还不错?不过pyx嫌水了…… 好办!当时我刚刚出完动态仙人掌不久,于是一拍脑袋说:把这个问题出到仙人掌上去! 当然被pyx鄙视了…… 后来一直就没啥动静,直到5月底的CTSC. 试机的时候pyx给我看了套他出的神题……里面有一道题……我不小心读成了下面这个样子: “给定n个m维的模2意