hdu5242 上海邀请赛 优先队列+贪心
题意是给你一棵树 n个点 n-1条边 起点是1 每一个点都有权值 每次能从根节点走到叶子节点 经行k次游戏 每次都是从1開始 拿过的点的权值不能拿第二次 问最大权值和。
開始看到题时也没想到什么方法 就依照常规的来 结果超时了 试着优化了好多次 最后过了 百度题讲解是树链剖分 醉了 还没学!
。。
说说我的做法吧 map【i】=a表示i节点的跟节点为a节点 从全部叶子节点開始入队(有点队列里有三个变量 各自是节点编号 权值 深度 优先级看代码 里面有点贪心的意思) 每次走根节点 假设根节点没走过 则走它 并把该店权值变为0 否则直接跳到1这个节点(假设一个个跳可能会超时)再入队 当出队的编号为1时而且拿的个数小于游戏次数 则拿 否则结束 在跑深度的时候有个优化
開始没有超时了 假设该节点深度已知了 则以后的根节点就不用跑了。!
!
详细看代码吧
#include<stdio.h>
#include<string.h>
#include<queue>
#include<iostream>
using namespace std; int map[100010],mark[100010];
int Deep[100010];
__int64 num[100010];
struct node
{
__int64 value;
int ii;
int deep;
bool operator < (const node& x) const
{
return deep<x.deep||(deep==x.deep&&value<x.value);
}
}a;
int main()
{
int T,i,j,n,k,r=1;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&k);
for(i=1;i<=n;i++)
{
scanf("%I64d",&num[i]);
}
memset(mark,0,sizeof(mark));
for(i=1;i<n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
mark[x]=1;
map[y]=x;
}
priority_queue<node>Q;
memset(Deep,0,sizeof(Deep));
for(i=1;i<=n;i++)
{
if(mark[i]==0)
{
int x=map[i];
int d=1;
while(x!=1)
{
if(Deep[x]>0) {d+=Deep[x];break;}
x=map[x];
d++;
}
x=i;
while(x!=1)
{
if(Deep[x]>0) break;
Deep[x]=d;
x=map[x];
d--;
}
a.deep=Deep[i];
a.value=num[i];
a.ii=i;
Q.push(a); }
}
//for(i=1;i<=n;i++)
//printf("%d ^^^ %d\n",i,Deep[i]);
__int64 sum=0;
int cont=0;
while(!Q.empty())
{
a=Q.top();
Q.pop();
int x=map[a.ii];
/*while(num[x]==0&&x!=1)
{
x=map[x];
}*/
if(a.ii==1)
{
a.value+=num[1];
num[1]=0;
sum+=a.value;
cont++;
if(cont>=k) break;
}
else
{
if(num[x]==0)
{
a.ii=1;
a.deep=0;
}
else
{
a.ii=x;
a.deep=Deep[x];
a.value+=num[x];
num[x]=0;
}
Q.push(a);
}
}
printf("Case #%d: %I64d\n",r++,sum);
}
return 0;
}
hdu5242 上海邀请赛 优先队列+贪心的更多相关文章
- [POI 2001+2014acm上海邀请赛]Gold Mine/Beam Cannon 线段树+扫描线
Description Byteman, one of the most deserving employee of The Goldmine of Byteland, is about to re ...
- 最高的奖励 - 优先队列&贪心 / 并查集
题目地址:http://www.51cpc.com/web/problem.php?id=1587 Summarize: 优先队列&贪心: 1. 按价值最高排序,价值相同则按完成时间越晚为先: ...
- POJ2431 优先队列+贪心 - biaobiao88
以下代码可对结构体数组中的元素进行排序,也差不多算是一个小小的模板了吧 #include<iostream> #include<algorithm> using namespa ...
- hdu3438 Buy and Resell(优先队列+贪心)
Buy and Resell Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- HDU5090——Game with Pearls(匈牙利算法|贪心)(2014上海邀请赛重现)
Game with Pearls Problem DescriptionTom and Jerry are playing a game with tubes and pearls. The rule ...
- ZOJ-3410Layton's Escape(优先队列+贪心)
Layton's Escape Time Limit: 2 Seconds Memory Limit: 65536 KB Professor Layton is a renowned arc ...
- CodeForces - 853A Planning (优先队列,贪心)
Helen works in Metropolis airport. She is responsible for creating a departure schedule. There are n ...
- poj2431(优先队列+贪心)
题目链接:http://poj.org/problem?id=2431 题目大意:一辆卡车,初始时,距离终点L,油量为P,在起点到终点途中有n个加油站,每个加油站油量有限,而卡车的油箱容量无限,卡车在 ...
- H - Expedition 优先队列 贪心
来源poj2431 A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being ...
随机推荐
- EasyUI系列学习(四)-Droppable(放置)
一.创建组件 1.使用标签创建一个放置区 <div id="pox" class="easyui-droppable" style="width ...
- android ormlite 清空表
delete from TableName; //清空数据 update sqlite_sequence SET seq = where name ='TableName';//自增长ID为0 Sam ...
- SQL 循环插入10000条
SQL> create table tt_test ( x int, y char(50) ); Table created. SQL> SQL> begin 2 for i in ...
- JS——百度背景图
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 【译】x86程序员手册03 - 2.1内存组织和分段
2.1 Memory Organization and Segmentation 内存组织和分段 The physical memory of an 80386 system is organized ...
- 05--QT常用的类
http://blog.csdn.net/HMSIWTV/article/category/1128561/2 Qt常用类(1)—— 开端 使用Qt进行编程必须对 Qt 中常用的类有一定的 ...
- Lvs Keepalive DR模式高可用配置
Lvs Keepalive DR模式配置 一.环境 #DIP# eth0:192.168.233.145#VIP# eth0:0 192.168.233.250/32 #RIP1:192.168.23 ...
- 我所理解的Android和iOS上的View
View,几乎是所有界面系统中的基类,在iOS里面是UIView,在Android里是View. 那么,到底View是什么东西,他做了些什么,他是怎么做到的,在这篇文章中,希望能带给大家一些启发. 抽 ...
- PHP 之ip查询接口
/** * @param $ip 待查询的ip * @return mixed */ function getIpAddressInfo($ip) { $ipurl = 'http://api.ip1 ...
- git 如何创建一个分支
参考: https://jingyan.baidu.com/article/adc81513b95a20f723bf73bf.html 首先进入本地git仓库目录下,打开git bash环境 使用gi ...