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.
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 the only line — the minimum cost of building such a hierarchy, or -1 if it is impossible to build it.
4
7 2 3 1
4
1 2 5
2 4 1
3 4 1
1 3 5
11
3
1 2 3
2
3 1 2
3 1 3
-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.
#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的更多相关文章
-
使用JSONObject.fromObject的时候出现“There is a cycle in the hierarchy”异常 的解决办法
在使用JSONObject.fromObject的时候,出现“There is a cycle in the hierarchy”异常. 意思是出现了死循环,也就是Model之间有循环包含关系: ...
-
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 ...
-
IOS 开发中 Whose view is not in the window hierarchy 错误的解决办法
在 IOS 开发当中经常碰到 whose view is not in the window hierarchy 的错误,该错误简单的说,是由于 "ViewController" ...
-
cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
-
cf Round 613
A.Peter and Snow Blower(计算几何) 给定一个点和一个多边形,求出这个多边形绕这个点旋转一圈后形成的面积.保证这个点不在多边形内. 画个图能明白 这个图形是一个圆环,那么就是这个 ...
-
谈谈计算机上的那些存储器-Memory Hierarchy
文章首发于浩瀚先森博客http://www.guohao1206.com/2016/12/07/1248.html 说到计算机上的存储器,很多人第一反应是硬盘,然后是内存. 其实在计算机上除了硬盘和内 ...
-
ARC下OC对象和CF对象之间的桥接(bridge)
在开发iOS应用程序时我们有时会用到Core Foundation对象简称CF,例如Core Graphics.Core Text,并且我们可能需要将CF对象和OC对象进行互相转化,我们知道,ARC环 ...
-
[Recommendation System] 推荐系统之协同过滤(CF)算法详解和实现
1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...
-
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 ...
随机推荐
-
CUDA编程
目录: 1.什么是CUDA 2.为什么要用到CUDA 3.CUDA环境搭建 4.第一个CUDA程序 5. CUDA编程 5.1. 基本概念 5.2. 线程层次结构 5.3. 存储器层次结构 5.4. ...
-
ebj笔记
所有EJB3.0开发商都必须提供一个JMS provider的实现,JMS provider对于message-driven bean而言绝对是必须的.JMS是一套用于访问企业消息系统的开发商中立的A ...
-
酷炫地给py代码标上行数
Python IDLE是没有显示行号的功能的,今天学了一个方式可以酷炫地给自己的代码加上行号,该方法直接修改代码,慎用哦!代码如下: import fileinput for line in file ...
-
java对象序列化、反序列化
平时我们在Java内存中的对象,是无法进行IO操作或者网络通信的,因为在进行IO操作或者网络通信的时候,人家根本不知道内存中的对象是个什么东西,因此必须将对象以某种方式表示出来,即存储对象中的状态.一 ...
-
Android 弹出输入框
final EditText inputServer = new EditText(SettingActivity.this); AlertDialog.Builder builder = new A ...
-
什么是 maven的uber-jar
在maven的一些文档中我们会发现 "uber-jar"这个术语,许多人看到后感到困惑.其实在很多编程语言中会把super叫做uber (因为suber可能是关键字), 这是上世纪 ...
-
js -history.back(-1)和history.go(-1) 区别
既然history.back(-1)和history.go(-1)都是返回之前页面, history.back(-1)//直接返回当前页的上一页,,是个新页面 history.go(-1)// ...
-
**CI中自动类加载的用法总结
总结: 哪一个类中用到某一个类,就在构造函数中加载这个类,比如m_attach,C_Feed类中有用到,那么就在构造函数中加载 控制器: class C_Feed extends CI_Control ...
-
[原创]Fitnesse测试工具介绍及安装
1 Fitnesse简介 Fitnesse是一款开源的验收测试框架,完全有java语言编写完成,支持多语言软件产品的测试,包括(java,c,c++,python,php),在Fitnesse框架中, ...
-
【SQL】SQL中Case When的用法
Case具有两种格式.简单Case函数和Case搜索函数. --简单Case函数 CASE sex ' THEN '男' ' THEN '女' ELSE '其他' END --Case搜索函数 ' T ...