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