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之间有循环包含关系: 解决 ...
随机推荐
- ansible操作模块相关
1. 查看模块可用参数命令 ansible-doc -s module_name
- Understand JavaScript’s “this” With Clarity, and Master It
The this keyword in JavaScript confuses new and seasoned JavaScript developers alike. This article a ...
- vue 路由监听
发现网上其实有很多种答案,但是测试之后发现很多都不行,或者写的不完整. 一.在app.vue组件内,增加监听$route,如下: watch: { $route(to, from) { console ...
- Console Event Handling
http://www.codeproject.com/Articles/2357/Console-Event-Handling Console Event Handling Kumar Gaurav ...
- 2.7-2.8 导入、导出数据(进/出)hive表的方式
一.导入数据进hive表 1.语法 LOAD DATA [LOCAL] INPATH 'filepath' [OVERWRITE] INTO TABLE tablename [PARTITION (p ...
- 【Data Structure & Algorithm】二叉树中和为某值的所有路径
二叉树中和为某值的所有路径 题目:输入一个整数和一个二叉树,从树的根节点开始往下访问一直到叶节点所经过的所有节点形成一条路径.打印出和与输入整数相等的所有路径. 例如输入整数22和如下二叉树: 10 ...
- python-os.walk()使用举例
文件目录结构 dir 1 1 1.txt 2.txt 3.txt 2 2.txt 3 4 4.txt 3.txt 1.txt 2 2.txt 3 3.txt dir.txt 代码: import os ...
- 利用ant 和 Junit 生成测试报告
我们除了使用java来直接运行junit之外,我们还可以使用junit提供的junit task与ant结合来运行. 涉及的几个主要的ant task如下: <junit>,定义一个jun ...
- Qt开篇
使用Qt两年有余,遇到问题多是现查现用,由于之前供职于一家保密性较强的单位,遇到的很多问题没有被记录下来.从今天开始,我会记记录自己的笔记.
- 51nod 1102 【单调栈】
思路: 对于这个高度往左能延伸最远x,往右能延伸最远y,(x+1+y)*w; 利用单调栈就行了: #include <cstdio> #include <stack> #inc ...