贪心+拓扑排序 AOJ 2456 Usoperanto
题意:给出一条链,比如x连到y,x一定要在y的左边,且代价是这条链经过的点的权值和,问如何排序使得代价最小
分析:类似拓扑排序,先把入度为0的点入队,把指向该点的所有点按照权值排序,保证这样是代价是最小的,然后把这一块看成一个点继续入队。看图更简单:
/************************************************
* Author :Running_Time
* Created Time :2015/10/3 星期六 13:02:41
* File Name :J.cpp
************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 1e6 + 10;
const int E = 2e6 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const double EPS = 1e-8;
struct Edge {
int v, nex;
}edge[E];
int fa[N], head[N], in[N];
int w[N], sta[N];
int n, e;
ll ans; void init(void) {
memset (head, -1, sizeof (head));
memset (in, 0, sizeof (in));
e = 0;
} void add_edge(int u, int v) {
edge[e].v = v; edge[e].nex = head[u];
head[u] = e++;
} void BFS(void) {
queue<int> Q;
for (int i=0; i<n; ++i) {
if (!in[i]) {
Q.push (i);
}
}
while (!Q.empty ()) {
int u = Q.front (); Q.pop ();
int tot = 0;
for (int i=head[u]; ~i; i=edge[i].nex) {
int v = edge[i].v;
sta[tot++] = w[v];
}
sort (sta, sta+tot);
for (int i=0; i<tot; ++i) {
ans += 1ll * sta[i] * (tot - i - 1);
}
if (fa[u] == -1) continue;
w[fa[u]] += w[u];
if (! (--in[fa[u]])) {
Q.push (fa[u]);
}
}
} int main(void) {
while (scanf ("%d", &n) == 1) {
init ();
for (int i=0; i<n; ++i) {
scanf ("%d%d", &w[i], &fa[i]);
if (fa[i] != -1) {
in[fa[i]]++;
add_edge (fa[i], i);
}
}
ans = 0;
BFS ();
printf ("%lld\n", ans);
} return 0;
}
贪心+拓扑排序 AOJ 2456 Usoperanto的更多相关文章
- [POI2004] SZP (贪心+拓扑排序)
[问题描述] Byteotian 中央情报局(BIA) 雇佣了许多特工. 他们每个人的工作就是监视 另一名特工. Byteasar 国王需要进行一次秘密行动,所以他要挑选尽量多的信得过的特工. 但 是 ...
- Aizu 2456 Usoperanto 贪心 拓扑排序
Usoperanto Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.bnuoj.com/v3/contest_show.php?cid= ...
- hdu-5695 Gym Class(贪心+拓扑排序)
题目链接: Gym Class Time Limit: 6000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- 2016"百度之星" - 初赛(Astar Round2A)1006 Gym Class(HDU5695)——贪心+拓扑排序
分析:首先,利用贪心可知,如果要所有人的分数和最高,需要把序号大的优先放在前面.其次,对于a的前面不能为b,那么只能a在b前面了,那么就建立一条从a到b的边,并且b的入度加1.然后就是拓扑排序了.要分 ...
- poj 3687 Labeling Balls - 贪心 - 拓扑排序
Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them with 1 to N ...
- Berland Army CodeForces - 883B (贪心,拓扑排序)
大意: n个点, 点$i$的等级为$r_i$, 只给出部分点的$r$值, $r_i$的范围为[1,k], 且[1,k]都至少有一个. 给定m条有向边, (x,y)表示$r[x]>r[y]$, 求 ...
- Luogu P5603 小C与桌游【贪心+拓扑排序】
[Description]https://www.luogu.com.cn/problem/P5603 \(\;\) 题意可以简化为:一个不保证联通,n个点,m条边的DAG(有向无环图),构造一个拓扑 ...
- POJ3687 Labeling Balls(拓扑排序\贪心+Floyd)
题目是要给n个重量1到n的球编号,有一些约束条件:编号A的球重量要小于编号B的重量,最后就是要输出字典序最小的从1到n各个编号的球的重量. 正向拓扑排序,取最小编号给最小编号是不行的,不举出个例子真的 ...
- 2019.01.20 bzoj5158 Alice&Bob(拓扑排序+贪心)
传送门 短代码简单题. 题意简述:对于一个序列XXX,定义其两个伴随序列a,ba,ba,b,aia_iai表示以第iii个数结尾的最长上升子序列长度,bib_ibi表示以第iii个数开头的最长下降 ...
随机推荐
- 笔记整理--LibCurl开发
LibCurl开发_未了的雨_百度空间 - Google Chrome (2013/7/26 21:11:15) LibCurl开发 一:LibCurl 编程流程1.调用curl_global_ini ...
- yum lock
状态 :睡眠中,进程ID:18439Another app is currently holding the yum lock; waiting for it to exit... 另一个应用程序是: ...
- storage engine option for directoryPerDB
Requested option conflicts with current storage engine option for directoryPerDB; you requested true ...
- ie的兼容方法,条件注释的方法
1.终极方法:条件注释 <!--[if lte IE 6]> 这段文字仅显示在 IE6及IE6以下版本. <![endif]--> <!--[if gte IE 6]&g ...
- 数据结构之 图论---图的深度遍历( 输出dfs的先后遍历序列 )
图的深度遍历 Time Limit: 1000MS Memory limit: 65536K 题目描述 请定一个无向图,顶点编号从0到n-1,用深度优先搜索(DFS),遍历并输出.遍历时,先遍历节点编 ...
- 【Maven】pom.xml(2)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- UIFont 字体样式 [UIFont fontWithName~];
设置字体样式代码:[UIFont fontWithName:@"Arial-BoldMT" size:15] 下边一一列举: 一:Font Family: American Typ ...
- ios 图片拉伸方法
前提:要注意图片的size和展示的图片view的size的大小. 假如图片高度50,展示图片view的高度30,拉伸会变成剪切. 如果图片尺寸不对,可以用mac自带的图片编辑器修改大小: 双击打开图 ...
- CF838D Airplane Arrangement
题目描述:https://www.luogu.org/problemnew/show/CF838D(有翻译) (为什么博客园把我刚写的给吞了……orz) 这题当初看的十分懵逼,不过听了肖大佬的做法还是 ...
- HBase之一:HBase原理和设计
一.简介 HBase —— Hadoop Database的简称,Google BigTable的另一种开源实现方式,从问世之初,就为了解决用大量廉价的机器高速存取海量数据.实现数据分布式存储提供可靠 ...