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 ...
随机推荐
- Android中文API(129) —— AudioManager
前言 本章内容是android.media.AudioManager,版本为Android 3.2 r1,翻译来自"文炜",欢迎访问他的博客:"http://www.cn ...
- First_1
#region 练习题 1.4 (1) ///*求一个N阶方阵所有原素的和.(改进:二维数组的大小可以自己输入)*/ //Console.WriteLine("请输入i和j的值:" ...
- 关于C++的子类指针指向父类
基类指针引用派生类对象 用基类指针引用一个派生类对象,由于派生类对象也是基类的对象,所以这种引用是安全的; 但是只能引用基类成员. 若试图通过基类指针引用那些只在派生类中才有的成员,编译器会报告语法错 ...
- BZOJ 1131: [POI2008]Sta( dfs )
对于一棵树, 考虑root的答案向它的孩子转移, 应该是 ans[son] = (ans[root] - size[son]) + (n - size[son]). so , 先 dfs 预处理一下, ...
- Android自定义照相机实现(拍照、保存到SD卡,利用Bundle在Acitivity交换数据)
Android自定义照相机实现 近期小巫在学校有一个创新项目,也不是最近,是一个拖了很久的项目,之前一直没有去搞,最近因为要中期检查,搞得我跟小组成员一阵忙活,其实开发一款照相机软件并不太难,下面就是 ...
- TF-IDF与余弦相似性的应用(一):自动提取关键词 - 阮一峰的网络日志
TF-IDF与余弦相似性的应用(一):自动提取关键词 - 阮一峰的网络日志 TF-IDF与余弦相似性的应用(一):自动提取关键词 作者: 阮一峰 日期: 2013年3月15日 ...
- UVA 1524 - Hot or Cold?(数学)
UVA 1524 - Hot or Cold? 题目链接 题意:给一个一元n次方程,带入x表示时间,f(x)表示温度,如今要求[s, e]的平均温度 思路:平均温度就是 总温度/ (e - s),画出 ...
- poj 2891 Strange Way to Express Integers(中国剩余定理)
http://poj.org/problem?id=2891 题意:求解一个数x使得 x%8 = 7,x%11 = 9; 若x存在,输出最小整数解.否则输出-1: ps: 思路:这不是简单的中国剩余定 ...
- MSSQL - 创建新用户
1.首先使用Windows身份验证登陆. 2.然后一次打开:安全性--->登录名.右键登录名,点击新建登录名. 3.常规选项卡下:填写登录名.选择SQL Server身份验证,填写登录名密码.取 ...
- WCF技术剖析之十七:消息(Message)详解(上篇)
原文:WCF技术剖析之十七:消息(Message)详解(上篇) [爱心链接:拯救一个25岁身患急性白血病的女孩[内有苏州电视台经济频道<天天山海经>为此录制的节目视频(苏州话)]]消息交换 ...