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 ;
}

最新文章

  1. 【java】Naming.bind和Registry.bind区别
  2. Visual 2015创建新项,缺少ADO.NET 实体数据模型的解决方法
  3. Asp.net MVC5 框架揭秘 S412 实例解析 – 绝妙的扩展 模式的胜利
  4. 413 Request Entity Too Large
  5. ubuntu静态IP配置
  6. surface实例-小球弹起事例
  7. MVC 国内架构设计
  8. Android通过使用Properties保存配置
  9. Cookie小解2
  10. 内核调试神器SystemTap — 简介与使用(一)
  11. 8天入门docker系列 —— 第一天 docker出现前的困惑和简单介绍
  12. SpringBoot的Profiles根据开发环境和测试环境载入不同的配置文件
  13. Fiddler 抓包工具
  14. Runloop深入理解
  15. pyqt5 eric6 pyqt5-tools
  16. svn+http+ad域
  17. mysqldump-1045
  18. 51nod 贪心算法题集
  19. HTML JavaScript 基础学习
  20. 评论:一套Developer Express控件包 For Delphi7

热门文章

  1. python 实用pickle序列化
  2. session机制大揭秘(结合cookie)
  3. docker安装mysql挂载宿主本地目录资源后无法启动的问题
  4. Mongodb 官网驱动2.2.4.26版本 增,删 改,查,mongodb2.2.4.26
  5. Linux-用户和权限
  6. 一个表格中选定的tr,显示在另一个表格中
  7. poj1639顶点度限制生成树
  8. fragment error
  9. C. Pearls in a Row
  10. PHP实用小程序(六)