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.
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 ...
随机推荐
- POJ 2528 QAQ段树+分离
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submitcid=58236#statu ...
- 数据库迁移 - SQLServer->MySQL
SqlServer转换为Mysql的一款工具推荐(mss2sql)
- Arduino 入门程序示例之直流电机(2015-06-15)
概述 演示直流电机的控制. 示例程序 PWM控制直流电机 略过控制电机转停的示例啦,有需要就把这里的 PWM 换成数字口输出 HIGH 或 LOW 就行了. // ------------------ ...
- 32位程序在64位系统上获取系统安装时间(要使用KEY_WOW64_64KEY标记)
众所周知,取系统的安装时间可取注册表HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion的子项InstallDate,此值是个 ...
- Android中通过耳机按键控制音乐播放的实现
今天在研究Android中实现Android 4.2.2源码中的Music应用的源码,关于通过耳机按键控制音乐播放的实现,有点好奇,就仔细分析了一下源码, 主要由 MediaButtonIntentR ...
- HDOJ 1800 Flying to the Mars 盲目搜索......................so easy...........
check the original problem here:http://acm.hdu.edu.cn/showproblem.php?pid=1800 the AC code: #include ...
- 在Ubuntu 12.04下编译qtiplot
不在windows下,再加上不想用盗版,所以需要一个origin的替代品——qtiplot.虽然我非常抵抗用这种不停点来点去的软件,用R的ggplot2画图多好啊,高效.优雅.漂亮,但是终抵不过老板一 ...
- Windows Azure 安全最佳实践 - 第 3 部分:确定安全框架
构建云应用程序时,安全始终是计划和执行Windows Azure的首要核心因素.第 1 部分提出安全是一项共同责任,Windows Azure为您的应用程序提供超出内部部署应用程序需求的强大安全功能. ...
- checkbox之checked的方法(attr和prop)区别
1. $('#checkbox').click(function(){ if($('#checkbox').is(':checked')) { $(".sendmailhui"). ...
- 论文阅读笔记 - YARN : Architecture of Next Generation Apache Hadoop MapReduceFramework
作者:刘旭晖 Raymond 转载请注明出处 Email:colorant at 163.com BLOG:http://blog.csdn.net/colorant/ 更多论文阅读笔记 http:/ ...