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的更多相关文章

  1. 使用JSONObject.fromObject的时候出现“There is a cycle in the hierarchy”异常 的解决办法

    在使用JSONObject.fromObject的时候,出现“There is a cycle in the hierarchy”异常.   意思是出现了死循环,也就是Model之间有循环包含关系: ...

  2. ORA-00494: enqueue [CF] held for too long (more than 900 seconds) by 'inst 1, osid 5166'

    凌晨收到同事电话,反馈应用程序访问Oracle数据库时报错,当时现场现象确认: 1. 应用程序访问不了数据库,使用SQL Developer测试发现访问不了数据库.报ORA-12570 TNS:pac ...

  3. IOS 开发中 Whose view is not in the window hierarchy 错误的解决办法

    在 IOS 开发当中经常碰到 whose view is not in the window hierarchy 的错误,该错误简单的说,是由于 "ViewController" ...

  4. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  5. cf Round 613

    A.Peter and Snow Blower(计算几何) 给定一个点和一个多边形,求出这个多边形绕这个点旋转一圈后形成的面积.保证这个点不在多边形内. 画个图能明白 这个图形是一个圆环,那么就是这个 ...

  6. 谈谈计算机上的那些存储器-Memory Hierarchy

    文章首发于浩瀚先森博客http://www.guohao1206.com/2016/12/07/1248.html 说到计算机上的存储器,很多人第一反应是硬盘,然后是内存. 其实在计算机上除了硬盘和内 ...

  7. ARC下OC对象和CF对象之间的桥接(bridge)

    在开发iOS应用程序时我们有时会用到Core Foundation对象简称CF,例如Core Graphics.Core Text,并且我们可能需要将CF对象和OC对象进行互相转化,我们知道,ARC环 ...

  8. [Recommendation System] 推荐系统之协同过滤(CF)算法详解和实现

    1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...

  9. CF memsql Start[c]UP 2.0 A

    CF memsql Start[c]UP 2.0 A A. Golden System time limit per test 1 second memory limit per test 256 m ...

随机推荐

  1. C++ 函数声明中指定,默认参数

    C++ 在声明函数的时候,如果指定了,参数的默认值,再调用函数的时候可以省略后面的参数. 如果调用函数写上的参数,但是不全.参数列表后面的使用默认值.如下例子,一看就清楚了. #include < ...

  2. 梳理一下重装sql2008R2sp1步骤

    我的电脑是这样,最早的时候装的是2005,后来公司用到2008,我就手动卸载,但是好像卸载的不够彻底,在装2008的时候,选择升级方式安装. 虽然成功了,但是在运行select @@version 时 ...

  3. 《Python爬虫学习系列教程》学习笔记

    http://cuiqingcai.com/1052.html 大家好哈,我呢最近在学习Python爬虫,感觉非常有意思,真的让生活可以方便很多.学习过程中我把一些学习的笔记总结下来,还记录了一些自己 ...

  4. NOI08冬令营 数据结构的提炼与压缩

    无聊随手翻,翻到了一个这样的好东西--据结构的提炼与压缩: 为了防止以后忘记,这里把论文里的题目都纪录一下吧. 1.二维结构的化简 问题一:ural 1568 Train car sorting 定义 ...

  5. Activity 和 Intent

    Activity 和 Intent 一.Intent指向Activity 二.利用 Intent 向第二个 Activity 传数据 三.利用 Intent 接受第二个 Activity 的返回值 四 ...

  6. transition与animation

    以前,一直都知道,transition是animation的一个简化版,甚至不算是动画,而是一种过渡. transition的用法 早两天用transition写了一个按钮滑动的效果,类似于IOS的设 ...

  7. python 读取图片的尺寸、分辨率

    #需要安装PIL模块 #encoding=gbk#--------------------------------------------------------------------------- ...

  8. ASP.NET - 对URL传递的值进行编码Server.UrlEncode()

    /// <summary> /// 搜索内容 /// </summary> /// <param name="sender"></para ...

  9. MongoDB shell操作

    shell命令操作语法和JavaScript很类似,其实控制台底层的查询语句都是用JavaScript脚本完成操作的.使用shell 命令,需要启动mongo.exe. 常用shell命令如下: 1. ...

  10. HBase数据存储格式

    好的数据结构,对于检索数据,插入数据的效率就会很高. 常见的数据结构 B+树 根节点和枝节点非常easy,分别记录每一个叶子节点的最小值,并用一个指针指向叶子节点.  叶子节点里每一个键值都指向真正的 ...