B. Hierarchy
http://codeforces.com/problemset/problem/17/B
用邻接矩阵建图后,
设cost[v]表示去到顶点v的最小值。
很多个人去顶点v的话,就选最小的那个就OK
然后,如果有大于等于2个人的cost[v]是inf的,就不符合boss只有一个这个规矩。-1
不应该只统计有孤立点就输出-1,因为m可以等于0(坑)
另外这个图是不会有环的,因为有环就表明相对大小乱了。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = 1e3 + ;
int e[maxn][maxn];
int cost[maxn];
struct node {
int u, v, w;
int tonext;
}E[ + ];
int first[maxn];
int has[maxn];
int num;
void add(int u, int v, int w) {
++num;
E[num].u = u;
E[num].v = v;
E[num].w = w;
E[num].tonext = first[u];
first[u] = num;
}
int a[maxn];
void work() {
int n;
cin >> n;
for (int i = ; i <= n; ++i) {
cin >> a[i];
}
int m;
cin >> m;
memset(e, 0x3f, sizeof e);
for (int i = ; i <= m; ++i) {
int u, v, w;
cin >> u >> v >> w;
if (a[u] > a[v]) {
has[v] = ;
has[u] = ;
e[u][v] = min(e[u][v], w);
}
}
memset(cost, 0x3f, sizeof cost);
for (int i = ; i <= n; ++i) {
for (int j = ; j <= n; ++j) {
if (e[i][j] != inf) {
add(i, j, e[i][j]);
}
}
}
for (int i = ; i <= n; ++i) {
for (int j = first[i]; j; j = E[j].tonext) {
int v = E[j].v;
cost[v] = min(cost[v], E[j].w);
}
}
int ans = ;
int t = ;
for (int i = ; i <= n; ++i) {
if (cost[i] == inf) { //不能到达
t++; //有一个没有老板了,
if (t == ) { //2个没有就不行了
cout << - << endl;
return;
}
continue;
}
ans += cost[i];
}
cout << ans << endl;
} int main() {
#ifdef local
freopen("data.txt","r",stdin);
#endif
IOS;
work();
return ;
}
B. Hierarchy的更多相关文章
- 使用JSONObject.fromObject的时候出现“There is a cycle in the hierarchy”异常 的解决办法
在使用JSONObject.fromObject的时候,出现“There is a cycle in the hierarchy”异常. 意思是出现了死循环,也就是Model之间有循环包含关系: ...
- IOS 开发中 Whose view is not in the window hierarchy 错误的解决办法
在 IOS 开发当中经常碰到 whose view is not in the window hierarchy 的错误,该错误简单的说,是由于 "ViewController" ...
- 谈谈计算机上的那些存储器-Memory Hierarchy
文章首发于浩瀚先森博客http://www.guohao1206.com/2016/12/07/1248.html 说到计算机上的存储器,很多人第一反应是硬盘,然后是内存. 其实在计算机上除了硬盘和内 ...
- whose view is not in the window hierarchy
参考:http://www.jianshu.com/p/9e90cb866fdf 在做界面跳转的时候,我们经常会用到这两个函数 func dismissViewControllerAnimated(f ...
- Query Designer:Hierarchy层级显示
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- The hierarchy of the type is inconsistent错误问题
在springMVC的AOP 面向切面编程中,引用: package com.ah.aop; import java.lang.reflect.Method; import org.springfra ...
- Warning: Attempt to present on whose view is not in the window hierarchy!
当我想从一个VC跳转到另一个VC的时候,一般会用 - (void)presentViewController:(UIViewController *)viewControllerToPresent a ...
- The hierarchy of the type NsRedisConnectionFactory is inconsistent
The hierarchy of the type is inconsistent 解释为:层次结构的类型不一致 由于我在eclipse里建了两个JAVA PROJECT项目,分别是A projiec ...
- 开发错误记录1:解决:Only the original thread that created a view hierarchy can touch its views.
今天在项目中要使用圆角头像,导入开源 CircleImageView ,然后setImageBitmap()时 运行时就会发现,它会报一个致命性的异常:: · ERROR/AndroidRuntime ...
- json:There is a cycle in the hierarchy!
在使用JSONObject.fromObject的时候,出现“There is a cycle in the hierarchy”异常. 意思是出现了死循环,也就是Model之间有循环包含关系: 解决 ...
随机推荐
- js获取dom对象style样式的值
js获取到的dom对象的style通常是没有值得,因为我们都写在外部文件中,从慕课网上见到讲师封装的一个方法,挺不错.特此记录下来. function getStyle(obj,attr){ if(o ...
- luogu 3812 【模板】 线性基
线性基是一个支持在集合里插入数并查询最大子集异或值 #include<iostream> #include<cstdio> #include<cstring> #i ...
- 如何在Flask的构架中传递logger给子模块
Logger的传递 作为一个新手,如何将主函数的logger传入子模块是一件棘手的事情.某些情况下可以直接将logger作为参数传入子模块的构造函数中,但倘若子模块与主模块存在相互依赖的关系则容易出现 ...
- TypeError: Unexpected keyword argument passed to optimizer: amsgrad原因及解决办法
原因: AMSgrad只支持2017年12月11日后发行的keras版本 解决办法: pip install --upgrade keras
- Gibonacci number-斐波那契数列
Description In mathematical terms, the normal sequence F(n) of Fibonacci numbers is defined by the r ...
- Day01:Python入门
一.编程与编程语言 编程的目的是将人类的思想流程按照某种能够被计算机识别的表达方式传递给计算机,从而让计算机能像人脑一样自动执行工作. 能被计算机所识别的表达方式是编程语言,python就是一门编程语 ...
- SQL集锦之IndexOf、LastIndexOf 【转】
DECLARE @Name NVARCHAR (50)SET @Name = '12345.67890ABCDE.FGHIJKLMNOPQRSTUVWXYZTest' DECLARE @Positio ...
- OnCtlColor
https://baike.baidu.com/item/OnCtlColor/4750440?fr=aladdin CTLCOLOR_BTN 按钮控件 · CTLCOLOR_DLG 对话框 · CT ...
- 性能测试之Jmeter学习(二)
一.Jmeter的基本操作 1.添加|移除测试元件 2.加载和保存测试元件 3.配置测试对中的测试元件 4.保存测试计划 5.运行测试计划 6.终止测试 7.错误报告 二.Jmeter体系结构 注释: ...
- jquery中innerheight outerHeight()与height()的区别
1. .height() 获取匹配元素集合中的第一个元素的当前计算高度值 或 设置每一个匹配元素的高度值(带一个参数). 注意:1).css('height')和.height()之间的区别是后者返回 ...