Aizu 2456 Usoperanto 贪心 拓扑排序
Usoperanto
Time Limit: 1 Sec
Memory Limit: 256 MB
题目连接
http://www.bnuoj.com/v3/contest_show.php?cid=6866#problem/J
Description
Usoperanto is an artificial spoken language designed and regulated by Usoperanto Academy. The academy is now in study to establish Strict Usoperanto, a variation of the language intended for formal documents.
In Usoperanto, each word can modify at most one other word, and modifiers are always put before modifiees. For example, with a noun uso ("truth") modified by an adjective makka ("total"), people say makka uso, not uso makka. On the other hand, there have been no rules about the order among multiple words modifying the same word, so in case uso is modified by one more adjectivebeta ("obvious"), people could say both makka beta uso and beta makka uso.
In Strict Usoperanto, the word order will be restricted according to modification costs. Words in a phrase must be arranged so that the total modification cost is minimized. Each pair of a modifier and a modifiee is assigned a cost equal to the number of letters between the two words; the total modification cost is the sum of the costs over all modifier-modifiee pairs in the phrase. For example, the pair of makka and uso in a phrase makka beta uso has the cost of 4 for beta (four letters). As the pair of beta and uso has no words in between and thus the cost of zero, makka beta uso has the total modification cost of 4. Similarly beta makka uso has the total modification cost of 5. Applying the "minimum total modification cost" rule, makka beta uso is preferred to beta makka uso in Strict Usoperanto.
Your mission in this problem is to write a program that, given a set of words in a phrase, finds the correct word order in Strict Usoperanto and reports the total modification cost.
Input
The format of the input is as follows.
N
M0 L0
...
MN-1 LN-1
The first line contains an integer N (1 ≤ N ≤ 106). N is the number of words in a phrase.
Each of the following N lines contains two integers Mi (1 ≤ Mi ≤ 10) and Li (-1 ≤ Li ≤ N - 1, Li ≠ i) describing the i-th word (0 ≤ i ≤ N-1). Mi is the number of the letters in the word. Li specifies the modification: Li = -1 indicates it does not modify any word; otherwise it modifies the Li-th word.
Note the first sample input below can be interpreted as the uso-beta-makka case.
Output
Print the total modification cost.
Sample Input
3
3 -1
4 0
5 0
Sample Output
4
HINT
题意
让你用题中给东西,构成一个链
输入是 ai,bi
表示ai必须要放在第bi个单词前面
代价是ai和bi之间的单词长度和
然后问你最小代价是多少
题解:
贪心就好了
这其实是一个图论题,你连接起来,就会构成一棵树
我们就每次贪心的选择最小的点权,且度数为1的点放在第一个,然后依次放
类似拓扑排序的一样做
@)1%KBO0HM418$J94$1R.jpg)
代码:
//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <bitset>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 1200500
#define mod 1001
#define eps 1e-9
#define pi 3.1415926
int Num;
//const int inf=0x7fffffff;
const ll inf=;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//************************************************************************************* int n;
int fa[maxn],ind[maxn];
ll sum[maxn];
int lst[maxn],top=;
int pre[maxn],nxt[maxn],to[maxn],cnt; void makeedge(int x,int y)
{
to[cnt]=y;nxt[cnt]=pre[x];pre[x]=cnt++;
} int main()
{
memset(pre,-,sizeof(pre));
scanf("%d",&n);
for(int i=;i<n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
fa[i]=y;
if(y>=)
{
ind[y]++;
makeedge(y,i);
}
sum[i]=x;
}
queue<int> q;
while(!q.empty()) q.pop();
for(int i=;i<n;i++)
if(!ind[i]) q.push(i);
ll ans=,tmp=;
while(!q.empty())
{
int s=q.front();
q.pop();
top=;
for(int p=pre[s];p!=-;p=nxt[p])
{
lst[++top]=sum[to[p]];
}
sort(lst+,lst++top);
tmp=;
for(int i=;i<top;i++)
{
tmp+=lst[i];
ans+=tmp;
}
if(fa[s]>=)
{
--ind[fa[s]];
sum[fa[s]]+=sum[s];
if(!ind[fa[s]])
q.push(fa[s]);
}
}
cout<<ans<<endl;
}
Aizu 2456 Usoperanto 贪心 拓扑排序的更多相关文章
- [POI2004] SZP (贪心+拓扑排序)
[问题描述] Byteotian 中央情报局(BIA) 雇佣了许多特工. 他们每个人的工作就是监视 另一名特工. Byteasar 国王需要进行一次秘密行动,所以他要挑选尽量多的信得过的特工. 但 是 ...
- 贪心+拓扑排序 AOJ 2456 Usoperanto
题目传送门 题意:给出一条链,比如x连到y,x一定要在y的左边,且代价是这条链经过的点的权值和,问如何排序使得代价最小 分析:类似拓扑排序,先把入度为0的点入队,把指向该点的所有点按照权值排序,保证这 ...
- Aizu 2456 Usoperanto (贪心)
贪心,对于一个修饰关系可以连一条有向边,在合并的时候,子节点的序列一定是连续安排的,因为如果有交叉,交换以后一定更优. 然后一个序列一个序列的考虑,长度短的应该在前面,否则同样交换以后更优.因此排序以 ...
- 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各个编号的球的重量. 正向拓扑排序,取最小编号给最小编号是不行的,不举出个例子真的 ...
随机推荐
- Complete The Pattern #1
Complete The Pattern #1 Task: You have to write a function pattern which creates the following patte ...
- Android开发之读写联系人
读写联系人需要用到android的ContentProvider 同时需要读和写联系人的权限 需要使用到联系人数据库中的 * raw_contacts表: * contact_id:联系人id * d ...
- poj 1924 Paths on a Grid(组合数学)
题目:http://poj.org/problem?id=1942 题意:给定一个矩形网格的长m和高n,其中m和n都是unsigned int32类型,一格代表一个单位,就是一步,求从左下角到右上角有 ...
- FreeMarker中if标签内的判断条件
reeMarker中的<#if>标签除了里面直接判断 boolean 类型的变量外,也可以进行表达式判断,有几个细节记录一下 1. 判断对象是否存在(null) 经常会用到,如果对象 != ...
- linux 读取input输入设备demo
/******************************************************************* * linux 读取input输入设备demo * 说明: * ...
- 【Java】Java运行cmd命令直接导出.sql文件
Java中的Runtime.getRuntime().exec(commandStr)可以调用执行cmd命令 package Util; import java.io.File; import jav ...
- vtiger 支持 物业收费功能 微信收费
谁要?需要什么功能? 直接在下面留言,博主会整理大家的需求,形成产品,发出来.
- 初次接触Object对象变量
Object 变量存储为 32 位(4 个字节)的地址形式,其为对象的引用.利用 Set 语句,声明为 Object 的变量可以赋值为任何对象的引用. 第一次注意到还有个数据类型,帮助文件里只有上面这 ...
- HNU OJ10320 穿越火线 简单模拟
穿越火线 Time Limit: 10000ms, Special Time Limit:25000ms, Memory Limit:65536KB Total submit users: 12, A ...
- 在windows xp 平台上安装mvc4失败
使用web 平台安装程序,在windows xp上安装mvc4 出现失败,需要主要是windows powershell 2.0安装失败,需要先卸载power shell 1.0或者 winowrm ...