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之间有循环包含关系: 解决 ...
随机推荐
- hdu 2188 悼念512汶川大地震遇难同胞——选拔志愿者(Bash Game)
题意:从0开始捐款,每次不超过m元,首先达到n元的获胜 思路:等同于从n开始,每次取不超过m,首先达到0的获胜.(Bash Game) #include<iostream> #includ ...
- hpuoj 1193: Interval
Interval [STL.双指针.二分] 题目链接 http://acm.hpu.edu.cn/problem.php?id=1193 或者 题目链接 http://acm.nyist.net/Ju ...
- liunx目录/etc下相关配置
这些都是比较有实用性的系统配置,收藏下,以备不时之需!以下是etc下重要配置文件解释: 1./etc/hosts #文件格式: IPaddress hostname aliases #文件功能: 提 ...
- 「LuoguP3796」 【模板】AC自动机(加强版)
题目描述 有N个由小写字母组成的模式串以及一个文本串T.每个模式串可能会在文本串中出现多次.你需要找出哪些模式串在文本串T中出现的次数最多. 输入输出格式 输入格式: 输入含多组数据. 每组数据的第一 ...
- JNI之JAVA调用C++接口
1.JNI定义(来自百度百科) JNI是Java Native Interface的缩写,中文为JAVA本地调用.从Java1.1开始,Java Native Interface(JNI)标准成为ja ...
- windows下的套接字IO模型
一般情况下,IO操作的行为受两种因素的影响: IO操作对象的类型(阻塞还是非阻塞) 获取IO操作结果的方式(同步还是异步). 同步就是指操作的发起和操作结果的获取由调用者完成. 异步指操作发起由调用方 ...
- 利用openssl进行base64的编码与解码
openssl可以直接使用命令对文件件进行base64的编码与解码,利用openssl提供的API同样可以做到这一点. 废话不多说,直接上代码了.需要注意的是通过base64编码后的字符每64个字节都 ...
- 在IIS6.0以上版本发布Ajax中,解决添加.v路径找不到的问题?
问题描述:配置Aiax方式如下: 1.在AppCode中加入文件夹Ajax,加入两个类文件: Ajax.cs: using System; using System.Collections.Gener ...
- Json 不同语言的使用
Javascript: 1.使用eval var parse_json_by_eval = function(str){ return eval('('+str+')'); } var value = ...
- JavaScript-Tool:jquery.qrcode.js
ylbtech-JavaScript-Tool:jquery.qrcode.js 1.返回顶部 1. 插件描述:jquery.qrcode.js 是一个能够在客户端生成矩阵二维码QRCode 的jqu ...